Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Object-Oriented
Programming in C++
Lecture Slides By Adil Aslam
My Email Address:
adilaslam5959@gmail.com
Object Oriented ...
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
About Me
i am Student of BSCS
My email address:
adilaslam5...
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Inheritance in C++
Outer Class
Inheritance in C++
• Inherit Definition - Derive quality and
characteristics from parents or ancestors. Like you
inherit f...
Inheritance in C++
• Inheritance is the capability of one class to acquire
properties and characteristics from another cla...
Advantage of inheritance
• If we develop any application using this concept
than that application have following advantage...
Real life Example of Inheritance
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Diagram
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
In the above diagram data members and methods are ...
Basic Syntax of Inheritance
• While defining a subclass like this, the super class must be
already defined or at least dec...
Inheritance in C++ Program Example
class Rectangle
{
... .. ...
};
class Area : public Rectangle
{
... .. ...
};
class Per...
Inheritance in C++ Program Example
class Rectangle
{
... .. ...
};
class Area : public Rectangle
{
... .. ...
};
class Per...
Inheritance in C++ Program Example
class Rectangle
{
... .. ...
};
class Area : public Rectangle
{
... .. ...
};
class Per...
Example of Inheritance
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Class Dog inherits
the properties
f...
Example of Inheritance
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
public:
int tail = 1;
};
int ma...
Example of Inheritance
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
public:
int tail = 1;
};
int ma...
Example of Inheritance
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
public:
int tail = 1;
};
int ma...
Example of Inheritance
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
public:
int tail = 1;
};
int ma...
Example of Inheritance
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
public:
int tail = 1;
};
int ma...
Example of Inheritance
class Animal {
public:
int legs = 4;
};
class Dog : public Animal {
public:
int tail = 1;
};
int ma...
Inheritance Visibility Mode
• Depending on Access modifier used while
inheritance, the availability of class members of
Su...
Inheritance Visibility Mode
• 2) Private Inheritance
• In private mode, the protected and public members of
super class be...
Table showing all the Visibility Modes
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Derived
Class
Deriv...
Access Control and Inheritance
• A derived class can access all the non-private
members of its base class. Thus base-class...
Inheritance Visibility Mode
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Member Access Specifier How Me...
Inheritance Visibility Mode
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Member Access Specifier How Me...
Inheritance Visibility Mode
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Member Access Specifier How Me...
Access Control and Inheritance
• A derived class inherits all base class methods with
the following exceptions:
• Construc...
class Base {
public: int m_public; // can be accessed by anybody
private: int m_private; // can only be accessed by Base m...
Types of Inheritance
• Based on number of ways inheriting the feature of
base class into derived class it have five types ...
Types of Inheritance
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Types of Inheritance
• Single Inheritance
• In single inheritance there exists single base class
and single derived class....
Single Inheritance
• In single inheritance there exists single base class
and single derived class.
• It is the most simpl...
Single Inheritance Example
• When a single class is derived from a single parent
class, it is called Single inheritance. I...
Syntax of Inheritance in C++
cclass base_classname
{
properties...
methods...
};
class derived_classname : visibility_mode...
Single Inheritance Example
• For example: Car, Bicycle, Motorcycle are all vehicles and
have many similar properties like ...
Single Inheritance Example
class A
{
Public:
int salary;
};
class B: public A
{
};
Object Oriented Programming in C++
Lect...
Single Inheritance Example
class A
{
Public:
int salary;
};
class B: public A
{
};
Object Oriented Programming in C++
Lect...
Single Inheritance Example
class A
{
Public:
int salary;
};
class B: public A
{
};
Object Oriented Programming in C++
Lect...
Single Inheritance Example
class A
{
Public:
int salary;
};
class B: public A
{
};
Object Oriented Programming in C++
Lect...
Example of Single Inheritance-1
class Person
{
public:
string name;
int age;
};
class Student : public Person
{
public:
vo...
Example of Single Inheritance-1
class Person
{
public:
string name;
int age;
};
class Student : public Person
{
public:
vo...
Example of Single Inheritance-1
class Person
{
public:
string name;
int age;
};
class Student : public Person
{
public:
vo...
Example of Single Inheritance-2
int main()
{
//Below Creating an object of Child Class(Student)
Student s;
s.name="Adil As...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Another Example of Single Inheritance
class Employee {
public: int salary;
};
class Developer : public Employee {
public:
...
Another Example of Single Inheritance
class Employee {
public: int salary;
};
class Developer : public Employee {
public:
...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Same Previous Program with some Modifications-1
class Employee //Parent Class
{
public:
int salary;
};
class Developer : p...
Same Previous Program with some Modifications-1
int main()
{
//Creating an object of Developer Class(Child Class)
Develope...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Ambiguity in Single Inheritance in C++
• If parent and child classes have same named
method, parent name and scope resolut...
Ambiguity in Single Inheritance in C++
• If parent and child classes have same named
method, parent name and scope resolut...
Ambiguity in Single Inheritance in C++
• If parent and child classes have same named
method, parent name and scope resolut...
Ambiguity in Single Inheritance in C++
• If parent and child classes have same named
method, parent name and scope resolut...
Example of Single Inheritance-1
class staff {
private:
char name[50];
int code;
public:
void getdata();
void display();
};...
Example of Single Inheritance-2
void staff::getdata() {
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}
void staff:...
Example of Single Inheritance-3
int main()
{
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata(); //calling staff class...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
One More Example of Single Inheritance-1
class person { //Parent class
private:
char fname[100],lname[100],gender[10];
pro...
One More Example of Single Inheritance-2
void person::input_person() {
cout<<"First Name: ";
cin>>fname;
cout<<"Last Name:...
One More Example of Single Inheritance-3
void student::input_student() {
person::input_person();
cout<<"College: ";
fflush...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Explanation of Previous Program
• In the previous example, we have a class person with
attributes fname (first name), lnam...
Types of Inheritance
• Multiple Inheritance
• In this type of inheritance a single derived class
may inherit from two or m...
Types of Inheritance
• Multiple Inheritance
• In this type of inheritance a single derived class
may inherit from two or m...
Multiple Inheritance Example
• When a class is derived from two or more base
classes, such inheritance is called Multiple
...
Syntax of Multiple Inheritance
class base_class1
{
properties;
methods;
};
class base_class2
{
properties;
methods;
};
cla...
Multiple Inheritance Example
class A
{
};
class B
{
};
class C: public B, public A
{
};
Object Oriented Programming in C++...
Multiple Inheritance Example
class A
{
};
class B
{
};
class C: public B, public A
{
};
Object Oriented Programming in C++...
Multiple Inheritance Example
class A
{
public: string name;
};
class B
{
public: int marks;
};
class C: public B, public A...
Example of Multiple Inheritance
class A {
public: string name;
};
class B {
public: int marks;
};
class C: public B, publi...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Ambiguity in Multiple Inheritance
• In multiple inheritance, a single class is derived
from two or more parent classes. So...
Ambiguity in Multiple Inheritance
class A {
public: void display() {
cout <<"This is method of A";
}
};
class B{
public: v...
Ambiguity in Multiple Inheritance
class A {
public: void display() {
cout <<"This is method of A";
}
};
class B{
public: v...
Ambiguity in Multiple Inheritance
class A {
public: void display() {
cout <<"This is method of A";
}
};
class B{
public: v...
Compiler View
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Compiler
Error
Ambiguity Resolution of Multiple Inheritance
• This problem can be resolved by class name and
using scope resolution opera...
Solution of Ambiguity in Multiple Inheritance
class A {
public: void display() {
cout <<"This is method of A";
}
};
class ...
Solution of Ambiguity in Multiple Inheritance
class A {
public: void display() {
cout <<"This is method of A";
}
};
class ...
Solution of Ambiguity in Multiple Inheritance-1
class A {
public:
void display()
{
cout <<"This is method of An";
}
};
cla...
Solution of Ambiguity in Multiple Inheritance-2
class C: public A, public B {
public:
void output()
{
A::display();
B::dis...
Solution of Ambiguity in Multiple Inheritance-2
class C: public A, public B {
public:
void output()
{
A::display();
B::dis...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Another Example of Multiple Inheritance
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
One More Example of Multiple Inheritance-1
class student {
protected: int rno,m1,m2;
public:
void get() {
cout<<"Enter the...
One More Example of Multiple Inheritance-2
class statement:public student,public sports {
int tot,avg;
public:
void displa...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Example of Multiple Inheritance-1
class liquid {
float specific_gravity;
public: void input() {
cout<<"Specific gravity: "...
Example of Multiple Inheritance-2
class petrol: public liquid, public fuel {
public: void input() {
liquid::input();
fuel:...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Types of Inheritance
• Hierarchical Inheritance
• In this type of inheritance, multiple derived classes
inherits from a si...
Hierarchical Inheritance Example
• When more than one classes are derived from a
single base class, such inheritance is kn...
Syntax of Hierarchical Inheritance
class base_classname {
properties;
methods;
};
class derived_class1:visibility_mode bas...
Hierarchical Inheritance Example
class A
{
};
class B: public A
{
};
class C: public A
{
};
class D: public A
{
};
Object ...
Hierarchical Inheritance Example
class A
{
};
class B: public A
{
};
class C: public A
{
};
class D: public A
{
};
Object ...
Example of Hierarchical Inheritance-1
class Side {
protected: int l;
public: void set_values (int x) {
l=x;
}
};
class Squ...
Example of Hierarchical Inheritance-1
class Side {
protected: int l;
public: void set_values (int x) {
l=x;
}
};
class Squ...
Example of Hierarchical Inheritance-1
class Side {
protected: int l;
public: void set_values (int x) {
l=x;
}
};
class Squ...
Example of Hierarchical Inheritance-1
class Side {
protected: int l;
public: void set_values (int x) {
l=x;
}
};
class Squ...
Example of Hierarchical Inheritance-2
int main () {
//Creating an Object of Class Square
Square s;
s.set_values (10);
cout...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
One More Example of Hierarchical Inheritance-1
class Shape {
protected: float width, height;
public:
void set_data (float ...
One More Example of Hierarchical Inheritance-1
int main ()
{
//Creating an Object of Class Rectangle
Rectangle rect;
//Cre...
Explanation of the Previous Program
• The object of the class Rectangle contains :
width, height inherited from Shape beco...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Example of Hierarchical Inheritance-1
class person {
char name[100],gender[10]; int age;
public:
void getdata() {
cout<<"N...
Example of Hierarchical Inheritance-2
class student: public person{
char institute[100], level[20];
public:
void getdata()...
Example of Hierarchical Inheritance-3
class employee: public person {
char company[100]; float salary;
public:
void getdat...
Example of Hierarchical Inheritance-4
int main()
{
student s;
employee e;
cout<<"Student"<<endl;
cout<<"Enter data"<<endl;...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Hierarchical Inheritance Example
class Number
{
private:
int num;
public:
void getNumber() {
cout << "Enter an integer num...
Hierarchical Inheritance Example Cont.
//Base Class 1, to calculate square of a number
class Square : public Number
{
publ...
Hierarchical Inheritance Example Cont.
//Base Class 2, to calculate cube of a number
class Cube : public Number
{
private:...
Hierarchical Inheritance Example Cont.
int main() {
Square objS;
Cube objC;
int sqr, cube;
objS.getNumber();
sqr =objS.get...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Types of Inheritance
• Multilevel Inheritance
• In this type of inheritance the derived class inherits
from a class, which...
Types of Inheritance
• Multilevel Inheritance
• In this type of inheritance the derived class inherits
from a class, which...
Multilevel Inheritance
• When a class is derived from a class which is also
derived from another class, i.e. a class havin...
Syntax of Multilevel Inheritance
class base_classname
{
properties;
methods;
};
class intermediate_classname:visibility_mo...
Multilevel Inheritance
class A
{
};
class B: public A
{
};
class C: public B
{
};
Object Oriented Programming in C++
Lectu...
Multilevel Inheritance
class A
{
};
class B: public A
{
};
class C: public B
{
};
Object Oriented Programming in C++
Lectu...
Example of Multilevel Inheritance
class A {
public: void display() {
cout<<"Base Class Content";
}
};
class B : public A {...
Example of Multilevel Inheritance
class A {
public: void display() {
cout<<"Base Class Content";
}
};
class B : public A {...
Example of Multilevel Inheritance
class A {
public: void display() {
cout<<"Base Class Content";
}
};
class B : public A {...
Example of Multilevel Inheritance
class A {
public: void display() {
cout<<"Base Class Content";
}
};
class B : public A {...
Example of Multilevel Inheritance
class A {
public: void display() {
cout<<"Base Class Content";
}
};
class B : public A {...
Explanation of Previous Program
• In this program, class B is derived from A and C is
derived from B. An object of class C...
One More Example of Multilevel Inheritance-1
class person {
char name[100],gender[10]; int age;
public:
void getdata() {
c...
One More Example of Multilevel Inheritance-2
class employee: public person {
char company[100];
float salary;
public:
void...
One More Example of Multilevel Inheritance-3
class programmer: public employee
{
int number;
public:
void getdata()
{
empl...
One More Example of Multilevel Inheritance-4
int main()
{
programmer p;
cout<<"Enter data"<<endl;
p.getdata();
cout<<endl<...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
One More Example of Multilevel Inheritance-1
class grandfather {
int age;
char name [20];
public:
void get() {
cout << "En...
One More Example of Multilevel Inheritance-2
class father : public grandfather {
int age;
char name [20];
public:
void get...
One More Example of Multilevel Inheritance-3
class son : public father {
int age; char name [20];
public: void get() {
gra...
One More Example of Multilevel Inheritance-4
int main ()
{
//Creating an object of Class Son
son s;
s.get();
s.show();
}
O...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Types of Inheritance
• Hybrid Inheritance
• Hybrid Inheritance is a method where one or more types of
inheritance are comb...
Types of Inheritance
• Hybrid Inheritance
• Hybrid Inheritance is a method where one or more types of
inheritance are comb...
Hybrid Inheritance Example
class A
{
};
class B: public A
{
};
class C: public A
{
};
class D: public B, public C
{
};
Obj...
Hybrid Inheritance Example
class A
{
};
class B: public A
{
};
class C: public A
{
};
class D: public B, public C
{
};
Obj...
Example of Hybrid Inheritance-1
class student {//base class derivation
protected:
int r_no;
public:
void getRollno()
{
cou...
Example of Hybrid Inheritance-2
class test : public student {//intermediate base class
protected:
int part1, part2;
public...
Example of Hybrid Inheritance-3
class sports
{
protected:
int score;
public:
void getSportsMarks()
{
cout << "Enter the ma...
Example of Hybrid Inheritance-4
//derived from test and sports
class result : public test, public sports
{
int total;
publ...
Example of Hybrid Inheritance-5
int main ()
{
//Creating an object of class result
result s1;
s1.getRollno();
s1.getMarks(...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Private inheritance
• In private inheritance, following are the key
features:
• Each public member in the base class is pr...
The following table shows the accesses specifier of the data
member of the base class baseA in the derived class deriveD.
...
Example of Private Inheritance-1
class student {
private:
int roll;
protected:
char sex;
public:
void getroll();
void gets...
Example of Private Inheritance-2
void student::getroll() {
cout<<"Enter Roll:";
cin>>roll;
}
void student::getsex() {
cout...
Example of Private Inheritance-3
void result::display_marks() {
cout<<"nSex:"<<sex;
cout<<"nMarks:"<<marks;
}
int main() {...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Order of Constructor Call
• Base class constructors are always called in the
derived class constructors. Whenever you crea...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Base class Default Constructor in Derived class Constructors
class Base {
int x;
public:
Base() { cout << "Base default co...
Constructor, Destructor and Inheritance
• The constructors are used to initialize member variables of
the object, and the ...
Constructor, Destructor and Inheritance
• In heritance, destructors are executed in reverse
order of the constructor execu...
Constructor, Destructor and Inheritance-1
class A { //Base Class
public:
A() {
cout<<"n Default Constructor of Base Class ...
Constructor, Destructor and Inheritance-2
class C: public A, public B //Derivation of Class
{
public:
C() {
cout<<"n Defau...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Base class Parameterized Constructor in Derived class
Constructor-1
class Base
{
public :
int x;
public:
Base(int i)
{
x =...
Base class Parameterized Constructor in Derived class
Constructor-2
class Derived : public Base
{
public :
int y;
public:
...
Base class Parameterized Constructor in Derived class
Constructor-3
int main()
{
Derived d(10) ;
cout << d.x<<endl ; // Ou...
Why is Base class Constructor called inside Derived class ?
• Constructors have a special job of initializing the
object p...
Constructor call in Multiple Inheritance
• Its almost the same, all the Base class's
constructors are called inside derive...
Base class with Constructor and Derived class without Constructor
class A { //Base Class
public:
A()
{
cout<<"I am Base Cl...
Base class with Constructor and Derived class without Constructor
class A { //Base Class
public:
A()
{
cout<<"I am Base Cl...
Base class without Constructor and Derived class with Constructor
class A { //Base Class
public:
};
class B: public A { //...
Base class without Constructor and Derived class with Constructor
class A { //Base Class
public:
};
class B: public A { //...
Base class and Derived class with Constructor
class A { //Base Class
public:
A() {
cout<<"I am Base Class Constructor";
}
...
Base class and Derived class with Constructor
class A { //Base Class
public:
A() {
cout<<"I am Base Class Constructor";
}
...
Overriding Base Class Functions
• A derived class can override a member function of
its base class by defining a derived c...
Overriding Base Class Functions
class mother {
public:
void display () {
cout << "mother: display functionn"; }
};
class d...
Overriding Base Class Functions
class mother {
public:
void display () {
cout << "mother: display functionn"; }
};
class d...
Overriding Base Class Functions
class mother {
public:
void display () {
cout << "mother: display functionn"; }
};
class d...
Overriding Base Class Functions
class mother {
public:
void display () {
cout << "mother: display functionn"; }
};
class d...
Overriding Base Class Functions
class mother {
public:
void display () {
cout << "mother: display functionn"; }
};
class d...
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Gaining Access to an Overridden Function
• It is occasionally useful to be able to call the
overridden version. This is do...
Gaining Access to an Overridden Function-1
class mother {
public:
void display () {
cout << "mother: display functionn";
}...
Gaining Access to an Overridden Function-1
class mother {
public:
void display () {
cout << "mother: display functionn";
}...
Gaining Access to an Overridden Function-2
int main ()
{
daughter d;
d.display();
return 0;
}
Object Oriented Programming ...
Function Overriding
• In C++ programming language, Functions overriding is a
process of declaring the functions of same na...
Advantages of Inheritance
• Reduce code redundancy.
• Provides code reusability.
• Reduces source code size and improves c...
Disadvantages of Inheritance
• In Inheritance base class and child classes are
tightly coupled. Hence If you change the co...
Functions that are Never Inherited
• Constructors and Destructors are never inherited
and hence never overrided.
• Also, a...
Inheritance and Static Functions
• They are inherited into the derived class.
• If you redefine a static member function i...
Hybrid Inheritance and Virtual Class
• What is a virtual base class?
• An ambiguity can arise when several paths exist to
...
Hybrid Inheritance and Virtual Class
• In Multiple Inheritance, the derived class inherits from more
than one base class. ...
Hybrid Inheritance and Virtual Class
class A
{
void show();
};
class B:public A {};
class C:public A {};
class D:public B,...
Hybrid Inheritance and Virtual Class
class A
{
void show();
};
class B:public A {};
class C:public A {};
class D:public B,...
Hybrid Inheritance and Virtual Class
class A
{
void show();
};
class B:public A {};
class C:public A {};
class D:public B,...
Hybrid Inheritance and Virtual Class
class A
{
void show();
};
class B:public A {};
class C:public A {};
class D:public B,...
Hybrid Inheritance and Virtual Class
class A
{
void show();
};
class B:public A {};
class C:public A {};
class D:public B,...
Hybrid Inheritance and Virtual Class
class A
{
void show();
};
class B:public A {};
class C:public A {};
class D:public B,...
Hybrid Inheritance and Virtual Class
• Now by adding virtual keyword, we tell compiler to
call any one out of the two show...
Hybrid Inheritance and Virtual Class
• Now by adding virtual keyword, we tell compiler to
call any one out of the two show...
Syntax use of Virtual Keyword
class A {
.....
.....
};
class B : virtual public A {
.....
.....
};
class C : virtual publi...
Virtual Class
• What is Virtual base class? Explain its uses.
• When two or more objects are derived from a
common base cl...
Virtual base class Example-1
class A {
public:
int i; //Member Variable of Class A
};
class B : virtual public A {
public:...
Virtual base class Example-1
class A {
public:
int i; //Member Variable of Class A
};
class B : virtual public A {
public:...
Virtual base class Example-1
class A {
public:
int i; //Member Variable of Class A
};
class B : virtual public A {
public:...
Virtual base class Example-2
int main()
{
D ob;
ob.i = 10;
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << "Val...
Output of the Previous Program is :
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Hybrid Inheritance and Constructor call
• Consider this Example
class A {};
class B : virtual public A {};
class C : virtu...
Hybrid Inheritance and Constructor call
• As we all know that whenever a derived class
object is instantiated, the base cl...
Hybrid Inheritance and Constructor call
• This will result in multiple calls to the constructor of
class A, which is undes...
C++ friend Function and friend Classes
• One of the important concept of OOP is data
hiding, i.e., a nonmember function ca...
C++ friend Function and friend Classes
• friend Function in C++
• If a function is defined as a friend function then,
the ...
C++ friend Function and friend Classes
• Why use friend function ?
• You do not access private or protected data
member of...
C++ friend Function and friend Classes
• Why use friend function ?
• You do not access private or protected data
member of...
C++ friend Function and friend Classes
• Why use friend function ?
• You do not access private or protected data
member of...
C++ friend Function and friend Classes
• Characteristics of friend functions
• A friend function is not in the scope of th...
friend Function Example
class Distance {
private:
int meter;
public:
friend int func(Distance);
};
int func(Distance d) {
...
friend Function Example
class Distance {
private:
int meter;
public:
friend int func(Distance);
};
int func(Distance d) {
...
friend Function Example
class Distance {
private:
int meter;
public:
friend int func(Distance);
};
int func(Distance d) {
...
friend Function Example
class Distance {
private:
int meter;
public:
friend int func(Distance);
};
int func(Distance d) {
...
friend Function Example
class Distance {
private:
int meter;
public:
friend int func(Distance);
};
int func(Distance d) {
...
friend Function Example
• In previous Program, friend function func() is declared
inside Distance class. So, the private d...
Example to Operate on Objects of two Different
class using friend Function-1
class B; // forward declaration
class A {
pri...
Example to Operate on Objects of two Different
class using friend Function-2
int func(A d1,B d2)
/*Function func() is the ...
Explanation of Previous Program
• In this previous program, classes A and B has declared
func() as a friend function. Thus...
Another Example friend Function
class employee {
private:
friend void sal();
};
void sal()
{
int salary=400000;
cout<<"Sal...
Friend functions have the following Properties
• Friend of the class can be member of some other class.
• Friend of one cl...
friend Classes in C++
• Similarly like, friend function. A class can be made a
friend of another class using keyword frien...
friend Classes in C++
• When a class is made a friend class, all the member
functions of that class becomes friend functio...
friend Class Example
class A {
private:
string a;
public:
A() {
a="Adil";
}
friend class B;
};
class B {
private:
int b;
p...
friend Class Example
class A {
private:
string a;
public:
A() {
a="Adil";
}
friend class B;
};
class B {
private:
int b;
p...
friend Class Example
class A {
private:
string a;
public:
A() {
a="Adil";
}
friend class B;
};
class B {
private:
int b;
p...
friend Class Example
int main() {
A a;
B b;
b.showA(a);
return 0;
}
Object Oriented Programming in C++
Lecture Slides By A...
More Example of friend Class-1
class Numbers {
int a;
int b;
public:
Numbers(int i, int j)
{
a = i;
b = j;
}
friend class ...
More Example of friend Class-2
int Average :: average(Numbers x)
{
return ((x.a + x.b)/2);
}
int main()
{
Numbers ob(23, 6...
Advantages of using friend classes
• A friend function has the following advantages :
• 1. Provides additional functionali...
Polymorphism in C++
• The process of representing one Form in multiple
forms is known as Polymorphism. Here one form
repre...
Polymorphism in C++
Real life example of Polymorphism in C++
• Suppose if you are in class room that time you
behave like ...
Polymorphism in C++
Object Oriented Programming in C++
Lecture Slides By Adil Aslam
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Object Oriented Programming Using C++ (Slides 3/5)
Upcoming SlideShare
Loading in …5
×

Object Oriented Programming Using C++ (Slides 3/5)

1,434 views

Published on

Inheritance in C++
Type of Inheritance in C++
Advantage of inheritance
Inheritance in C++ Program Example
Inheritance Visibility Mode
Single Inheritance
Single Inheritance Example
Multiple Inheritance
Example of Multiple Inheritance
Ambiguity in Multiple Inheritance
Hierarchical Inheritance
Hierarchical Inheritance Example
Multilevel Inheritance
Example of Multilevel Inheritance
Hybrid Inheritance
Example of Hybrid Inheritance
Private inheritance in C++
Constructor, Destructor and Inheritance
Constructor call in Multiple Inheritance
Overriding Base Class Functions
Gaining Access to an Overridden Function
Disadvantages of Inheritance
Hybrid Inheritance and Virtual Class
Virtual Class
C++ friend Function and friend Classes
Polymorphism in C++
Type of Polymorphism
Compile time polymorphism
Method Overloading in C++
Method Overriding in C++
C++ Operator Overloading
Rules For Operator Overloading

Published in: Education
  • Be the first to comment

Object Oriented Programming Using C++ (Slides 3/5)

  1. 1. Object-Oriented Programming in C++ Lecture Slides By Adil Aslam My Email Address: adilaslam5959@gmail.com Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  2. 2. Object Oriented Programming in C++ Lecture Slides By Adil Aslam About Me i am Student of BSCS My email address: adilaslam5959@gmail.com
  3. 3. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Inheritance in C++ Outer Class
  4. 4. Inheritance in C++ • Inherit Definition - Derive quality and characteristics from parents or ancestors. Like you inherit features of your parents. • Example: "She had inherited the beauty of her mother" • Inheritance in Object Oriented Programming can be described as a process of creating new classes from existing classes. • The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object-oriented programming. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  5. 5. Inheritance in C++ • Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is called Child or Derived or Sub class. • Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused. Object Oriented Programming in C++ Lecture Slides By Adil Aslam NOTE : All members of a class except Private, are inherited
  6. 6. Advantage of inheritance • If we develop any application using this concept than that application have following advantages, • Application development time is less. • Application take less memory. • Application execution time is less. • Application performance is enhance (improved). • Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost. • Use of Virtual Keyword Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  7. 7. Real life Example of Inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  8. 8. Diagram Object Oriented Programming in C++ Lecture Slides By Adil Aslam In the above diagram data members and methods are represented in broken line are inherited from faculty class and they are visible in student class logically.
  9. 9. Basic Syntax of Inheritance • While defining a subclass like this, the super class must be already defined or at least declared before the subclass declaration. • Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, private or protected. • : is operator which is used for inheriting the features of base class into derived class it improves the functionality of derived class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class Subclass_name : access_mode Superclass_name { // data members // methods }
  10. 10. Inheritance in C++ Program Example class Rectangle { ... .. ... }; class Area : public Rectangle { ... .. ... }; class Perimeter : public Rectangle { .... .. ... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  11. 11. Inheritance in C++ Program Example class Rectangle { ... .. ... }; class Area : public Rectangle { ... .. ... }; class Perimeter : public Rectangle { .... .. ... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Super Class/ Parent Class Sub Class Perimeter Sub Class Area
  12. 12. Inheritance in C++ Program Example class Rectangle { ... .. ... }; class Area : public Rectangle { ... .. ... }; class Perimeter : public Rectangle { .... .. ... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  13. 13. Example of Inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class Dog inherits the properties from its super class Animal Animal Dog
  14. 14. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  15. 15. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Animal is a Super Class
  16. 16. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Dog is a sub Class
  17. 17. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Sub class Dog inherit the Properties of Super class Animal
  18. 18. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Create an object of sub class
  19. 19. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Legs are: 4 Tail is: 1
  20. 20. Inheritance Visibility Mode • Depending on Access modifier used while inheritance, the availability of class members of Super class in the sub class changes. It can either be private, protected or public. • 1) Public Inheritance • This is the most used inheritance mode. In this the protected member of super class becomes protected members of sub class and public becomes public. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class Subclass : public Superclass
  21. 21. Inheritance Visibility Mode • 2) Private Inheritance • In private mode, the protected and public members of super class become private members of derived class. • 3) Protected Inheritance • In protected mode, the public and protected members of Super class becomes protected members of Sub class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class Subclass : Superclass // By default its private inheritance class subclass : protected Superclass
  22. 22. Table showing all the Visibility Modes Object Oriented Programming in C++ Lecture Slides By Adil Aslam Derived Class Derived Class Derived Class Base class Public Mode Private Mode Protected Mode Private Not Inherited Not Inherited Not Inherited Protected Protected Private Protected Public Public Private Protected
  23. 23. Access Control and Inheritance • A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Access public protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no
  24. 24. Inheritance Visibility Mode Object Oriented Programming in C++ Lecture Slides By Adil Aslam Member Access Specifier How Members of the Base Class Appear in the Derived Class Private Private members of the base class are inaccessible to the derived class. Protected members of the base class become private members of the derived class. Public members of the base class become private members of the derived class.
  25. 25. Inheritance Visibility Mode Object Oriented Programming in C++ Lecture Slides By Adil Aslam Member Access Specifier How Members of the Base Class Appear in the Derived Class Protected Private members of the base class are inaccessible to the derived class. Protected members of the base class become protected members of the derived class. Public members of the base class become protected members of the derived class.
  26. 26. Inheritance Visibility Mode Object Oriented Programming in C++ Lecture Slides By Adil Aslam Member Access Specifier How Members of the Base Class Appear in the Derived Class Public Private members of the base class are inaccessible to the derived class. Protected members of the base class become protected members of the derived class. Public members of the base class become public members of the derived class.
  27. 27. Access Control and Inheritance • A derived class inherits all base class methods with the following exceptions: • Constructors, destructors and copy constructors of the base class. • Overloaded operators of the base class. • The friend functions of the base class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  28. 28. class Base { public: int m_public; // can be accessed by anybody private: int m_private; // can only be accessed by Base members and friends (but not derived classes) protected: int m_protected; // can be accessed by Base members, friends, and derived classes }; class Derived: public Base { public: Derived() { m_public = 1; // allowed: can access public base members from derived class m_private = 2; // not allowed: can not access private base members from derived class m_protected = 3; // allowed: can access protected base members from derived class } }; int main() { Base base; base.m_public = 1; // allowed: can access public members from outside class base.m_private = 2; // not allowed: can not access private members from outside class base.m_protected = 3; // not allowed: can not access protected members from outside class } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  29. 29. Types of Inheritance • Based on number of ways inheriting the feature of base class into derived class it have five types they are: • Single inheritance • Multiple inheritance • Hierarchical inheritance • Multiple inheritance • Hybrid inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  30. 30. Types of Inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  31. 31. Types of Inheritance • Single Inheritance • In single inheritance there exists single base class and single derived class. • It is the most simplest form of Inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B
  32. 32. Single Inheritance • In single inheritance there exists single base class and single derived class. • It is the most simplest form of Inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Parent Class/ Base Class Child Class/ Derived Class
  33. 33. Single Inheritance Example • When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance. • For example, • Animal is derived from living things • Car is derived from vehicle • Typist is derived from staff • Manager derived from employee • Circle derived from shapes Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  34. 34. Syntax of Inheritance in C++ cclass base_classname { properties... methods... }; class derived_classname : visibility_mode base_classname { properties... methods... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  35. 35. Single Inheritance Example • For example: Car, Bicycle, Motorcycle are all vehicles and have many similar properties like tire, brakes, seat, etc. So they can be derived from class Vehicle. • Therefore, vehicle is base class and car, bus, motorcycle are derived classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Vehicle MotorcycleBusCar
  36. 36. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  37. 37. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A is a Parent Class Class B is a Child Class
  38. 38. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Public Data Member of Parent Class
  39. 39. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Child class B inherit the properties of Parent class A After Inheritance Class A Member Variable goes in Class B as a Member Variable of Class B
  40. 40. Example of Single Inheritance-1 class Person { public: string name; int age; }; class Student : public Person { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  41. 41. Example of Single Inheritance-1 class Person { public: string name; int age; }; class Student : public Person { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class is Person Child Class Student Access the Properties of the Parent Class
  42. 42. Example of Single Inheritance-1 class Person { public: string name; int age; }; class Student : public Person { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance Class Student Look like this class Student : public Person { public: string name; int age; public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } };
  43. 43. Example of Single Inheritance-2 int main() { //Below Creating an object of Child Class(Student) Student s; s.name="Adil Aslam"; s.age=20; //Calling Child Class Function s.show(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  44. 44. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  45. 45. Another Example of Single Inheritance class Employee { public: int salary; }; class Developer : public Employee { public: void data() { cout<<"Enter the Salary: "; cin>>salary; cout<<"Salary is: "<<salary; } }; int main() { Developer obj; //Creating an object of Developer Class (Child Class) obj.data(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  46. 46. Another Example of Single Inheritance class Employee { public: int salary; }; class Developer : public Employee { public: void data() { cout<<"Enter the Salary: "; cin>>salary; cout<<"Salary is: "<<salary; } }; int main() { Developer obj; //Creating an object of Developer Class (Child Class) obj.data(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class is Employee Child Class is Developer
  47. 47. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  48. 48. Same Previous Program with some Modifications-1 class Employee //Parent Class { public: int salary; }; class Developer : public Employee //Developer is Child Class { public: void data() { cout<<"Enter the Salary: "; cin>>salary; cout<<"Salary is: "<<salary<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  49. 49. Same Previous Program with some Modifications-1 int main() { //Creating an object of Developer Class(Child Class) Developer d; d.data(); //Creating an object of Employee Class(Parent Class) Employee e; e.salary=1000000; cout<<"Now Salary is: "<<e.salary; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  50. 50. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  51. 51. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.display(); } Above which function is called
  52. 52. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.display(); } Above which function is called Ans is typist class method is called Typist Class Method having high Preference
  53. 53. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.display(); } What about if we want to call staff Class Method…???
  54. 54. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.staff::display(); } What about if we want to call staff Class Method…??? Ans is use Scope Resolution Operator(::)
  55. 55. Example of Single Inheritance-1 class staff { private: char name[50]; int code; public: void getdata(); void display(); }; class typist: public staff { private: int speed; public: void getdata(); void display(); }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this Example we use Scope Resolution Operator(::)
  56. 56. Example of Single Inheritance-2 void staff::getdata() { cout<<"Name:"; gets(name); cout<<"Code:"; cin>>code; } void staff::display() { cout<<"Name:"<<name<<endl; cout<<"Code:"<<code<<endl; } void typist::getdata() { cout<<"Speed:"; cin>>speed; } void typist::display() { cout<<"Speed:"<<speed<<endl; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this Example we use Scope Resolution Operator(::)
  57. 57. Example of Single Inheritance-3 int main() { typist t; cout<<"Enter data"<<endl; t.staff::getdata(); //calling staff class method t.getdata(); cout<<endl<<"Display data"<<endl; t.staff::display(); //calling staff class method t.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this Example we use Scope Resolution Operator(::) In this example, typist class is derived and staff class is the base class. Public members of class staff such as staff::getdata() and staff::display() are inherited to class typist. Since the child is derived from a single parent class, it is single inheritance.
  58. 58. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  59. 59. One More Example of Single Inheritance-1 class person { //Parent class private: char fname[100],lname[100],gender[10]; protected: int age; public: void input_person(); void display_person(); }; class student: public person { //Child class private: char college_name[100]; char level[20]; public: void input_student(); void display_student(); }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  60. 60. One More Example of Single Inheritance-2 void person::input_person() { cout<<"First Name: "; cin>>fname; cout<<"Last Name: "; cin>>lname; cout<<"Gender: "; cin>>gender; cout<<"Age: "; cin>>age; } void person::display_person() { cout<<"First Name : "<<fname<<endl; cout<<"Last Name : "<<lname<<endl; cout<<"Gender : "<<gender<<endl; cout<<"Age : "<<age<<endl; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  61. 61. One More Example of Single Inheritance-3 void student::input_student() { person::input_person(); cout<<"College: "; fflush(stdin); gets(college_name); cout<<"Level: "; cin>>level; } void student::display_student() { person::display_person(); cout<<"College : "<<college_name<<endl; cout<<"Level : "<<level<<endl; } int main() { student s; cout<<"Input data"<<endl; s.input_student(); cout<<endl<<"Display data"<<endl; s.display_student(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  62. 62. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  63. 63. Explanation of Previous Program • In the previous example, we have a class person with attributes fname (first name), lname (last name), gender and age and methods input_person() to input data and display_person() to display data. Another class student is derived from person which has college_name and level as attributes and input_student() and display_student() as methods to input and display data respectively. • Here, person is base class and student is derived class. Since, person has publicly inherited student members, the private members fname, lname and gender are not inherited. The protected member age is inherited as a protected member in student, and public member functions input_person() and display_person() are inherited as public members. This is the simplest example of inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  64. 64. Types of Inheritance • Multiple Inheritance • In this type of inheritance a single derived class may inherit from two or more than two base classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class C
  65. 65. Types of Inheritance • Multiple Inheritance • In this type of inheritance a single derived class may inherit from two or more than two base classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class C Parent ClassParent Class Child Class
  66. 66. Multiple Inheritance Example • When a class is derived from two or more base classes, such inheritance is called Multiple Inheritance. It allow us to combine the features of several existing classes into a single class. • For example, • Petrol is derived from both liquid and fuel. • A child has character of both his/her father and mother, etc. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  67. 67. Syntax of Multiple Inheritance class base_class1 { properties; methods; }; class base_class2 { properties; methods; }; class derived_classname : visibility_mode base_class1, visibility_mode base_class2 { properties; methods; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  68. 68. Multiple Inheritance Example class A { }; class B { }; class C: public B, public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  69. 69. Multiple Inheritance Example class A { }; class B { }; class C: public B, public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class Parent Class Child Class Note: Class A and Class B Both are Parent in Context of Child Class B
  70. 70. Multiple Inheritance Example class A { public: string name; }; class B { public: int marks; }; class C: public B, public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Child Class C inherit the Properties of both Class A and Class B After inheritance Child Class C look Like this class C: public B, public A { public: string name; public: int marks; };
  71. 71. Example of Multiple Inheritance class A { public: string name; }; class B { public: int marks; }; class C: public B, public A { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Marks are: "<<marks; } }; int main() { C c; c.name="Adil Aslam"; c.marks=90; c.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  72. 72. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  73. 73. Ambiguity in Multiple Inheritance • In multiple inheritance, a single class is derived from two or more parent classes. So, there may be a possibility that two or more parents have same named member function. • If the object of child class needs to access one of the same named member function then it results in ambiguity. The compiler is confused as method of which class to call on executing the call statement. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  74. 74. Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.display(); /*causes ambiguity*/ } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  75. 75. Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.display(); /*causes ambiguity*/ } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A and Class B Both have Member Function with the same Name
  76. 76. Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.display(); /*causes ambiguity*/ } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Here we call the display function using object of class C but compiler confuse which display function it call because Class C inherit the Properties of both class A and class B Class C After Inheritance Look Like this class C: public A, public B { public: void display() { cout <<"This is method of A"; void display() { cout <<"This is method of B"; };
  77. 77. Compiler View Object Oriented Programming in C++ Lecture Slides By Adil Aslam Compiler Error
  78. 78. Ambiguity Resolution of Multiple Inheritance • This problem can be resolved by class name and using scope resolution operator to specify the class whose method is called. • In the previous example, if we want to call the method of class A then we can call it as below, • Similarly, if we need to call the method of class B then, Object Oriented Programming in C++ Lecture Slides By Adil Aslam derived_objectname.parent_classname::same_named_function([parameter]); sample.A :: display(); sample.B :: display();
  79. 79. Solution of Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.A::display(); //Now Here A Class (Parent Class) Method is Called } Object Oriented Programming in C++ Lecture Slides By Adil Aslam This is method of A
  80. 80. Solution of Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.B::display(); //Now Here B Class (Parent Class) Method is Called } Object Oriented Programming in C++ Lecture Slides By Adil Aslam This is method of B
  81. 81. Solution of Ambiguity in Multiple Inheritance-1 class A { public: void display() { cout <<"This is method of An"; } }; class B { public: void display() { cout <<"This is method of B"; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  82. 82. Solution of Ambiguity in Multiple Inheritance-2 class C: public A, public B { public: void output() { A::display(); B::display(); } }; int main() { C sample; sample.output(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  83. 83. Solution of Ambiguity in Multiple Inheritance-2 class C: public A, public B { public: void output() { A::display(); B::display(); } }; int main() { C sample; sample.output(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Here inside a function body we Set which function is called
  84. 84. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  85. 85. Another Example of Multiple Inheritance class Mammal { public: Mammal() { cout << "Mammals can give direct birth." << endl; } }; class WingedAnimal { public: WingedAnimal() { cout << "Winged animal can flap." << endl; } }; class Bat: public Mammal, public WingedAnimal { }; int main() { Bat b1; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  86. 86. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  87. 87. One More Example of Multiple Inheritance-1 class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"<<endl; cin>>m1>>m2; } }; class sports { protected: int sm; // sm = Sports mark public: void getsm() { cout<<"nEnter the sports mark :"<<endl; cin>>sm; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  88. 88. One More Example of Multiple Inheritance-2 class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot; cout<<"ntAverage : "<<avg; } }; int main() { statement obj; obj.get(); obj.getsm(); obj.display(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  89. 89. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  90. 90. Example of Multiple Inheritance-1 class liquid { float specific_gravity; public: void input() { cout<<"Specific gravity: "; cin>>specific_gravity; } void output() { cout<<"Specific gravity: "<<specific_gravity<<endl; } }; class fuel { float rate; public: void input() { cout<<"Rate(per liter): $ "; cin>>rate; } void output() { cout<<"Rate(per liter): $ "<<rate<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  91. 91. Example of Multiple Inheritance-2 class petrol: public liquid, public fuel { public: void input() { liquid::input(); fuel::input(); } void output() { liquid::output(); fuel::output(); } }; int main() { petrol p; cout<<"Enter data"<<endl; p.input(); cout<<endl<<"Displaying data"<<endl; p.output(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  92. 92. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  93. 93. Types of Inheritance • Hierarchical Inheritance • In this type of inheritance, multiple derived classes inherits from a single base class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class CClass B Class D
  94. 94. Hierarchical Inheritance Example • When more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class. Problems where hierarchy has to be maintained can be solved easily using this inheritance. • For Example: • Civil, Computer, Mechanical, Electrical are derived from Engineer. • Natural language, Programming language are derived from Language. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  95. 95. Syntax of Hierarchical Inheritance class base_classname { properties; methods; }; class derived_class1:visibility_mode base_classname { properties; methods; }; class derived_class2:visibility_mode base_classname { properties; methods; }; ... ... ... class derived_classN:visibility_mode base_classname { properties; methods; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  96. 96. Hierarchical Inheritance Example class A { }; class B: public A { }; class C: public A { }; class D: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  97. 97. Hierarchical Inheritance Example class A { }; class B: public A { }; class C: public A { }; class D: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Here A is Parent Class Class B, C and D are Child Classes of Class A
  98. 98. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  99. 99. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Side is a Parent Class Class “Square” is Child of “Slide” Class Class “Cube” is Child of “Slide” Class
  100. 100. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance Class Square Look Like tis class Square: public Side { protected: int l; public: void set_values (int x) { l=x; } public: int sq() { return (l *l); } };
  101. 101. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance Class Cube Look Like tis class Cube: public Side { protected: int l; public: void set_values (int x) { l=x; } public: int cub() { return (l *l*I); } };
  102. 102. Example of Hierarchical Inheritance-2 int main () { //Creating an Object of Class Square Square s; s.set_values (10); cout<<"---------Result----------"<<endl<<endl; cout << "The square value is::" << s.sq() << endl; //Creating an Object of Class Cube Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In the above example the two derived classes "Square", "Cube" uses a single base class "Side". Thus two classes are inherited from a single class. This is the hierarchical inheritance OOP's concept in C++.
  103. 103. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  104. 104. One More Example of Hierarchical Inheritance-1 class Shape { protected: float width, height; public: void set_data (float a, float b) { width = a; height = b; } }; class Rectangle: public Shape { public: float area () { return (width * height); } }; class Triangle: public Shape { public: float area () { return (width * height / 2); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  105. 105. One More Example of Hierarchical Inheritance-1 int main () { //Creating an Object of Class Rectangle Rectangle rect; //Creating an Object of Class Triangle Triangle tri; rect.set_data (5,3); tri.set_data (2,5); cout<<"------------Result------------"<<endl<<endl; cout <<"Area of Rectangle is: "<<rect.area() << endl; cout <<"Area of Triangle is: "<<tri.area() << endl; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  106. 106. Explanation of the Previous Program • The object of the class Rectangle contains : width, height inherited from Shape becomes the protected member of Rectangle. • set_data() inherited from Shape becomes the public member of Rectangle area is Rectangle’s own public member The object of the class Triangle contains : width, height inherited from Shape becomes the protected member of Triangle.set_data() inherited from Shape becomes the public member of Triangle area is Triangle’s own public member • set_data () and area() are public members of derived class and can be accessed from outside class i.e. from main() Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  107. 107. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  108. 108. Example of Hierarchical Inheritance-1 class person { char name[100],gender[10]; int age; public: void getdata() { cout<<"Name: "; fflush(stdin); /*clears input stream*/ gets(name); cout<<"Age: "; cin>>age; cout<<"Gender: "; cin>>gender; } void display() { cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; cout<<"Gender: "<<gender<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  109. 109. Example of Hierarchical Inheritance-2 class student: public person{ char institute[100], level[20]; public: void getdata() { person::getdata(); cout<<"Name of College/University: "; fflush(stdin); gets(institute); cout<<"Level: "; cin>>level; } void display() { person::display(); cout<<"Name of College/University: "<<institute<<endl; cout<<"Level: "<<level<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  110. 110. Example of Hierarchical Inheritance-3 class employee: public person { char company[100]; float salary; public: void getdata() { person::getdata(); cout<<"Name of Company: "; fflush(stdin); gets(company); cout<<"Salary: Rs."; cin>>salary; } void display(){ person::display(); cout<<"Name of Company: "<<company<<endl; cout<<"Salary: Rs."<<salary<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  111. 111. Example of Hierarchical Inheritance-4 int main() { student s; employee e; cout<<"Student"<<endl; cout<<"Enter data"<<endl; s.getdata(); cout<<endl<<"Displaying data"<<endl; s.display(); cout<<endl<<"Employee"<<endl; cout<<"Enter data"<<endl; e.getdata(); cout<<endl<<"Displaying data"<<endl; e.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this program student and employee classes are derived from person. Person has two public methods: getdata() and display(). These methods are inherited by both student and employee. Input is given using getdata() method and displayed using display() method. This is an example of hierarchical inheritance since two classes are derived from a single class.
  112. 112. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  113. 113. Hierarchical Inheritance Example class Number { private: int num; public: void getNumber() { cout << "Enter an integer number: "; cin >> num; } int returnNumber() { return num; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  114. 114. Hierarchical Inheritance Example Cont. //Base Class 1, to calculate square of a number class Square : public Number { public: int getSquare() { int num, sqr; //get number from class Number num=returnNumber(); sqr=num*num; return sqr; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  115. 115. Hierarchical Inheritance Example Cont. //Base Class 2, to calculate cube of a number class Cube : public Number { private: public: int getCube(){ int num, cube; //get number from class Number num=returnNumber(); cube=num*num*num; return cube; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  116. 116. Hierarchical Inheritance Example Cont. int main() { Square objS; Cube objC; int sqr, cube; objS.getNumber(); sqr =objS.getSquare(); cout << "Square of "<< objS.returnNumber() << " is: " << sqr << endl; objC.getNumber(); cube=objC.getCube(); cout << "Cube of "<< objS.returnNumber() << " is: " << cube << endl; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  117. 117. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  118. 118. Types of Inheritance • Multilevel Inheritance • In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class
  119. 119. Types of Inheritance • Multilevel Inheritance • In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class Most Parent Class Parent Class Child Class Child Class
  120. 120. Multilevel Inheritance • When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent classes, such inheritance is called Multilevel Inheritance. The level of inheritance can be extended to any number of level depending upon the relation. Multilevel inheritance is similar to relation between grandfather, father and child. • For Example, • Student is derived from person and person is derived from class living things. • Car is derived from vehicle and vehicle is derived from machine. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  121. 121. Syntax of Multilevel Inheritance class base_classname { properties; methods; }; class intermediate_classname:visibility_mode base_classname { properties; methods; }; class child_classname:visibility_mode intermediate_classname { properties; methods; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  122. 122. Multilevel Inheritance class A { }; class B: public A { }; class C: public B { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  123. 123. Multilevel Inheritance class A { }; class B: public A { }; class C: public B { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Most Parent Class Parent Class of C and Child Class of A Child Class of B
  124. 124. Example of Multilevel Inheritance class A { public: void display() { cout<<"Base Class Content"; } }; class B : public A { }; class C : public B { }; int main() { C c; c.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  125. 125. Example of Multilevel Inheritance class A { public: void display() { cout<<"Base Class Content"; } }; class B : public A { }; class C : public B { }; int main() { C c; c.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class A Having a Member Function “display”
  126. 126. Example of Multilevel Inheritance class A { public: void display() { cout<<"Base Class Content"; } }; class B : public A { }; class C : public B { }; int main() { C c; c.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class B inherit the Member Function from Class A class B : public A { public: void display() { cout<<"Base Class Content"; } }; After inheritance Class B Look Like this This is only Logical Concept
  127. 127. Example of Multilevel Inheritance class A { public: void display() { cout<<"Base Class Content"; } }; class B : public A { }; class C : public B { }; int main() { C c; c.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class C inherit the Member Function from Class B class C : public B { public: void display() { cout<<"Base Class Content"; } }; After inheritance Class C Look Like this
  128. 128. Example of Multilevel Inheritance class A { public: void display() { cout<<"Base Class Content"; } }; class B : public A { }; class C : public B { }; int main() { C c; c.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Base Class Content Call the display function of class C. This Display function Class C inherit from Class B and Class B inherit from Class A
  129. 129. Explanation of Previous Program • In this program, class B is derived from A and C is derived from B. An object of class C is defined in main() function. When the display() function is called, display() in class A is executed because there is no display() function in C and B. The program first looks for display() in class C first but, can't find it. Then, looks in B because C is derived from B. Again it can't find it. And finally looks it in A and executes the codes inside that function. • If there was display() function in C too, then it would have override display() in A because of member function overriding. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  130. 130. One More Example of Multilevel Inheritance-1 class person { char name[100],gender[10]; int age; public: void getdata() { cout<<"Name: "; gets(name); cout<<"Age: "; cin>>age; cout<<"Gender: "; cin>>gender; } void display() { cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; cout<<"Gender: "<<gender<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  131. 131. One More Example of Multilevel Inheritance-2 class employee: public person { char company[100]; float salary; public: void getdata() { person::getdata(); cout<<"Name of Company: "; gets(company); cout<<"Salary: Rs."; cin>>salary; } void display() { person::display(); cout<<"Name of Company: "<<company<<endl; cout<<"Salary: Rs."<<salary<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  132. 132. One More Example of Multilevel Inheritance-3 class programmer: public employee { int number; public: void getdata() { employee::getdata(); cout<<"Number of programming language known: "; cin>>number; } void display() { employee::display(); cout<<"Number of programming language known: "<<number; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  133. 133. One More Example of Multilevel Inheritance-4 int main() { programmer p; cout<<"Enter data"<<endl; p.getdata(); cout<<endl<<"Displaying data"<<endl; p.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam This program is an example of multiple inheritance. Here, programmer class is derived from employee which is derived from person. Each class has required attributes and methods. The public features of person is inherited by employee and the public features of employee is inherited by programmer. The method getdata() asks user to input data, while display() displays the data.
  134. 134. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  135. 135. One More Example of Multilevel Inheritance-1 class grandfather { int age; char name [20]; public: void get() { cout << "Enter your grand father's Name : "; cin >> name; cout << "Enter your grand father's Age : "; cin >> age; } void show() { cout << "n Your grand father's name is " << name; cout << "n Your grand father's age is " << age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  136. 136. One More Example of Multilevel Inheritance-2 class father : public grandfather { int age; char name [20]; public: void get() { cout << "Enter your father's Name : "; cin >> name; cout << "Enter your father's Age : "; cin >> age; } void show() { cout << "n Your father's name is " << name; cout << "n Your father's age is " << age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  137. 137. One More Example of Multilevel Inheritance-3 class son : public father { int age; char name [20]; public: void get() { grandfather :: get(); father :: get(); cout << "Enter the child's Name : "; cin >> name; cout << "Enter the child's Age : "; cin >> age; } void show() { grandfather :: show(); father :: show(); cout << "n Child's name is " << name; cout << "n Child's age is " << age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  138. 138. One More Example of Multilevel Inheritance-4 int main () { //Creating an object of Class Son son s; s.get(); s.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  139. 139. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  140. 140. Types of Inheritance • Hybrid Inheritance • Hybrid Inheritance is a method where one or more types of inheritance are combined together and used. • Example of Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class C Class D
  141. 141. Types of Inheritance • Hybrid Inheritance • Hybrid Inheritance is a method where one or more types of inheritance are combined together and used. • Example of Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class C Class D Most Parent Class Parent Class Child Class Parent Class Child Class Child Class
  142. 142. Hybrid Inheritance Example class A { }; class B: public A { }; class C: public A { }; class D: public B, public C { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam This is a mixture of Hierarchical and Multilevel inheritance so called hybrid inheritance
  143. 143. Hybrid Inheritance Example class A { }; class B: public A { }; class C: public A { }; class D: public B, public C { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Most Parent Class Class B:: Parent Class of D and Child Class of A Class C:: Parent Class of D and Child Class of A Child Class
  144. 144. Example of Hybrid Inheritance-1 class student {//base class derivation protected: int r_no; public: void getRollno() { cout << "Enter the roll number of student : "; cin >> r_no; } void putRollno() { cout << "nRoll Number -: " << r_no << "n"; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  145. 145. Example of Hybrid Inheritance-2 class test : public student {//intermediate base class protected: int part1, part2; public: void getMarks() { cout << "Enter the marks of student in SA 1 : "; cin >> part1; cout << "Enter the marks of student in SA 2 : "; cin >> part2; } void putMarks() { cout << "Marks Obtained : " << "n"; cout << " Part 1 -: " << part1; cout << "n Part 2 -: " << part2 << "n"; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  146. 146. Example of Hybrid Inheritance-3 class sports { protected: int score; public: void getSportsMarks() { cout << "Enter the marks in Physical Eduction : "; cin >> score; } void putSportsMarks() { cout << "Additional Marks : " << score << "n n"; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  147. 147. Example of Hybrid Inheritance-4 //derived from test and sports class result : public test, public sports { int total; public: void display () { total = part1 + part2 + score; putRollno(); putMarks(); putSportsMarks(); cout << "Total Score : " << total ; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  148. 148. Example of Hybrid Inheritance-5 int main () { //Creating an object of class result result s1; s1.getRollno(); s1.getMarks(); s1.getSportsMarks(); s1.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  149. 149. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  150. 150. Private inheritance • In private inheritance, following are the key features: • Each public member in the base class is private in the derived class • Each protected member in the base class is private in the derived class • Each private member in the base class remains private in the base class Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  151. 151. The following table shows the accesses specifier of the data member of the base class baseA in the derived class deriveD. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class baseA { private: int x; protected: int y; public: int z; }; class deriveD: private baseA { private: int w; }; Member in deriveD Access status How obtained x Not accessible from baseA From baseA y Private in deriveD From baseA z Private in deriveD From baseA w Private in deriveD In deriveD
  152. 152. Example of Private Inheritance-1 class student { private: int roll; protected: char sex; public: void getroll(); void getsex(); void display_roll(); }; class result: private student { private: float marks; public: void getmarks(); void display_marks(); }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  153. 153. Example of Private Inheritance-2 void student::getroll() { cout<<"Enter Roll:"; cin>>roll; } void student::getsex() { cout<<"nEnter Sex:"; cin>>sex; } void result::getmarks() { getsex(); cout<<"nEnter Marks:"; cin>>marks; } void student::display_roll() { cout<<"nRoll:"<<roll; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  154. 154. Example of Private Inheritance-3 void result::display_marks() { cout<<"nSex:"<<sex; cout<<"nMarks:"<<marks; } int main() { student a; result b; a.getroll(); b.getmarks(); a.display_roll(); b.display_marks(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  155. 155. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  156. 156. Order of Constructor Call • Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first the base class default constructor is executed and then the derived class's constructor finishes execution. • Points to Remember • Whether derived class's default constructor is called or parameterized is called, base class's default constructor is always called inside them. • To call base class's parameterized constructor inside derived class's parameterized constructor, we must mention it explicitly while declaring derived class's parameterized constructor. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  157. 157. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  158. 158. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Base is a Parent Class
  159. 159. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Default Constructor of Base Class
  160. 160. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Derived Class is a Child Class
  161. 161. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Default Constructor of Derived Class
  162. 162. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Derived Class Parameterized Constructor with one Parameter
  163. 163. Base class Default Constructor in Derived class Constructors class Base { int x; public: Base() { cout << "Base default constructor"<<endl; } }; class Derived : public Base { int y; public: Derived() { cout << "Derived default constructor"<<endl; } Derived(int i) { cout << "Derived parameterized constructor"; } }; int main() { Base b; Derived d1; Derived d2(10); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Base default constructor Base default constructor Derived default constructor Base default constructor Derived parameterized constructor
  164. 164. Constructor, Destructor and Inheritance • The constructors are used to initialize member variables of the object, and the destructor is used to destroy the object. The compiler automatically invokes constructors and destructors. The derived class does not require a constructor , if the base class contain a zero-argument constructor. In case the base class has a parameterized constructor, then it is essential for the derived class to have a constructor. The derived class constructor passes arguments to the base class constructor. • In inheritance, normally derived classes are used to declare objects. Hence, it is necessary to define constructor in the derived class. When an object of a derived class is declare, the constructor of the base and derived classes are executed. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  165. 165. Constructor, Destructor and Inheritance • In heritance, destructors are executed in reverse order of the constructor execution. The destructors are executed when an object goes out of scope. • To know the execution of constructors and destructors, let us study the following program: Object Oriented Programming in C++ Lecture Slides By Adil Aslam Write a program to show sequence of execution of constructor and destructor in multiple inheritance.
  166. 166. Constructor, Destructor and Inheritance-1 class A { //Base Class public: A() { cout<<"n Default Constructor of Base Class A"; } ~A() { cout<<"n Destructor of Base Class A"; } }; class B { //Base Class public: B() { cout<<"n Default Constructor of Base Class B"; } ~B() { cout<<"n Destructor of Base Class B"; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  167. 167. Constructor, Destructor and Inheritance-2 class C: public A, public B //Derivation of Class { public: C() { cout<<"n Default Constructor of Base Class C"; } ~C() { cout<<"n Destructor of Base Class C"; } }; int main() { C object; //Creating an Object of Class C return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  168. 168. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  169. 169. Base class Parameterized Constructor in Derived class Constructor-1 class Base { public : int x; public: Base(int i) { x = i; cout << "Base Parameterized Constructor"<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  170. 170. Base class Parameterized Constructor in Derived class Constructor-2 class Derived : public Base { public : int y; public: Derived(int j) : Base(j) { y = j; cout << "Derived Parameterized Constructor"<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  171. 171. Base class Parameterized Constructor in Derived class Constructor-3 int main() { Derived d(10) ; cout << d.x<<endl ; // Output will be 10 cout << d.y ; // Output will be 10 } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  172. 172. Why is Base class Constructor called inside Derived class ? • Constructors have a special job of initializing the object properly. A Derived class constructor has access only to its own class members, but a Derived class object also have inherited property of Base class, and only base class constructor can properly initialize base class members. Hence all the constructors are called, else object wouldn't be constructed properly. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  173. 173. Constructor call in Multiple Inheritance • Its almost the same, all the Base class's constructors are called inside derived class's constructor, in the same order in which they are inherited. • In this case, first class A constructor will be executed, then class B constructor and then class C constructor. • In this case, first class B constructor will be executed, then class C constructor and then class A constructor. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class C : public A, public B ; class A : public B, public C ;
  174. 174. Base class with Constructor and Derived class without Constructor class A { //Base Class public: A() { cout<<"I am Base Class Constructor"; } }; class B: public A { //Derived Class }; int main() { B object; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  175. 175. Base class with Constructor and Derived class without Constructor class A { //Base Class public: A() { cout<<"I am Base Class Constructor"; } }; class B: public A { //Derived Class }; int main() { B object; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam The base class A has a constructor, and the derived class B does not have a constructor. The Object is an object of the derived class B. When object is declare, the constructor of the base class is executed. The derived class is without a constructor; hence only the base class constructor is executed. I am Base Class Constructor
  176. 176. Base class without Constructor and Derived class with Constructor class A { //Base Class public: }; class B: public A { //Derived Class public: B() { cout<<"I am Derived Class Constructor"; } }; int main() { B object; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  177. 177. Base class without Constructor and Derived class with Constructor class A { //Base Class public: }; class B: public A { //Derived Class public: B() { cout<<"I am Derived Class Constructor"; } }; int main() { B object; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam I am Derived Class Constructor
  178. 178. Base class and Derived class with Constructor class A { //Base Class public: A() { cout<<"I am Base Class Constructor"; } }; class B: public A { //Derived Class public: B() { cout<<"I am Derived Class Constructor"; } }; int main() { B object; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  179. 179. Base class and Derived class with Constructor class A { //Base Class public: A() { cout<<"I am Base Class Constructor"; } }; class B: public A { //Derived Class public: B() { cout<<"I am Derived Class Constructor"; } }; int main() { B object; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam I am Base Class Constructor I am Derived Class Constructor In this program both the base and derived class contain constructors. When the object of the derived class type is declare and constructor of both classes are executed, the constructor of the base class is executed first follow by the constructor of the derived class
  180. 180. Overriding Base Class Functions • A derived class can override a member function of its base class by defining a derived class member function with the same name and parameter list. • It is often useful for a derived class to define its own version of a member function inherited from its base class. This may be done to specialize the member function to the needs of the derived class. When this happens, the base class member function is said to be overridden by the derived class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  181. 181. Overriding Base Class Functions class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionn"; } }; int main () { daughter d; d.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  182. 182. Overriding Base Class Functions class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionn"; } }; int main () { daughter d; d.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class Child Class
  183. 183. Overriding Base Class Functions class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionn"; } }; int main () { daughter d; d.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class Method Child Class Method
  184. 184. Overriding Base Class Functions class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionn"; } }; int main () { daughter d; d.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Both Function Having same Name so theses called Overridden Functions
  185. 185. Overriding Base Class Functions class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionn"; } }; int main () { daughter d; d.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam daughter: display function Creating an Object of daughter class
  186. 186. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  187. 187. Gaining Access to an Overridden Function • It is occasionally useful to be able to call the overridden version. This is done by using the scope resolution operator to specify the class of the overridden member function being accessed. • Syntax : • Example : Object Oriented Programming in C++ Lecture Slides By Adil Aslam mother::display(); Class_Name::Method_Name;
  188. 188. Gaining Access to an Overridden Function-1 class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionnn"; mother::display(); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  189. 189. Gaining Access to an Overridden Function-1 class mother { public: void display () { cout << "mother: display functionn"; } }; class daughter : public mother { public: void display () { cout << "daughter: display functionnn"; mother::display(); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  190. 190. Gaining Access to an Overridden Function-2 int main () { daughter d; d.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam daughter: display function mother: display function
  191. 191. Function Overriding • In C++ programming language, Functions overriding is a process of declaring the functions of same name and signatures as in a parent classes and a child classes. Functions overriding is usually used in terms of inheritance, it is also used to produce polymorphic behavior. • Signature of base class method and derived class must be same. Signature involves: • Number of arguments • Type of arguments • Sequence of arguments • When a member function of child class is overridden then its object cannot access the member function of a parent class but in C++, overridden member function of a parent class can be accessed by the use of a scope resolution operator. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  192. 192. Advantages of Inheritance • Reduce code redundancy. • Provides code reusability. • Reduces source code size and improves code readability. • Code is easy to manage and divided into parent and child classes. • Supports code extensibility by overriding the base class functionality within child classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  193. 193. Disadvantages of Inheritance • In Inheritance base class and child classes are tightly coupled. Hence If you change the code of parent class, it will get affects to the all the child classes. • In class hierarchy many data members remain unused and the memory allocated to them is not utilized. Hence affect performance of your program if you have not implemented inheritance correctly. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  194. 194. Functions that are Never Inherited • Constructors and Destructors are never inherited and hence never overrided. • Also, assignment operator = is never inherited. It can be overloaded but can't be inherited by sub class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  195. 195. Inheritance and Static Functions • They are inherited into the derived class. • If you redefine a static member function in derived class, all the other overloaded functions in base class are hidden. • Static Member functions can never be virtual. We will study about Virtual in coming topics. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  196. 196. Hybrid Inheritance and Virtual Class • What is a virtual base class? • An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class. • C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary care is taken so that the duplication is avoided regardless of the number of paths that exist to the child class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  197. 197. Hybrid Inheritance and Virtual Class • In Multiple Inheritance, the derived class inherits from more than one base class. Hence, in Multiple Inheritance there are a lot chances of ambiguity. • Virtual base class is used in situation where a derived have multiple copies of base class. Consider the following figure: Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class D Class CClass B
  198. 198. Hybrid Inheritance and Virtual Class class A { void show(); }; class B:public A {}; class C:public A {}; class D:public B, public C {}; int main() { D obj; obj.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  199. 199. Hybrid Inheritance and Virtual Class class A { void show(); }; class B:public A {}; class C:public A {}; class D:public B, public C {}; int main() { D obj; obj.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Most Parent Class
  200. 200. Hybrid Inheritance and Virtual Class class A { void show(); }; class B:public A {}; class C:public A {}; class D:public B, public C {}; int main() { D obj; obj.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class Member Function
  201. 201. Hybrid Inheritance and Virtual Class class A { void show(); }; class B:public A {}; class C:public A {}; class D:public B, public C {}; int main() { D obj; obj.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class B inherit the Member Function of Parent Class A Class C also inherit the Member Function of Parent class A
  202. 202. Hybrid Inheritance and Virtual Class class A { void show(); }; class B:public A {}; class C:public A {}; class D:public B, public C {}; int main() { D obj; obj.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class D inherit the Properties of both Class B and Class C. Hence class D has two inherited copies of function show().(Member Function)
  203. 203. Hybrid Inheritance and Virtual Class class A { void show(); }; class B:public A {}; class C:public A {}; class D:public B, public C {}; int main() { D obj; obj.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In main() function when we call function show(), then ambiguity arises, because compiler doesn't know which show() function to call. Hence we use Virtual keyword while inheriting class.
  204. 204. Hybrid Inheritance and Virtual Class • Now by adding virtual keyword, we tell compiler to call any one out of the two show() functions. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {};
  205. 205. Hybrid Inheritance and Virtual Class • Now by adding virtual keyword, we tell compiler to call any one out of the two show() functions. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {}; Use of Virtual Keyword
  206. 206. Syntax use of Virtual Keyword class A { ..... ..... }; class B : virtual public A { ..... ..... }; class C : virtual public A { ..... ..... }; class D : public B, public C { .....// only one copy of A .....// will be inherited }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  207. 207. Virtual Class • What is Virtual base class? Explain its uses. • When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class’ name with the word virtual. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  208. 208. Virtual base class Example-1 class A { public: int i; //Member Variable of Class A }; class B : virtual public A { public: int j; }; class C: virtual public A { public: int k; }; class D: public B, public C { public: int sum; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  209. 209. Virtual base class Example-1 class A { public: int i; //Member Variable of Class A }; class B : virtual public A { public: int j; }; class C: virtual public A { public: int k; }; class D: public B, public C { public: int sum; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class Class B inherit the Member Variable of Parent Class A Class C also Inherit the Member Variable of Parent Class A Class D inherit the Member Variables of Both Class B and D
  210. 210. Virtual base class Example-1 class A { public: int i; //Member Variable of Class A }; class B : virtual public A { public: int j; }; class C: virtual public A { public: int k; }; class D: public B, public C { public: int sum; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance D class Look like this class D: public B, public C { public: int i; int j; int k; int sum; }; Note above only one copy of Member Variable of “int i” is inherited due to virtual keyword
  211. 211. Virtual base class Example-2 int main() { D ob; ob.i = 10; ob.j = 20; ob.k = 30; ob.sum = ob.i + ob.j + ob.k; cout << "Value of i is : "<< ob.i<<endl; cout << "Value of j is : "<< ob.j<<endl; cout << "Value of k is : "<< ob.k<<endl; cout << "Sum is : "<< ob.sum <<endl; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  212. 212. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  213. 213. Hybrid Inheritance and Constructor call • Consider this Example class A {}; class B : virtual public A {}; class C : virtual public A {}; class D : public B, public C {}; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  214. 214. Hybrid Inheritance and Constructor call • As we all know that whenever a derived class object is instantiated, the base class constructor is always called. But in case of Hybrid Inheritance, as discussed in Previous example, if we create an instance of class D, then following constructors will be called : • before class D's constructor, constructors of its super classes will be called, hence constructors of class B, class C and class A will be called. • when constructors of class B and class C are called, they will again make a call to their super class's constructor Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  215. 215. Hybrid Inheritance and Constructor call • This will result in multiple calls to the constructor of class A, which is undesirable. As there is a single instance of virtual base class which is shared by multiple classes that inherit from it, hence the constructor of the base class is only called once by the constructor of concrete class, which in our case is class D. • If there is any call for initializing the constructor of class A in class B or class C, while creating object of class D, all such calls will be skipped. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  216. 216. C++ friend Function and friend Classes • One of the important concept of OOP is data hiding, i.e., a nonmember function cannot access an object's private or protected data. But, sometimes this restriction may force programmer to write long and complex codes. So, there is mechanism built in C++ programming to access private or protected data from non-member function which is friend function and friend class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  217. 217. C++ friend Function and friend Classes • friend Function in C++ • If a function is defined as a friend function then, the private and protected data of class can be accessed from that function. The complier knows a given function is a friend function by its keyword friend. • The declaration of friend function should be made inside the body of class (can be anywhere inside class either in private or public section) starting with keyword friend. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  218. 218. C++ friend Function and friend Classes • Why use friend function ? • You do not access private or protected data member of any class, to access private and protected data member of any class you need a friend function. • Syntax Object Oriented Programming in C++ Lecture Slides By Adil Aslam class class_name { ...... .... ........ friend return_type function_name(argument/s); ...... .... ........ } return_type function_name(argument/s); { }
  219. 219. C++ friend Function and friend Classes • Why use friend function ? • You do not access private or protected data member of any class, to access private and protected data member of any class you need a friend function. • Syntax Object Oriented Programming in C++ Lecture Slides By Adil Aslam class class_name { ...... .... ........ friend return_type function_name(argument/s); ...... .... ........ } return_type function_name(argument/s); { } Friend Function Declaration
  220. 220. C++ friend Function and friend Classes • Why use friend function ? • You do not access private or protected data member of any class, to access private and protected data member of any class you need a friend function. • Syntax Object Oriented Programming in C++ Lecture Slides By Adil Aslam class class_name { ...... .... ........ friend return_type function_name(argument/s); ...... .... ........ } return_type function_name(argument/s); { } Friend Function Definition
  221. 221. C++ friend Function and friend Classes • Characteristics of friend functions • A friend function is not in the scope of the class, in which it has been declared as friend. • It cannot be called using the object of that class. • It can be invoked like a normal function without any object. • Unlike member functions, it cannot use the member names directly. • It can be declared in public or private part without affecting its meaning. • Usually, it has objects as arguments. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  222. 222. friend Function Example class Distance { private: int meter; public: friend int func(Distance); }; int func(Distance d) { d.meter=5; return d.meter; } int main() { Distance D; cout<<" Distance : "<<func(D); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  223. 223. friend Function Example class Distance { private: int meter; public: friend int func(Distance); }; int func(Distance d) { d.meter=5; return d.meter; } int main() { Distance D; cout<<" Distance : "<<func(D); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Private Data Variable of Class Distance
  224. 224. friend Function Example class Distance { private: int meter; public: friend int func(Distance); }; int func(Distance d) { d.meter=5; return d.meter; } int main() { Distance D; cout<<" Distance : "<<func(D); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Public Member Function of Class Distance
  225. 225. friend Function Example class Distance { private: int meter; public: friend int func(Distance); }; int func(Distance d) { d.meter=5; return d.meter; } int main() { Distance D; cout<<" Distance : "<<func(D); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam friend Function Declaration friend Function Definition
  226. 226. friend Function Example class Distance { private: int meter; public: friend int func(Distance); }; int func(Distance d) { d.meter=5; return d.meter; } int main() { Distance D; cout<<" Distance : "<<func(D); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Accessing Private data from Non- Member Function Distance : 5
  227. 227. friend Function Example • In previous Program, friend function func() is declared inside Distance class. So, the private data can be accessed from this function. • Though this example gives you what idea about the concept of friend function, this program doesn't give you idea about when friend function is helpful. • Suppose, you need to operate on objects of two different class then, friend function can be very helpful. You can operate on two objects of different class without using friend function but, you program will be long, complex and hard to understand. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  228. 228. Example to Operate on Objects of two Different class using friend Function-1 class B; // forward declaration class A { private: int data; public: A(): data(12){ } friend int func(A , B); //friend function Declaration }; class B { private: int data; public: B(): data(1){ } friend int func(A , B); //friend function Declaration }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  229. 229. Example to Operate on Objects of two Different class using friend Function-2 int func(A d1,B d2) /*Function func() is the friend function of both classes A and B. So, the private data of both class can be accessed from this function.*/ { return (d1.data+d2.data); } int main() { A a; B b; cout<<"Data : "<<func(a,b); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Data : 13
  230. 230. Explanation of Previous Program • In this previous program, classes A and B has declared func() as a friend function. Thus, this function can access private data of both class. In this program, two objects of two different class A and B are passed as an argument to friend function. Thus, this function can access private and protected data of both class. Here, func() function adds private data of two objects and returns it to main function. • To work this program properly, a forward declaration of a class should be made as in above example(forward declaration of class B is made). It is because class B is referenced from class A using code: friend int func(A , B);. So, class A should be declared before class B to work properly. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  231. 231. Another Example friend Function class employee { private: friend void sal(); }; void sal() { int salary=400000; cout<<"Salary: "<<salary; } int main() { employee e; sal(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Salary: 400000
  232. 232. Friend functions have the following Properties • Friend of the class can be member of some other class. • Friend of one class can be friend of another class or all the classes in one program, such a friend is known as GLOBAL FRIEND. • Friend can access the private or protected members of the class in which they are declared to be friend, but they can use the members for a specific object. • Friends are non-members hence do not get “this” pointer. • Friends, can be friend of more than one class, hence they can be used for message passing between the classes. • Friend can be declared anywhere (in public, protected or private section) in the class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  233. 233. friend Classes in C++ • Similarly like, friend function. A class can be made a friend of another class using keyword friend. For example: Object Oriented Programming in C++ Lecture Slides By Adil Aslam ........ ..... ........ class A{ friend class B; // class B is a friend class ..... ..... ..... } class B{ ..... ..... ..... }
  234. 234. friend Classes in C++ • When a class is made a friend class, all the member functions of that class becomes friend function. In this program, all member functions of class B will be friend function of class A. Thus, any member function of class B can access the private and protected data of class A. • If B is declared friend class of A then, all member functions of class B can access private data and protected data of class A but, member functions of class A cannot private and protected data of class B. Remember, friendship relation in C++ is granted not taken. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  235. 235. friend Class Example class A { private: string a; public: A() { a="Adil"; } friend class B; }; class B { private: int b; public: void showA(A x) { cout << "A::a= " << x.a; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  236. 236. friend Class Example class A { private: string a; public: A() { a="Adil"; } friend class B; }; class B { private: int b; public: void showA(A x) { cout << "A::a= " << x.a; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Private Member Variable of Class A Default constructor of class A
  237. 237. friend Class Example class A { private: string a; public: A() { a="Adil"; } friend class B; }; class B { private: int b; public: void showA(A x) { cout << "A::a= " << x.a; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Now Class A is a friend of Class B Declare a variable of type class A and pass to show Function Accessing Private data Variable of class A
  238. 238. friend Class Example int main() { A a; B b; b.showA(a); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam A::a= Adil
  239. 239. More Example of friend Class-1 class Numbers { int a; int b; public: Numbers(int i, int j) { a = i; b = j; } friend class Average; }; class Average { public: int average(Numbers x); }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  240. 240. More Example of friend Class-2 int Average :: average(Numbers x) { return ((x.a + x.b)/2); } int main() { Numbers ob(23, 67); Average avg; cout<<"The Average of 23 and 67 is: "<<avg.average(ob); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam The Average of 23 and 67 is: 45
  241. 241. Advantages of using friend classes • A friend function has the following advantages : • 1. Provides additional functionality which is kept outside the class. • 2. Provides functions that need data which is not normally used by the class. • 3. Allows sharing private class information by a non member function. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  242. 242. Polymorphism in C++ • The process of representing one Form in multiple forms is known as Polymorphism. Here one form represent original form or original method always resides in base class and multiple forms represents overridden method which resides in derived classes. • Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and morphs means forms. So polymorphism means many forms. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  243. 243. Polymorphism in C++ Real life example of Polymorphism in C++ • Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person have different-different behaviors. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  244. 244. Polymorphism in C++ Object Oriented Programming in C++ Lecture Slides By Adil Aslam

×