Team Emertxe
C++
Beyond C
C++
Contents
int main(void) {
Introduction to C++
Concepts of Object Oriented Programming (OOP)
Pillars of OOP
Abstraction
Encapsulation
Inheritance
Polymorphism
Properties of C++
}
Introduction to C++
C++
Introduction
●
Developed by Bjarne Stroustrup at Bell Labs as an
extension to C (1979)
●
Adds many new features to the C language, and is
perhaps best thought of as a super-set of C
●
Initially it was was called as C with Classes
●
An Object Oriented Programming language
C++
Introduction - Applications
●
If you need high performance and precise control over
memory and other H/W resources, then C++ would be
nice choice
●
Few applications of C++ are

Video games

High-performance Financial applications

Graphical Applications and Simulations

Productivity / Office Tools

Embedded and Real Time Software

Audio and Video Processing
C++
Introduction – Vs C
●
Multi-paradigm programming language

Choose a specific single approach or mix parts of different
programming paradigms
●
Mostly preferred as Object oriented

But note C++ supports procedural programming, generic
programming too!
●
Power of standard library (STL)

A library of container classes, algorithms, and iterators
C++
Introduction – Type of Programming Languages
●
Assembly
●
Procedural

C, Fortran, COBOL etc.
●
Object Oriented

C++, Java, Python, C#, VB.NET etc.
●
Declarative

HTML, XML, CSS, SQL Haskell, ML, Prolog etc.
C++
Introduction – POP vs OOP
POP OOP
●
POP follows a top-down approach
●
Program is divided into small
chunks based on the functions.
●
Each function contains different
data.
●
Follows a systematic approach to
solve the problem.
●
No easy way for data hiding.
●
OOP takes a bottom-up approach
in designing a program.
●
Program is divided into objects
depending on the problem.
●
Each object controls its own data.
●
Focuses on security of the data
irrespective of the algorithm.
●
Data hiding is possible in OOP
/* My first C++ code */
#include <iostream>
int main()
{
// To display Hello world
std::cout << "Hello worldn";
return 0;
}
File Header
Preprocessor Directive
The start of program
Comment
Statement
Program Termination
C++
Anatomy of a Simple C++ Code
C++
Introduction – Compilation
●
Assuming your code is ready, use the following
commands to compile the code
●
On command prompt, type
●
$ g++ <file_name>.cpp
●
This will generate a executable named a.out
●
But it is recommended that you follow proper conversion
even while generating your code, so you could use
●
$ g++ <file_name>.cpp -o <file_name>
●
This will generate a executable named <file_name>
C++
Introduction – Execution
●
To execute your code you shall try
●
$ ./a.out
●
If you have named you output file as your <file_name>
then
●
$ ./<file_name>
●
This should the expected result on your system
Concepts of Object Oriented
Programming (OOP)
C++
OOP Concepts – Object
●
Something, which changes with time
●
Something, which is physical which can be seen,
touched, felt etc.
●
So it can be related with three properties

Identity

Attributes

Behavior
●
But note that, most of properties of the object generally
changes, but not its identity!, on the same sense behavior /
attribute will not affect other objects of same type.
C++
OOP Concepts – Object
●
So if we consider a Car as an example, then

Attributes : Color, Type

Behavior : Starts, Moves, Stops …

