C++ Inheritance
This article describes basic concepts of C++ Inheritance mechanism.
Introduction
This article describes basic concepts of C++ Inheritance mechanism.
Inheritance
- Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.
- Inheritance is implemented in C++ through the mechanism of derivation. Derivation allows you to derive a class, called a derived class, from another class, called a base classes
- The class whose members you want to include in your new class is called a base class. Your new class is derived from the base class.
- You can also add new data members and member functions to the derived class.
- When you derive a class, the derived class inherits class members of the base class. You can refer to inherited members (base class members) as if they were members of the derived classes
class A{ //A is the base class
};
class B: public A {
int a; //we can add new data members
void f(); //we can add new member function
}; //B is the derived class
class A{ //A is the base class
};
class B: public A {
int a; //we can add new data members
void f(); //we can add new member function
}; //B is the derived class
class A{ //A is the base class
};
class B: public A {
int a; //we can add new data members
void f(); //we can add new member function
}; //B is the derived class
Multiple Inheritance
class A{ //A is the base class
};
class B: public A {
int a; //we can add new data members
void f(); //we can add new member function
}; //B is the derived class
class A{ //A is the base class
};
class B: public A {
int a; //we can add new data members
void f(); //we can add new member function
}; //B is the derived class
class A{
public:
int a; //ambiguous member
A():a(10){};
};
class B{
public:
int a; //ambiguous members
B():a(20){};
};
class C:public A,public B
{
public:
int a; //overriding ambigous data members
};
;
};
class Derived:public Base
{
public:
int a; //overriding ambigous data members
Derived():a(100){};
};
int main()
{
Derived c1;
cerr << c1.a << ":"<< endl; //accessing derived class member
cerr << c1.Base::a << ":"<< endl; //accessing the base class member
}
;
};
class Derived:public Base
{
public:
int a; //overriding ambigous data members
Derived():a(100){};
};
int main()
{
Derived c1;
cerr << c1.a << ":"<< endl; //accessing derived class member
cerr << c1.Base::a << ":"<< endl; //accessing the base class member
}
class Parent{
public:
int a;
}
class Base:public Parent{
public:
void disp(){cout << a << endl;}
};
//both base and base1 have the same base class
class Base1:public Parent{
public:
void disp(){cout << a << endl;}
};
class Derived:private Base,private Base1
//this will cause error
{
public:
};
int main()
{
Derived d;
d.disp();
}
Virtual Base Class
using namespace std;
class Parent{
public:
int a;
}
//virtual base class
class Base:public virtual Parent{
public:
void disp(){cout << a << endl;}
};
//virtual base class
class Base1:public virtual Parent{
public:
void disp(){cout << a << endl;}
};
//not an error due to one subobject of indirect base class
class Derived:private Base,private Base1
{
public:
};
class Parent{
public:
int a;
}
class Base:public virtual Parent{
public:
void disp(){cout << a << endl;}
};
class Base1:private virtual Parent{
public:
void disp(){cout << a << endl;}
};
//Base provides more access
class Derived:private Base,private Base1
{
public:
};
int main()
{
Derived d;
//It choose base,else this line woul
//give error due to private inheritance
cout << d.a <<endl; //take from Base
}
Ambiguous base classes
Using Keyword Declaration
class Base{
private:
int a;
public:
void disp(){cout << a << endl;}//member disp will be hidden
Base():a(10){};
};
class Derived:protected Base
{
public:
void disp(int a){};//member named disp in derived class
};
int main()
{
Derived d;
d.disp(); //this will give error since disp function is hidden
}
class Base{
private:
int a;
public:
void disp(){cout << a << endl;}
Base():a(10){};
};
class Derived:protected Base
{
public:
//introducing disp function from base to derived class
using Base::disp;
void disp(int a){};
};
int main()
{
Derived d;
d.disp();//because of using declaration this is allowed
//name disp is overloaded with 2 functions
}
class Base{
private:
int a;
public:
void disp(){cout << a << endl;} //function in case class
Base():a(10){};
};
class Derived:protected Base
{
public:
using Base::disp; //using keyword
//function with same name,return type and args in derived class
void disp(){cerr << "DErived" << endl;};
void disp(int a){};
};
int main()
{
Derived d;
d.disp();//will print derived ,no conflicts
}
class Parent{
public:
int a;
};
class Base:public Parent{
using Parent::a;
public:
void disp(){cout << a << endl;}
};
class Base1:public Parent{};
class Derived:public Base,public Base1{
public:
using Parent::a;
void disp(){}; //a is not used hence no error in compilation
};
class Parent{
public:
int a;
};
class Base:public Parent{
using Parent::a;
public:
void disp(){cout << a << endl;}
};
class Base1:public Parent{};
class Derived:public Base,public Base1{
public:
using Parent::a;
void disp(){}; //a is not used hence no error in compilation
};
Pointer Derived Class and Base Class
class Base{
public:
int a;
Base():a(10){};
};
class Derived:public Base
{
public:
int a; //overriding ambigous data members
Derived():a(100){};
};
int main()
{
Derived d1;
Derived *d2=&d1; //
Base *b1;
b1=d2; //convers pointer to base class from derived class
cerr << b1->a << endl;//prints 10 of base class
cerr << d2->a << endl;//prints 100 of derived class
}
//In the above example,chaning the main function
int main()
{
Derived d1;
Derived *d2=&d1; //
Base *b1;
d2=b1; //this line will give an error
}
Inherited member access
Increasing Access
class Base{
private:
int a;
public:
void disp(){cout << a << endl;}
Base():a(10){};
};
class Derived:private Base
{
public:
//disp is inherited as private but access increase by using declaration
//in public access scope
using Base::disp;
};
int main()
{
Derived d;
d.disp();//without using declaration will give error
}
class Base{
private:
int a;
public:
void disp(){cout << a << endl;}
Base():a(10){};
};
class Derived:public Base
{
public:
using Base::a; //this line will give error
};
class Base{
private:
int a;
public:
void disp(){cout << a << endl;}
Base():a(10){};
};
class Derived:public Base
{
private: //this will give an error
using Base::disp;
};