Inheritance⚓︎
Basic Inheritance⚓︎
For?
Inherit Attributes
and Methods
from Base class (parent) to Derived class (child).
Grammar?
Use the :
symbol.
Let's do it!
Problem: Let the Car
class (child) inherits the Attributes and Methods from the Vehicle
class (parent):
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
//Base class
class Vehicle{
public:
Vehicle(){}
~Vehicle(){}
public:
string m_brand = "Ford";
public:
string Honk(){
return "Tu, tu! \n";
}
};
//Derived class
class Car: public Vehicle{
public:
string m_model = "Tesla";
};
int main(){
Car newCar;
assert (newCar.Honk() == "Tu, tu! \n");
assert (newCar.m_brand == "Ford"); //cout << newCar.brand << endl;
assert (newCar.m_model == "Tesla"); //cout << newCar. model << endl;
}
Multiple Inheritance⚓︎
Let's do it!
Problem: Let the Son
class (child) inherits the Attributes and Methods from both the Mom
and Dad
class (parents):
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
//Base class
class Mom{
public:
int m_age = 32;
public:
string Speak(){
return "Mom is me!";
}
};
class Dad{
public:
int m_age = 44;
public:
string Say(){
return "Dad is me!";
}
};
//Derived class
class Son: public Mom, public Dad{
public:
int m_age = 5;
public:
string Say(){
return "I am son!";
}
};
int main(){
Son ason;
assert (ason.Speak() == "Mom is me!");
assert (ason.Say() == "I am son!");
assert (ason.m_age == 5);
}
Inheritance Access⚓︎
What is access specifiers
for methods?
Access specifiers define how the members (attributes and methods) of a class can be accessed.
There are three access specifiers: public
, private
, protected
.
-
public
: members are accessible from outside the class. -
private
: members cannot be accessed (or viewed) from outside the class (but is accessable with in the class). -
protected
: members cannot be accessed from outside the class. However, they can be accessed in inherited classes.
Let's do it!
Problem: Let the Programmer
class (child) inherits the Attributes and Methods from both the Employee
class (parents) in a safe way:
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
//Base class
class Employee{
protected:
int m_salary = 32;
};
//Derived class
class Programmer: public Employee{
public:
int m_bonus = 44;
public:
void setSalary(int s){
m_salary = s;
}
int getSalary(){
return m_salary;
}
};
int main(){
Programmer Mark;
Mark.setSalary(50000);
assert (Mark.getSalary() == 50000);
Mark.m_salary = 90000; //error: ‘int Employee::m_salary’ is protected within this context
}
Public
, Private
, and Protected
Inheritances⚓︎
What is access specifiers
for inheritances?
-
public inheritances
: Basic class'spublic
andprotected
members remain the original status in the derived class. -
private inheritances
: Basic class'spublic
andprotected
members become private in the derived class. -
protected inheritances
: Basic class'spublic
andprotected
members become protected in the derived class. -
Note:
private
members of the base class are inaccessible to the derived class.
Let's do it!
Problem: illustrate all the inheritances method:
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived: public Base {
// x is public
// y is protected
// z is not accessible from PublicDerived
};
class ProtectedDerived: protected Base {
// x is protected
// y is protected
// z is not accessible from ProtectedDerived
};
class PrivateDerived: private Base {
// x is private
// y is private
// z is not accessible from PrivateDerived
};
Let's do it!
Problem 1: C++ public
Inheritance:
// C++ program to demonstrate the working of public inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PublicDerived : public Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
};
int main() {
PublicDerived object1;
cout << "Private = " << object1.getPVT() << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.pub << endl;
return 0;
}
Let's do it!
Problem 2: C++ protected
Inheritance:
// C++ program to demonstrate the working of protected inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class ProtectedDerived : protected Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access public member from Base
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Let's do it!
Problem 3: C++ private
Inheritance:
// C++ program to demonstrate the working of private inheritance
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
// function to access private member
int getPVT() {
return pvt;
}
};
class PrivateDerived : private Base {
public:
// function to access protected member from Base
int getProt() {
return prot;
}
// function to access private member
int getPub() {
return pub;
}
};
int main() {
PrivateDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}