Identity : Ford, Audi
One cars behavior/attribute will not affect other cars. So
every car has its own identity.
C++
OOP Concepts – Object
●
Now in computing world, the objects are not physical or
even visible (some times) that’s it!, but every other
aspect remains the same as real time object
●
Some example can be like
– Date & Time: Can be seen and it changes
– Timer: A peripheral register who’s value changes,
using which we derive date and time, but generally
not seen by the end user
●
Well how do I create objects in C++??, please refer the
next slide
C++
OOP Concepts – Class
A set or category of things having some property or attribute in
common and differentiated from others by kind, type, or quality.
Source: google
Template definition of the methods and variables
in a particular kind of object
Source: google
C++
OOP Concepts – Class
●
A technique which helps to describe the object
completely from properties to its implementation
●
Acts as blue print, which helps us to create objects of
the same type!.
●
What do you understand from the image put in the next
slide?
C++
OOP Concepts – Class
C++
OOP Concepts – Class
●
What is to be understood here is the blueprint of bicycle
will always be same, like its going to have 2 tiers, a
seat, a handle etc.,
●
We may create different types of bicycles with a defined
class
C++
OOP Concepts – Class - Components
●
Identity – what is it known as?
– Employee, Bank Account, Event, Player, Document, Album
●
Attributes – what describes it?
– Width, Height, Color, Score, File Type, Length
●
Behavior – what can it do?
– Play, Open, Search, Save, Print, Create, Delete, Close
C++
OOP Concepts – Class - Components
Balance
Number
Holder name
Date opened
Deposit()
Withdraw()
Transfer()
10000
SBIN001122
Ajay
03/05/2000
Deposit()
Withdraw()
Transfer()
150
SBIN123456
John
25/11/2015
Deposit()
Withdraw()
Transfer()
Class
Object
Object
class ClassName
{
/* Group of data types */
/* Group of functions */
};
class Employee
{
int id;
string name;
string address;
void get_id(void)
{
cout << “Enter ID No: ”;
cin >> id;
}
void get_name(void)
{
cout << “Enter Name: ”;
cin >> name;
}
void get_address(void)
{
cout << “Enter Address: ”;
cin >> address;
}
};
C++
Class
Syntax Syntax
C++
Class – Access Modifiers
●
3 Types

Private

This is the default access behavior

Accessed only by the functions inside the class

Not allowed to be accessed directly by any object or
function outside the class

Public

Can be accessed by all members, even by other classes too

Protected

