class <derived_class_name> : <access-specifier>
<base_class_name>
{
//body
}
Here
// C++ Implementation to show that a derived class doesn't inherit access to private data members.
// However, it does inherit a full parent object.
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
The below table summarizes the above three modes and shows the access specifier of the members of the base class in the subclass when derived in public, protected and private modes:
Example code
#include<iostream>
using namespace std;
class Beverage
{
public :
void drinks()
{
cout << "All drinks are available" << endl;
}
};
class Coffee : public Beverage
{
public:
void mocha()
{
cout << "Mocha tastes the best";
}
};
int main()
{
Coffee c;
c.drinks();
c.mocha(); // using sub class we are accessing member function of base class.
return 0;
}
Example code ↓
#include <<ostream>
using namespace std;
class A
{
public:
int x;
A()
{
x = 10;
}
};
class B : public A
{
public:
int y;
B()
{
y = 20;
}
};
class C : public B
{
public:
int sum;
C()
{
sum = x + y;
}
};
int main()
{
C c_one;
cout << "The sum is = " << c_one.sum; // by creating derived class object the constructor of parent class are called.
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int x;
A()
{
x = 10;
}
};
class B
{
public:
int y;
B()
{
y = 20;
}
};
class C : public A, public B
{
public:
int sum;
C()
{
sum = x + y;
}
};
int main()
{
C c_one;
cout << "The sum is = " << c_one.sum;
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int x, y, z;
A()
{
x = 10;
y = 10;
z = 10;
}
};
class B : public A
{
public:
int sum;
B()
{
sum = x + y + z;
}
};
class C : public A
{
public:
int mult;
C()
{
mult = x * y * z;
}
};
int main()
{
B b_one;
C c_one;
cout << "The sum is = " << b_one.sum << endl;
cout << "The multiplication is = " << c_one.mult;
return 0;
}
#include <iostream>
using namespace std;
class A
{
public:
int x, y;
A()
{
x = 10;
y = 10;
}
};
class B : public A
{
public:
int sum;
B()
{
sum = x + y;
}
};
class C
{
public:
int z;
C()
{
z = 20;
}
};
class D : public B, public C
{
public:
int total_sum;
D()
{
total_sum = sum + z;
}
};
int main()
{
D d_one;
cout << "The total sum is = " << d_one.total_sum << endl;
return 0;
}
There are two ways to avoid this ambiguity:
// C++ program demonstrating ambiguity in Multipath Inheritance
#include <iostream>
using namespace std;
class ClassA {
public:
int a;
};
class ClassB : public ClassA {
public:
int b;
};
class ClassC : public ClassA {
public:
int c;
};
class ClassD : public ClassB, public ClassC {
public:
int d;
};
int main()
{
ClassD obj;
// obj.a = 10; // Statement 1, Error
// obj.a = 100; // Statement 2, Error
obj.ClassB::a = 10; // Statement 3
obj.ClassC::a = 100; // Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout << " a from ClassB : " << obj.ClassB::a;
cout << "\n a from ClassC : " << obj.ClassC::a;
cout << "\n b : " << obj.b;
cout << "\n c : " << obj.c;
cout << "\n d : " << obj.d << '\n';
}
Output ↓
obj.ClassB::a = 10;
abj.ClassC:: a = 100;
#include<iostream>
class ClassA
{
public:
int a;
};
class ClassB : virtual public ClassA
{
public:
int b;
};
class ClassC : virtual public ClassA
{
public:
int c;
};
class ClassD : public ClassB, public ClassC
{
public:
int d;
};
int main()
{
ClassD obj;
obj.a = 10; // Statement 3
obj.a = 100; // Statement 4
obj.b = 20;
obj.c = 30;
obj.d = 40;
cout << "\n a : " << obj.a;
cout << "\n b : " << obj.b;
cout << "\n c : " << obj.c;
cout << "\n d : " << obj.d << '\n';
}
Output ↓
a : 100
b : 20
c : 30
d : 40
Reference ↓