Академический Документы
Профессиональный Документы
Культура Документы
Introduction
Inheritance
New classes created from existing classes
Absorb attributes and behaviors
Derived class
Class that inherits data members and member
functions from a previously defined base class
Types of inheritance
public: Derived objects are accessible by the base
class objects
private: Derived objects are inaccessible by the base
class
protected: Derived classes and friends can access
protected members of the base class
2
Public Inheritance
class A : public B
{
// Class A now inherits the members of Class B
// with no change in the access specifier for
}
// the inherited members
Protected Inheritance
class A : protected B
{
// Class A now inherits the members of Class B
// with public members promoted to protected
}
// but no other changes to the inherited members
Private Inheritance
class A : private B
{
// Class A now inherits the members of Class B
// with public and protected members
}
// promoted to private
Inheritance (continued)
Example:
class Shape
{
public:
int GetColor ( ) ;
protected:
// so derived classes can access it
int color;
};
class Two_D : public Shape
{
// put members specific to 2D shapes
here
};
class Three_D : public Shape
{
// put members specific to 3D shapes
here
};
int main ( )
{
Square mySquare;
Cube myCube;
mySquare.getColor ( );
// Square inherits
getColor()
mySquare.getArea ( );
myCube.getColor ( );
// Cube inherits
getColor()
myCube.getVolume ( );
}
Forms of Inheritance
The derived class inherits some or all of the
traits from the base class.A class can also inherit
properties from more than one classes or more
than one level.
Single inheritance
:Class inherits
from one base class
void B::get_ab(void)
int main()
Example:
#include<iostream>
using namespace std;
class B
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D:public B
{
int c;
public:
void mul(void);
void display(void);
};
{
a=5;b=10;
}
int B::get_a()
{
return a;
}
void B::show_a()
{ cout<<a= <<\n;}
void D::mul()
{
c=b*get_a();
}
void D::display()
{
cout<<a= <<get_a() <<\n;
cout<<b= <<b <<\n;
cout<<c= <<c <<\n\n;
}
{
D d;
d.get_ab();
d.mul();
d.show_a();
d.display(); d.b=20;
d.mul(); d.display();
return 0;
}
OUPUT:
a=5
a=5
b=10
c=50
a=5
b=20
c=100
Multiple inheritance
Class inherits from multiple base classes
A
Example:
#include<iostream>
using namespace std;
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}
OUTPUT:
M=10
N=20
M*n=200
Hierarchical inheritance
The traits of one class may be inherited by
more than one class.
A
Multilevel inheritance
deriving a class from another derived
A
class.
B
Example:
int main()
{
result student1;
//student1 object created
student1.get_no(111);
student1.get_marks(75.0
,59.5);
student1.display();
return 0;
}
OUTPUT:
Roll number: 111
Marks in sub1=75
Marks in sub2=59.5
Total=134.5
Hybrid inheritance
combination of two or more inheritance.
B
Example:
#include<iostream>
using namespace std;
class student
{
protected:
int roll_no;
public:
void get_no(int a)
{
roll_no=a;
}
void put_no(void)
{
cout<<roll_no:<<roll_no;
}
};
void put_score(void)
{
cout<<sports
wt:<<score<<\n;
} };
class result:public
test,public sports
{ float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
put_score();
cout<<total
score:<<total<<\n;
}
A
C
int main()
D
{
result student1;
student1.get_no(1234);
student1.get_marks(27.5,33.
0);
student1.get_score(6.0);
student1.display();
return 0;
}
OUTPUT:
Roll_no:1234
Marks obtained:
Part1=27.5
Part2=33
Sports wt:6
Total score=66.5
Methods of inheritance
Order of execution
Class B:public A
{ };
{ };
class beta
{
float y;
public:
beta(int j)
{
y=j;
cout<<beta initialized;
}
void show_y(void)
{
cout<<y=<<y<<\n;
}
};
class gamma:public
beta,public alpha
{
int m,n;
public:
gamma(int a,float b,int c,int
d):alpha(a),beta(b)
{
m=c;
n=d;
cout<<gamma initialized;
}
void show_mn(void)
{
cout<<m=<<m<<\n
<<n=<<n<<\n;
}
};
int main()
{
gamma g(5,10.75,20,30);
cout<</n;
g.show_x();
g.show_y();
g.show_mn();
return 0;
}
Output:
beta intialized
alpha intialized
Gamma intialized
X=5
Y=10.75
M=20
N=30
Nesting(containership) of
classes
C++ supports yet another way of inheriting properties of one
class into another.This approach takes a view that an object
can be a collection of many other object.
That is, a class can contain objects of other classes as its
members are shown below:
class alpha { ...........};
class beta { ...........};
class gamma {
alpha a;
beta b;
.............
};
All objects of gamma class will contain the objects a & b.This
kind of relationship is called containership or nesting.