Similar to that of private access modifiers, the difference is
that the class member declared as Protected are
inaccessible outside the class but they can be accessed by
any subclass(derived class) of that class
C++
Class
#include <iostream>
using namespace std;
class Employee
{
private:
// Members
int id;
string name, address;
public:
// Methods
void get_data(void)
{
cout << "Enter ID No: "; cin >> id;
cout << "Enter Name: "; cin >> name;
cout << "Enter Address: "; cin >> address;
}
void print_data(void)
{
ccout << "The ID is: " << id << endl;
cout << "The Name is: " << name << endl;
cout << "The Address is: " << address << endl;
}
};
int main()
{
Employee emp1;
emp1.get_data();
emp1.print_data();
return 0;
}
001_example.cpp
C++
Class
#include <iostream>
using namespace std;
class Employee
{
private:
int id;
string name, address;
public:
void get_data(void)
{
cout << "Enter ID No: "; cin >> id;
cout << "Enter Name: "; cin >> name;
cout << "Enter Address: "; cin >> address;
}
void print_data(void)
{
ccout << "The ID is: " << id << endl;
cout << "The Name is: " << name << endl;
cout << "The Address is: " << address << endl;
}
};
int main()
{
Employee emp1;
// Private Members
// cannot be accessed
emp1.id = 10;
emp1.name = "Tingu";
return 0;
}
002_example.cpp
C++
Class
003_example.cpp
#include <iostream>
using namespace std;
class Employee
{
private:
int id;
public:
string name, address;
void get_data(void)
{
cout << "Enter ID No: "; cin >> id;
cout << "Enter Name: "; cin >> name;
cout << "Enter Address: "; cin >> address;
}
void print_data(void)
{
ccout << "The ID is: " << id << endl;
cout << "The Name is: " << name << endl;
cout << "The Address is: " << address << endl;
}
};
int main()
{
Employee emp1;
// Allowed
emp1.name = "Tingu";
return 0;
}
C++
Class vs Structure
004_example.cpp
#include <iostream>
using namespace std;
struct sEmployee
{
int id;
string name;
string address;
};
class cEmployee
{
int id;
string name;
string address;
};
int main()
{
sEmployee emp1;
cEmployee emp2;
// Allowed, Since public by
// default
emp1.name = "Tingu";
// Not allowed, Since private
// by default
emp2.name = "Pingu";
return 0;
}
C++
Class vs Structure
005_example.cpp
#include <iostream>
using namespace std;
struct sEmployee
{
int id;
private:
string name;
string address;
};
class cEmployee
{
int id;
public:
string name;
string address;
};
int main()
{
sEmployee emp1;
cEmployee emp2;
// Not allowed, Since declared
// private
emp1.name = "Tingu";
// Allowed, Since declared
// public
emp2.name = "Pingu";
return 0;
}
C++
Class vs Structure
006_example.cpp
#include <iostream>
using namespace std;
struct sEmployee
{
int id;
string name;
string address;
};
class cEmployee
{
int id;
string name;
string address;
};
int main()
{
sEmployee emp1;
cEmployee emp2;
cout << "sizeof emp1 is " << sizeof(emp1) << endl;
cout << "sizeof emp2 is " << sizeof(emp2) << endl;
return 0;
}
C++
Class – Constructor and Destructor
●
Constructors are special class functions which performs
initialization of every object.
●
The Compiler generates the code such a way that when
an object is created a Constructor is called.
●
Constructors initialize values to object members after
storage is allocated to the object.
●
Constructor has the same name as that of the class and
it does not have any return type. Also, the constructor is
always public.
C++
Class – Constructor and Destructor
●
Just like constructor, destructor is called to destroys the
object as soon as the scope of object ends.
●
The syntax for destructor is same as that for the
constructor, the class name is used for the name of
destructor, with a tilde ~ sign as prefix to it.
C++
Class – Constructor and Destructor
007_example.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Employee
{
public:
int id;
char *name;
Employee()
{
id = 0;
name = new char [10];
}
~Employee()
{
delete name;
}
};
int main()
{
Employee emp1;
cout << "The ID is " << emp1.id << endl;
strcpy(emp1.name, "Tingu");
cout << "The Name is " << emp1.name << endl;
return 0;
}
C++
Class – Constructor and Destructor
008_example.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Employee
{
public:
int id;
char *name;
Employee(int x, char *s);
~Employee(void);
};
Employee::Employee(int x, char *s)
{
id = x;
name = new char [10];
strcpy(name, s);
}
Employee::~Employee(void)
{
delete name;
}
int main()
{
Employee e(10, (char *) "Tingu");
cout << "The ID is " << e.id << endl;
cout << "The Name is " << e.name << endl;
return 0;
}
C++
Overloading
●
A feature which allows us to have two or more members
with the same name is called as Overloading
●
C++ has two types of overloading

Function

Operator
●
The declared members can be in same scope, except
that both declarations have different arguments and
different definition
●
When an overloaded function or a operator is called the
compiler looks for the right definition by comparing the
arguments, this is called as Overload Resolution
C++
Overloading
009_example.cpp
#include <iostream>
using namespace std;
// Function Overloading
int add(int n1, int n2)
{
return n1 + n2;
}
double add(double n1, double n2)
{
return n1 + n2;
}
string add(string s1, string s2)
{
// Operator Overloading
return s1 + s2;
}
int main()
{
cout << add(5, 10) << endl;
cout << add(3.5, 6.5) << endl;
cout << add("Hell", "o") << endl;
return 0;
}
C++
Overloading
●
Operator overloading helps us create the operator which
provides a special meaning to the user-defined data
type, its a compile-time polymorphism, Will discuss
about this later
●
Now, as we just discussed, even constructors are special
function which can be overloaded as required!.
C++
Class – Constructor and Destructor
010_example.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Employee
{
public:
int id;
char *name;
Employee(int id);
Employee(int id, char *s);
~Employee(void);
};
Employee::Employee(int i)
{
id = i;
name = new char [10];
strcpy(name, "None");
}
Employee::Employee(int i, char *s)
{
id = i;
name = new char [10];
strcpy(name, s);
}
Employee::~Employee(void)
{
delete name;
}
int main()
{
Employee e1(10), e2(11, (char *) "Tingu");
cout << "ID: " << e1.id << endl;
cout << "Name: " << e1.name << endl;
cout << "ID: " << e2.id << endl;
cout << "Name: " << e2.name << endl;
return 0;
}
Pillars of Object Oriented
Programming (OOP)
C++
Pillars of OOP
●
What make us to select the C++ as a choice is, its
features like

