0% found this document useful (0 votes)
13 views

chapter_5_-_inheritance

inheritance

Uploaded by

sanket1shah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

chapter_5_-_inheritance

inheritance

Uploaded by

sanket1shah24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 5 - Inheritance

Compiled By:
Sanket Shah
Inheritance
• Inheritance is the process, by which class can
acquire the properties and methods of another class.
• The mechanism of deriving a new class from an old
class is called inheritance.
• The new class is called derived class and old class is
called base class.
• The derived class may have all the features of the
base class and the programmer can add new features
to the derived class.
Type of Inheritance
Single Inheritance
• If a class is derived from a single class then it
is called single inheritance.
• Class B is derived from class A
Type of Inheritance
Multilevel Inheritance
• A class is derived from a class which is
derived from another class then it is called
multilevel inheritance
• Here, class C is derived from class B and class
B is derived from class A, so it is called
multilevel inheritance.
Type of Inheritance
Multiple Inheritance
• If a class is derived from more than one class
then it is called multiple inheritance.
• Here, class C is derived from two classes,
class A and class B.
Type of Inheritance
Hierarchical Inheritance
• If one or more classes are derived from one
class then it is called hierarchical inheritance.
• Here, class B, class C and class D are derived
from class A.
Type of Inheritance
Hybrid Inheritance
• It is a combination of any above inheritance types. That is
either multiple or multilevel or hierarchical or any other
combination.
• Here, class B and class C are derived from class A and
class D is derived from class B and class C.
• class A, class B and class C is example of Hierarchical
Inheritance and class B, class C and class D is example of
Multiple Inheritance so this hybrid inheritance is
combination of Hierarchical and Multiple Inheritance.
Single Inheritance
Multilevel Inheritance
Multilevel Inheritance
Output
Multiple Inheritance
#include<iostream>
using namespace std;

class A
{
public:
A()
{
cout << "A's constructor called" << endl;
}
};

class B
{
public:
B()
{
cout << "B's constructor called" << endl;
}
};
Multiple Inheritance
class C: public B, public A // Note the order
{
public:
C()
{
cout << "C's constructor called" << endl;
}
};

int main()
{
C c;
return 0;
}
The diamond problem
• The diamond problem occurs when two super
classes of a class have a common base class.
For example, in the following diagram, the TA
class gets two copies of all attributes of Person
class, this causes ambiguities.
#include<iostream>
using namespace std;
class A
{
// Data members of person
public:
int _a;

};

class B : virtual public A


{
// data members of Faculty
public:
int _b;
};
class C : virtual public A
{
// data members of Student
public:
int _c;

};

class D : public B, public C


{
public:
int _d;

};
int main()
{
D d;
int w= d._a=10;
cout<<"W :"<<w<<endl;
int x=d._b=10;
cout<<"X :"<<x<<endl;
int y=d._c=10;
cout<<"Y :"<<y<<endl;
int z=d._d=10;
cout<<"Z :"<<z<<endl;
return 0;
}
Hierarchical Inheritance
#include<iostream>
using namespace std;
class A
{
public:
void dispA()
{
cout<<"class A method"<<endl;
}
};

class B : public A // Single Inheritance - class B is derived from class A


{
public:
void dispB()
{
cout<<"class B method"<<endl;
}
};
Hierarchical Inheritance
class C : public A // Multilevel Inheritance - class C is derived from class B
{
public:
void dispC()
{
cout<<"class C method"<<endl;
}
};
class D:public A
{
public:
void dispD()
{
cout<<"class D method"<<endl;
}
};
Hierarchical Inheritance
int main()
{
A a;
B b;
C c;
D d;
a.dispA();
cout<<endl;
b.dispA();
b.dispB();
cout<<endl;
c.dispA();
c.dispC();
cout<<endl;
d.dispA();
d.dispD();
}
Hybrid Inheritance
#include<iostream>
using namespace std;
class stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"Roll no"<<rno<<"\n";
}
};
Hybrid Inheritance
class test:public stu
{
protected:
float part1,part2;
public:
void get_mark(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
Hybrid Inheritance
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sports:"<<score<<"\n";

}
};
Hybrid Inheritance
class result: public test, public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total Score="<<total<<"\n";
}
Hybrid Inheritance
int main()
{
//clrscr();
result stu;
stu.get_no(123);
stu.get_mark(27.5,33.0);
stu.getscore(6.0);
stu.display();
return 0;
}

You might also like