1. Introduction
class <DerivedCalssName> : <accessSpecifier> <BaseClassName>
The size of the derived class will always be equall to or greater then base class.
Derived class can only access the public or protected members of the base class. Private data members will be inherited but cannot be used.
Example:
#include<iostream>
using namespace std;
class A
{
int a;
public:
void display()
{
cout<<"This is display from A class"<<endl;
}
};
class B :public A
{
int b;
public:
void display()
{
cout<<"This is display from B class"<<endl;
}
};
int main()
{
A a1;
a1.display(); // "A" class display()
B b1;
b1.display(); // "B" class display function.
cout<<"Size of object of A class " << sizeof(a1)<<endl;
cout<<"Size of object of B class " << sizeof(b1)<<endl;
}
Output:
This is display from A class
This is display from B class
Size of object of A class 4
Size of object of B class 8
2. Deriving by different access specifiers:
There are 3 types of access specifiers that can be used to derive.
1. public
2. private
3. protected
Deriving using public access specifier:
1. Public members of base class will be public members of derived class.
2. Protected members of base class will be protected members of derived class.
3. Private members of the base class are not accessable to derived class. Hence are not inherited.
Example:
class DerivedClass : public BaseClass
Deriving using protected access specifier:
1. Public members of base class will be protected members of derived class.
2. Protected members of base class will be protected members of derived class.
3. Private members of the base class are not accessable to derived class. Hence are not inherited.
Example:
class DerivedClass : protected BaseClass
Deriving using private access specifier:
1. Public members of base class will be private members of derived class.
2. Protected members of base class will be private members of derived class.
3. Private members of the base class are not accessable to derived class. Hence are not inherited.
Example:
class DerivedClass : private BaseClass
3. C++ Base class Pointer and Derived class pointer.
#include<iostream>
using namespace std;
class Base
{
public:
int a;
void display()
{
cout<<"This is base class show()"<<endl;
}
};
class Derived : public Base
{
public:
int b;
void display()
{
cout<<"This is derived class show()"<<endl;
}
};
int main()
{
Base *b1;
Derived der;
b1 = &der;
b1->display();
b1->a;
// b1->b; // error member b is not found in Base class
return 0;
}
Output:
This is base class show()
Derived Class Pointer:
The derived class pointer cannot hold the address of base class object. Because, the derived class pointer is supposed to access all the members of both base class and derived class.
By making derived class pointer to hold the address of base class, it cannot access the derived class members. Hence the compiler will throw an error.
Example:
int main()
{
Base b1;
Derived *der;
der= &b1; //error
}
4. Virtual Base Class

error: non-static member 'a' found in multiple base-class subobjects of type 'A':
class D -> class B -> class A
class D -> class C -> class A
cout<<"The value of a "<<a;
^
note: member found by ambiguous name lookup
int a;
^
1 error generated.
To make the compiler to inherit only once, we should make base class as “virtual” when ever it is being inherited. By making a Class virtual, compiler will take necessary steps to make sure that all the classes are inherited only once.
“virtual” is the keyword used to declare a virtual class.
/*
* File : virtual_base_class.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;
class A
{
public:
int a;
};
class B: virtual public A
{
public:
int b;
};
class C: virtual public A
{
public:
int c;
};
class D: public B, C
{
public:
int d;
void display()
{
cout<<"The value of a "<<a;
}
};
int main()
{
D d1;
}
5. Constructor and Destructor calls in Inheritance
/*
* File : constructor_destructor_in_inheritance.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"Constructor of A"<<endl;
}
~A()
{
cout<<"Destructor of A"<<endl;
}
};
class B: public A
{
public:
B()
{
cout<<"Constructor of B"<<endl;
}
~B()
{
cout<<"Destructor of B"<<endl;
}
};
class C: public B
{
public:
C()
{
cout<<"Constructor of C"<<endl;
}
~C()
{
cout<<"Destructor of C"<<endl;
}
};
class D: public C
{
public:
D()
{
cout<<"Constructor of D"<<endl;
}
~D()
{
cout<<"Destructor of D"<<endl;
}
};
int main()
{
D d1;
}
Constructor of A
Constructor of B
Constructor of C
Constructor of D
Destructor of D
Destructor of C
Destructor of B
Destructor of A
6. Constructor and Destructor calls in Inheritance example
/*
* File : constructor_destructor_in_inheritance.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"Constructor of A"<<endl;
}
~A()
{
cout<<"Destructor of A"<<endl;
}
};
class B
{
public:
B()
{
cout<<"Constructor of B"<<endl;
}
~B()
{
cout<<"Destructor of B"<<endl;
}
};
class C: public B, A
{
public:
C()
{
cout<<"Constructor of C"<<endl;
}
~C()
{
cout<<"Destructor of C"<<endl;
}
};
int main()
{
C c1;
}
Constructor of B
Constructor of A
Constructor of C
Destructor of C
Destructor of A
Destructor of B
7. Pointers and Inheritance
/*
* File : pointers_inheritance.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
A()
{
a = 10;
}
};
class B : public A
{
public:
int b;
B()
{
b = 20;
}
};
int main()
{
B b1;
//pointer to the derived class data member
int *ptr = &b1.b;
cout<<"The value of variable b is "<< *(ptr)<<endl;
//access the base class private variable
ptr --;
cout<<"The value of variable a is "<< *(ptr)<<endl;
}
Output:
The value of variable b is 20
The value of variable a is 10