Abstraction

Polymorphism

Inheritance

Encapsulation
C++
Abstraction
●
Show only “relevant” data and “hide” unnecessary
details of an object from the user

Focus on the essentials

Ignore irrelevant and the unimportant
C++
Abstraction
011_example.cpp
#include <iostream>
#include <cstring>
using namespace std;
class Employee
{
private: // Abstracted the members
int id;
char *name;
public:
Employee(int id, char *s);
~Employee(void);
int get_id(void);
char *get_name(void);
};
int Employee::get_id(void)
{
return id;
}
char *Employee::get_name(void)
{
return name;
}
Employee::Employee(int i, char *s)
{
id = i;
name = new char [10];
strcpy(name, s);
}
Employee::~Employee(void)
{
delete name;
}
int main()
{
Employee e1(11, (char *) "Tingu");
// Cannot access the members directly
// This, we had seen in the initial
// examples, Please refer 002_example.cpp
cout << "ID: " << e1.get_id() << endl;
cout << "Name: " << e1.get_name() << endl;
return 0;
}
C++
Encapsulation
●
Binding the data with the code that manipulates it.
●
It keeps the data and the code safe from external
interference
C++
Encapsulation
012_example.cpp
#include <iostream>
using namespace std;
class Employee
{
int id; // This member can be accessed with only get and set function since its
// private, hence we can say its encapsulated
public:
int get_id(void) const { // Getter
return id;
}
void set_id(int id) { // Setter
this->id = id;
}
};
int main()
{
Employee e;
e.set_id(10);
cout << "The ID is " << e.get_id() << endl;
return 0;
}
C++
Inheritance
●
Inheritance is the
mechanism by which
an object acquires the
some / all properties
of another object.
●
It supports the
concept of
hierarchical
classification.
I am just like
him,
except I have
lentils on me :)
I have got
cheese and
meat from him
C++
Inheritance
●
Now if we take an
Organization as an example,
Say “Emertxe”, We have
different group of people
here!.
●
So if we have a Base Class
called EmertxeMembers,
Some members will be
distinct from other some ways
●
So the Mentors, Candidates
will have a Sub Class along
with the Base Class with their
specifics
Mentor Candidate
Emertxe
Admin
C++
Inheritance
●
So what identity,
attribute and
behaviors do people
have in common?
Mentor Candidate
Emertxe
Admin
●
Name
●
ID
●
Address
●
Display Profile
●
Change Profile
C++
Inheritance
●
Then, the base class
of Emertxe Members
would look like
013_example.h
#ifndef EXAMPLE_013_H
#define EXAMPLE_013_H
#include <iostream>
#include <cstring>
using namespace std;
class EmertxeMember
{
protected:
int id;
string name;
string address;
public:
EmertxeMember(int id, string n, string a)
{
this->id = id;
name = n;
address = a;
}
void display_profile(void);
};
C++
Inheritance
●
Let see some things
specific to Candidate
apart from the main
attributes and
behaviors
Candidate
●
Name
●
ID
●
Address
●
Display Profile
●
Change Profile
Inherit Base Class
●
Batch ID
●
Course Taken
●
Current Module
●
Grade
●
Change Course
●
Add a Class Taken
Sub ClassExtension
C++
Inheritance
●
The extended candidate class would look like this
013_example.h
class Candidate : public EmertxeMember
{
// Note have not considered all cases said in the previous slide
string course;
int year;
public:
Candidate(int id, string n, string a, string course, int year);
void display_profile(void);
};
C++
Inheritance
●
On the same line the
Mentor may have
some extra features as
shown
●
Name
●
ID
●
Address
●
Display Profile
●
Change Profile
Inherit Base Class
●
Subject Taken
●
Rank
●
Change Rank
●
Add a Subject
Sub ClassExtension
Mentor
C++
Inheritance
●
The extended mentor class would look like this
013_example.h
class Mentor : public EmertxeMember
{
// Note have not considered all cases said in the previous slide
string sub_taught;
string rank;
public:
Mentor(int id, string n, string a, string sub_taught, string year);
void display_profile(void);
};
#endif
C++
Inheritance
●
The definition of all the methods can be put as
013_example.cpp
#include "013_example.h"
Mentor::Mentor(int id, string n, string a, string sub_taught, string rank)
:EmertxeMember(id, n, a)
{
this->sub_taught = sub_taught;
this->rank = rank;
}
Candidate::Candidate(int id, string n, string a, string course, int year)
:EmertxeMember(id, n, a)
{
this->course = course;
this->year = year;
}
C++
Inheritance
013_example.cpp
void EmertxeMember::display_profile(void)
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
}
void Mentor::display_profile(void) // Override the base class definition
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Subject Taught: " << sub_taught << endl;
cout << "Rank: " << rank << endl;
}
void Candidate::display_profile(void) // Override the base class definition
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Course: " << course << endl;
cout << "Year: " << year << endl;
}
C++
Inheritance
014_example.cpp
#include "013_example.h"
// Please compile along with 013_example.cpp
int main()
{
EmertxeMember m1(100, "Ringu", "Mangalore");
Mentor m2(108, "Tingu", "Mysore", "Linux Systems", "Senior");
Candidate c1(120, "Pingu", "Bangalore", "ECEP", 2019);
cout << "m1:--->n"; m1.display_profile();
cout << "m2:--->n"; m2.display_profile();
cout << "c1:--->n"; c1.display_profile();
return 0;
}
C++
Polymorphism
The condition of occurring in several different forms
Source: google
The occurrence of different forms among the members of a
population or colony, or in the life cycle of an individual organism
Source: google
A feature of a programming language that allows routines
to use variables of different types at different times
Source: google
Biology
Computing
C++
Polymorphism
●
Polymorphism means to process objects differently
based on their data type
●
One method with multiple implementation, for a certain
class of action. And which implementation to be used is
decided at runtime depending upon the situation (i.e.,
data type of the object)
●
This can be implemented by designing a generic
interface, which provides generic methods for a certain
class of action and there can be multiple classes, which
provides the implementation of these generic methods.
C++
Polymorphism
●
So ability of one existing type, say X to appear as and be
used like another type Y e.g.

