Beruflich Dokumente
Kultur Dokumente
INTRODUCTION
It is an important feature of Object Oriented Programming.
The mechanism by which object of one class acquires the properties of object of some other class is called inheritance. Basically new classes are created and they acquires the properties of existing classes and also adds some new properties of their own.
The existing class is known as the Base class.
INTRODUCTION
The new class is known as the derived class.
The derived class acquires some or all properties from the base class.
A derive class can also inherit properties from more than one class. There are 5 different types of inheritance: Single, multiple, hierarchical, multilevel and hybrid inheritance.
TYPES OF INHERITANCE
Single inheritance: a derived class with only base class. class A{ .
B
}; class D: public A{ ..
};
TYPES OF INHERITANCE
Multiple inheritance: a derived class with more than one base class.
B1 B2
TYPES OF INHERITANCE
class B1{ ..}; class B2{ ..};
TYPES OF INHERITANCE
Hierarchical inheritance: one base class may be inherited by multiple derived classes.
B
D1
D2
D3
TYPES OF INHERITANCE
class B{ ..}; class D1:public B { ..};
class D2:public B {
..}; class D3:public B { ..};
TYPES OF INHERITANCE
Multilevel inheritance: a class is derived from another derived class.
B1
B2
TYPES OF INHERITANCE
class B1{ ..}; class B2:public B1 { ..};
class D:public B2 {
..};
TYPES OF INHERITANCE
Hybrid inheritance: Its an combination of hierarchical, multilevel and multiple.
B
B1
B2
TYPES OF INHERITANCE
class B{ ..}; class B1:public B { ..};
class B2:public B {
..}; class D:public B1, public B2 { ..};
Class B Private: int c; Public: int b; f1(); f2() int d; f3(); class B: public A f4();
int b;
void get_ab(){a=5; b=10;} int get_a() {return a;} void show() {cout<<a<<a;} };
void mul(){c=b*get_a();}
void display() {cout<<a<<get_a(); cout<<b<<b;
cout<<c<<c;
};
B1.show();
B1.display(); B1.b=20; B1.mul(); return 0;}
MAKING A PRIVATE MEMBER INHERITABLE This can be done using a 3rd visibility modifier known as protected
A member declared as protected is accessible by member functions within its class and any class immediately derived from it.
It cannot be accessed by the functions outside these two classes.
Not inherited
Public
class B
Not inheritable;
public
protected
public class X: public D1, protected D2
AMBIGUITY RESOLUTION IN INHERITANCE Case I class M { public: void display(){cout<< class M;} };
class N
{ public: void display(){cout<< class N;} }; class p: public M, public N { public: void display(){M::display();}
};
AMBIGUITY RESOLUTION IN INHERITANCE Case I class p: public M, public N { public: void display(){M::display();} };
AMBIGUITY RESOLUTION IN INHERITANCE Case II class A { public: void display(){cout<< class A;} };
class B: public A
{ public: void display(){cout<< class B;} };
b.A::display();
b.B::display(); return 0; };
B1
B2
Virtual base class ensures that upon inheritance, only one copy of it will be copied.
ABSTRACT CLASS
This is a class that is not used to create objects
CONSTRUCTORS IN DERIVED CLASSES If the base class constructor takes no argument then it is not required to the derived class to have constructor If the base class constructor takes any argument then it is mandatory for the derived class to have constructor with one or more arguments and they passes the arguments to the base class constructor. When both the derived class and base classes contains constructors then base constructor is executed first then the derived class constructor is executed.
CONSTRUCTORS IN DERIVED CLASSES The base classes are constructed in the order in which they appear in the declaration of the derived class. The constructor will be executed in order of inheritance.
CONSTRUCTORS IN DERIVED CLASSES Example: D(int a1, ine a2, float b1, float b2, int d1): A(a1, a2), B(b1, b2) {
d=d1;
}
CONSTRUCTORS IN DERIVED CLASSES class gamma:public beta, public alpha { int m,n;
cout<<x=<<x;}
};
g.show_x();
g.show_y(); g.show_mn(); return 0; }
ASSIGNMENTS
How the constructor of derived class will be written in case of multilevel inheritance?
What is initialization list? How an initialization list is used in constructors of derived class?