Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Inheritance
Creating or deriving a new class using another class as a base is called inheritance in C++. The new class created is called a Derived class and the old class used as a base is called a Base class in C++ inheritance terminology
Inheritance
The derived class will inherit all the features of the base class in C++ inheritance. The derived class can also add its own features, data etc., It can also override some of the features (functions) of the base class, if the function is declared as virtual in base class
Inheritance
C++ inheritance is very similar to a parentchild relationship. When a class is inherited all the functions and data member are inherited, although not all of them will be accessible by the member functions of the derived class. But there are some exceptions to it too
Inheritance
Some of the exceptions to be noted in C++ inheritance are as follows. The constructor and destructor of a base class are not inherited the assignment operator is not inherited the friend functions and friend classes of the base class are also not inherited
Inheritance
There are some points to be remembered about C++ inheritance. The protected and public variables or members of the base class are all accessible in the derived class. But a private member variable not accessible by a derived class
Inheritance
It is a well known fact that the private and protected members are not accessible outside the class. But a derived class is given access to protected members of the base class
Inheritance
Let us see a piece of sample code for C++ inheritance. The sample code considers a class named vehicle with two properties to it, namely color and the number of wheels. A vehicle is a generic term and it can later be extended to any moving vehicles like car, bike, bus etc.,
Inheritance
class vehicle //Sample base class for c++ inheritance protected: char colorname[20]; int number_of_wheels; public: vehicle(); ~vehicle(); void start(); void stop(); void run(); }; {
Inheritance
class Car: public vehicle //Sample derived //class for C++ inheritance { protected: char type_of_fuel; public: Car(); };
Inheritance
The derived class Car will have access to the protected members of the base class. It can also use the functions start, stop and run provided the functionalities remain the same
Inheritance
In case the derived class needs some different functionalities for the same functions start, stop and run, then the base class should implement the concept of virtual functions
Private
Protected Public
Yes
Yes Yes
No
No Yes
Example
# include<iostream.h> class Sample1{ protected: int x; public: Sample1(int xx=0) //constructor {x=xx;} void set( int xx=0) { x=xx;} void display() { cout<<endl<<x=<<x;} };
Example
class Sample2: public Sample1 { protected: int y; public: sample2(int xx=0, int yy=0):Sample1(xx) { y=yy;} void set( int xx=0, int yy=0) { sample1::set(xx); y=yy; } void display() { sample1::display(); cout<<endl<<y=<<y; } };
Example
int main() { sample1 s1(50); sample2 s2(100,200); s1.display(); s2.display(); return 0; }
Example
When we declare an object of the derived class, the compiler executes the constructor of base class first, then the constructor of derived class In the derived class the set() of base class is called with class name and scope resolution operator. Sample1::set(xx); If it were set(xx); It would be treated as recursion If a base class does not contain a default constructorthat is, if the base class contains only constructors that require arguments- then you must provide a constructor for every derived class, even if the derived class does not need a constructor for any other reason
Example
class one //base class { private: int a; protected: int b; public: int c; }; class two:public one // publicly-derived class { public: void function1() { int z; z=a; //error: not accessible z=b; // works z=c; // works } };
Example
Class three: private one //privately-derived class { public: void fucntion2() { int y; y=a; // error: n;ot accessible y=b; // works y=c ; // works } }; void main() { int x; two second; // object of class two x=second a; // error: not accessible x=second.b; // error : not accessible x=second.c; // works txree third; // object of class three x=third.a; // error : not accessible x=third.b;// error: no;t accessible x=third.c;// error not accessible }
Overriding
If a base class contains a function that the derived class should not have, you can create a dummy, or empty, function with the same name in the derived class
Forms of Inheritance
A A B A
Type of Inheritance
Multilevel Inheritance Class A derives Class B, Class B derives Class C and so on. For multilevel inheritance to be meaningful: Data members should be protected Public inheritance should be used
Example
Class A { }; Class B: public A { }; Class C: public B { };
Multiple Inheritance
A class can be derived by more than one classes class A { }; class B { . }; class C: public A, public B { };
Multiple Occurrence of same function Class A { public: void f(); }; Class B { public: void f(); };
Duplicate Inheritance
The StudentEmployee class ends up being a descendant of Person two times, would end up with two copies of each member of Person
Person
Student
Employee
StudentEmployee
Example
#include<iostream.h> class A{ public: void fun(){ cout<<"I am in A"; } }; class B:public A{ }; class C:public A{ }; class D:public B, public C{ }; void main(){ B bobj; C cobj; D dobj; dobj.fun(); }
How to Tackle?
Use the keyword virtual when you define each of the child class. The word virtual indicates that the base class should be used only once. The header of the classes become:
class Student: virtual public Person class Employee: virtual public Person And class StudentEmployee: public Student, public Employee OR class StudentEmployee: virtual public Student, virtual public Employee
Summary
Inheritance promotes reusability by supporting the creation of new classes from existing classes. Variables and Methods from parent classes can be overridden by redefining them in derived classes.