Candidate object can be used in place of an
EmertxeMember object
015_example.cpp
#include "013_example.h"
// Please compile along with 013_example.cpp
int main()
{
EmertxeMember *m1 = new EmertxeMember(200, "Tingu", "Mysore");
EmertxeMember *m2 = new Candidate(300, "Pingu", "Bangalore", "ECEP", 2019);
cout << "m1:--->n"; m1->display_profile();
cout << "m2:--->n"; m2->display_profile();
return 0;
}
●
Did you observe the types used above?!!
C++
Polymorphism
●
In the previous example object m1 point to the declared
type which is the actual type!
●
But in case of m2 the declared type is EmertxeMember
and it points to Candidate, which is a actual type!
●
So every object has a type declared at compilation time,
but at run time it may point to the actual type
C++
Polymorphism
●
So if we execute the previous code, the result is as
follows
user@user:~] g++ 014_example.cpp 013_example.cpp
user@user:~] ./a.out
m1:-->
ID: 200
Name: Tingu
Address: Mysore
m2:-->
ID: 300
Name: Pingu
Address: Bangalore
user@user:~]
Note that, the
course name and year
is missing in the output!!
Why??
●
This is not complete even though we called the override
function!!
●
Here comes the need of virtual function concepts
C++
Virtual Functions
●
Member function that you expect to be redefined (i.e is
overridden) in derived classes
●
So if we refer to a derived class object using a pointer or
a reference to the base class, you can call a virtual
function for that object and execute the derived class's
version of the function
●
A base class function call is fixed before the program is
executed. This is called as early binding or static
linkage because the these functions is set during the
compilation of the program.
C++
Virtual Functions
●
Defining in a base class a virtual function, with another
version in a derived class, signals to the compiler that
we don't want static linkage for this function. This sort
of operation is referred to as dynamic linkage, or late
binding.
●
This concepts are mainly used to achieve Runtime
Polymorphism
C++
Virtual Functions
●
Now lets modify
013_example.h as
shown here, and
understand the
Runtime Polymorphism
016_example.h
#ifndef EXAMPLE_016_H
#define EXAMPLE_016_H
#include <iostream>
#include <cstring>
using namespace std;
class EmertxeMember
{
protected:
int id;
string name;
string address;
public:
EmertxeMember(int id, string n, string a)
{
this->id = id;
name = n;
address = a;
}
virtual void display_profile(void);
};
C++
Virtual Functions
016_example.h
class Candidate : public EmertxeMember
{
// Note have not considered all cases said in the previous slide
int course;
int year;
public:
Candidate(int id, string n, string a, int course, int year);
void display_profile(void);
};
class Mentor : public EmertxeMember
{
// Note have not considered all cases said in the previous slide
string sub_taught;
string rank;
public:
Mentor(int id, string n, string a, string sub_taught, string year);
void display_profile(void);
};
#endif
C++
Virtual Functions
016_example.cpp
#include "016_example.h"
Mentor::Mentor(int id, string n, string a, string sub_taught, string year)
:EmertxeMember(id, n, a)
{
this->sub_taught = sub_taught;
this->rank = rank;
}
Candidate::Candidate(int id, string n, string a, int course, int year)
:EmertxeMember(id, n, a)
{
this->course = course;
this->year = year;
}
C++
Virtual Functions
016_example.cpp
void EmertxeMember::display_profile(void)
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
}
void Mentor::display_profile(void) // Override the base class definition
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Subject Taught: " << sub_taught << endl;
cout << "Rank: " << rank << endl;
}
void Mentor::display_profile(void) // Override the base class definition
{
cout << "ID: " << id << endl;
cout << "Name: " << name << endl;
cout << "Address: " << address << endl;
cout << "Course: " << course << endl;
cout << "Year: " << year << endl;
}
C++
Virtual Functions
017_example.cpp
#include "016_example.h"
// Please compile along with 016_example.cpp
int main()
{
EmertxeMember *m1 = new EmertxeMember(200, "Tingu", "Mysore");
EmertxeMember *m2 = new Candidate(300, "Pingu", "Bangalore", "ECEP", 2019);
cout << "m1:--->n"; m1->display_profile();
cout << "m2:--->n"; m2->display_profile();
return 0;
}
●
You may observe the output of the code now
C++
Virtual Functions
●
What is happening behind the scene?
– A virtual table (V-Table) is created
●
Stores pointers to all virtual functions
●
Created per each class
●
Lookup during the function call
C++
Virtual Functions
●
What about constructors?

To create an object, you must know its exact type. The
VPTR has not even been initialized at this point, so the
answers in NO
●
Then what about destructors?

Yes, we must always clean up the mess created in the
subclass (else, we risk memory leaks!)
C++
Virtual Functions - Pure
●
If there is no meaningful definition you could give for the
function in the base class. But still you want to include a
virtual function in a base class so that it may be
redefined in a derived class to suit the objects of that
class Example
class EmertxeMember
{
protected:
int id;
string name;
string address;
public:
EmertxeMember(int id, string n, string a)
{
// ...
}
virtual void display_profile(void);
// Pure Virtual Function
virtual void change_profile(void) = 0;
};
●
We may declare it as
following
C++
Abstract Class
●
A class is abstract if it has at least one pure virtual
function
●
Sometimes you want to inherit only declarations, not
definitions
●
A method without an implementation is called an
abstract method
●
Often used to create an interface
●
We can have pointers and references of abstract class
type
●
If we do not override the pure virtual function in derived
class, then derived class also becomes abstract class
C++
Abstract Class
018_example.cpp
#include <iostream>
using namespace std;
class Polygon
{
protected:
int width, height;
string shape_name;
public:
Polygon() { }
Polygon(int a, int b, string name) : width(a), height(b), shape_name(name) { }
string get_name(void) {
return shape_name;
}
// A pure virtual functions
virtual int get_area(void) = 0;
void print_area(void) {
cout << "Area of " << this->get_name() << " is "
<< this->get_area() << endl;
}
};
C++
Abstract Class
018_example.cpp
class Rectangle: public Polygon
{
public:
Rectangle(int a, int b, string name) : Polygon(a, b, name) { }
int get_area(void)
{
return width * height;
}
};
class Triangle: public Polygon
{
public:
Triangle(int a, int b, string name) : Polygon(a, b, name) { }
int get_area(void)
{
return width * height / 2;
}
};
C++
Abstract Class
018_example.cpp
int main()
{
Rectangle rect (4, 5, "Rectangle");
Triangle trgl (4, 5, "Triangle");
Polygon *shapes[] = {&rect, &trgl};
for (int i = 0; i < 2; i++)
{
shapes[i]->print_area();
}
return 0;
}
C++
Abstract Class
●
Is a constructor needed? Because, the class will never be
instantiated!

Yes, The subclass will inherit it, So a constructor will be
used initialize its members
●
What about destructor? Because, the class will never be
created

Yes, Always define a virtual destructor in the base class,
to make sure that the destructor of its subclass is called!
Properties of C++
C++
Namespace
●
An abstract space that contains a set of names
●
Is a declarative region that provides a scope to the
identifiers (names of the types, function, variables etc)
within it
●
Useful for resolving naming conflicts
019_example.cpp
#include <iostream>
using namespace std;
int main()
{
int x = 10;
cout << x << endl;
double x = 15.5; // Not allowed to have the same name in a local space!
cout << x << endl;
return 0;
}
C++
Namespace
020_example.cpp
#include <iostream>
using namespace std;
int x = 10;
int main()
{
double x = 10.5;
cout << x << endl; // How to get the global x refernce here
return 0;
}
C++
Namespace
021_example.cpp
#include <iostream>
using namespace std;
namespace global
{
int x = 10;
}
int main()
{
double x = 10.5;
cout << global::x << endl;
return 0;
}
C++
Namespace
022_example.cpp
#include <iostream>
using namespace std;
namespace first
{
int x = 10;
}
namespace second
{
double x = 12.120;
}
int main()
{
double x = 10.5;
cout << x << endl;
cout << first::x << endl;
cout << second::x << endl;
return 0;
}
023_example.cpp
#include <iostream>
using namespace std;
namespace first
{
int x = 10;
}
namespace second
{
double x = 12.120;
}
int main()
{
using namespace second;
cout << x << endl;
return 0;
}
C++
Namespace
024_example.cpp
#include <iostream>
using namespace std;
namespace MySpace
{
class Employee
{
public:
int id;
string name;
};
}
class Employee
{
public:
int id;
string name;
};
int main()
{
Employee emp1;
MySpace::Employee emp2;
emp1.name = "Tingu";
emp2.name = "Pingu";
cout << emp1.name << endl;
cout << emp2.name << endl;
return 0;
}
C++
Templates
●
Templates are powerful features of C++ which allows you
to write generic programs
●
Templates are often used in larger code-base for the
purpose of code re-usability and flexibility of the
programs
●
The concept of templates can be used in two different
ways

Function Templates

Class Templates
C++
Templates
●
Templates are powerful features of C++ which allows you
to write generic programs
●
Templates are often used in larger code-base for the
purpose of code re-usability and flexibility of the
programs
●
The concept of templates can be used in two different
ways

Function Templates

Class Templates
C++
Templates
025_example.cpp
#include <iostream>
using namespace std;
template <typename T>
T Max (T a, T b)
{
return b < a ? a : b;
}
int main()
{
cout << Max(10, 20) << endl;
cout << Max(33.5, 20.2) << endl;
cout << Max(3.5, 10.2) << endl;
cout << Max('A', 'B') << endl;
cout << Max('Z', 'Y') << endl;
return 0;
}
Thank You