OBJECT ORIENTED PROGRAMMING (OOP) WITH C++
INHERITANCE
AIUB. FALL 2017
Dr. Mahbubul Syeed
Associate Professor, Department. of CS, AIUB
[email protected] www.msyeed.weebly.com
CONTENTS
ü Inheritance (What, why and how)
ü Base class and derived class in relation to inheritance (real life example)
ü What is inherited in inheritance
ü Access control in Inheritance (public, private and protected)
ü Multiple inheritances.
ACCESSING BASE CLASS PUBLIC VARIABLES AND METHODS
#include <iostream> class Car: public Vehicle {
#include <string> int noOfDoors;
using namespace std; public:
Car(int doors, string name){
class Vehicle{ noOfDoors = doors;
string engine; vechicleName = name; // direct access to base class public variable
}
double fuelLevel;
int getDoors (){return noOfDoors; }
public:
};
string vechicleName;
void setFuleLevel(double level){fuelLevel = level;}
class Bike: public Vehicle {
void setEngineConfig(string enginconf){engine = enginconf;} string bikeType;
double getFuelAmount(){return fuelLevel;} public:
string getEngineConfig(){return engine;} void setBikeType(string type){bikeType = type;}
}; string getBikeType(){ return bikeType; }
};
int main(){
Car myCar(5, “Axio”);
myCar.setEngineConfig("1050cc");
myCar.setFuleLevel(50.6);
cout << "My car config: " << endl<< myCar.getDoors() << endl<< myCar.getFuelAmount() << endl<< myCar.getEngineConfig();
}
ACCESSING BASE CLASS PUBLIC VARIABLES AND METHODS
Child / derived class can directly access:
- Base class / parent class public variables.
- Base class / parent class public functions.
ACCESSING BASE CLASS PRIVATE VARIABLES AND METHODS
#include <iostream> class Car: public Vehicle {
#include <string> int noOfDoors;
using namespace std; public:
Car(int doors){ noOfDoors = doors; }
class Vehicle{
private: void setCarDetail(){
//trying to access base class private variable directly
string engine;
engine = "2000cc";
double fuelLevel;
public:
//accessing private variable of base class through public function
void setFuleLevel(double level){fuelLevel = level;} setEngineConfig("2000cc");
void setEngineConfig(string enginconf){engine = enginconf;} }
double getFuelAmount(){return fuelLevel;}
string getEngineConfig(){return engine;} int getDoors (){return noOfDoors; }
}; };
int main(){
Car myCar(5);
myCar.setCarDetail();
myCar.setFuleLevel(60.4);
cout << "My car config: " << endl<< myCar.getDoors() << endl<< myCar.getFuelAmount() << endl<< myCar.getEngineConfig();
}
ACCESSING BASE CLASS PRIVATE VARIABLES AND METHODS
Child / derived class can never directly access:
- Base class / parent class private variables.
- Base class / parent class private functions.
Derived class can only access base class private data members through base class public functions.
ACCESSING BASE CLASS PROTECTED VARIABLES AND METHODS
#include <iostream> class Car: public Vehicle {
#include <string> int noOfDoors;
using namespace std; public:
Car(int doors){
class Vehicle{ noOfDoors = doors;
//accessing protected variable and function of super class directly from sub
private:
class
string engine; fuelLevel = 40.5;
protected: setEngineConfig("1500cc");
double fuelLevel; }
void setEngineConfig(string enginconf){engine = enginconf;} int getDoors (){return noOfDoors; }
public: };
void setFuleLevel(double level){fuelLevel = level;}
double getFuelAmount(){return fuelLevel;}
string getEngineConfig(){return engine;}
};
int main(){
Car myCar(5);
cout << "My car config: " << endl<< myCar.getDoors() << endl<< myCar.getFuelAmount() << endl<< myCar.getEngineConfig();
}
ACCESSING BASE CLASS PROTECTED VARIABLES AND METHODS
Child / derived class can directly access:
- Base class / parent class protected variables directly.
- Base class / parent class protected functions directly.
ACCESSING BASE CLASS PROTECTED VARIABLES AND METHODS
Protected variables can not be accessed outside the class with class object and dot operator!
This is same as private variable!!
#include <iostream> class Car: public Vehicle {
#include <string> int noOfDoors;
using namespace std; public:
Car(int doors){
class Vehicle{ noOfDoors = doors;
//accessing protected variable and function of super class directly from sub
private:
class
string engine; fuelLevel = 40.5;
protected: setEngineConfig("1500cc");
double fuelLevel; }
void setEngineConfig(string enginconf){engine = enginconf;} int getDoors (){return noOfDoors; }
public: };
void setFuleLevel(double level){fuelLevel = level;}
double getFuelAmount(){return fuelLevel;}
string getEngineConfig(){return engine;}
};
int main(){
Car myCar(5);
myCar.setEngineConfig("1500cc"); // cant access outside class with dot operator!!
myCar.fuelLevel= 30.5;
cout << "My car config: " << endl<< myCar.getDoors() << endl<< myCar.getFuelAmount() << endl<< myCar.getEngineConfig();
}
INHERITANCE TABLE
Access public protected private
Same class yes yes yes
Derived classes yes yes no
Outside classes yes no no
TYPES OF INHERITANCE
DERIVED CLASS INHERITS BASE CLASS AS PUBLIC
class DerivedClass : public BaseClass{
};
Base Class Derived Class
Private Private
Inherited as
Protected Protected
Public Public
DERIVED CLASS INHERITS BASE CLASS AS PROTECTED
class DerivedClass : protected BaseClass{
};
Base Class Derived Class
Private Private
Inherited as
Protected Protected
Public Protected
#include <iostream> class Car: protected Vehicle {
#include <string> int noOfDoors;
using namespace std; public:
Car(int doors){
class Vehicle{ noOfDoors = doors;
//accessing protected variable and function of super class directly from sub
private:
class
string engine; fuelLevel = 40.5;
protected: setEngineConfig("1500cc");
double fuelLevel; }
void setEngineConfig(string enginconf){engine = enginconf;} int getDoors (){return noOfDoors; }
public: };
void setFuleLevel(double level){fuelLevel = level;}
double getFuelAmount(){return fuelLevel;}
string getEngineConfig(){return engine;}
};
int main(){
Car myCar(5);
myCar.setEngineConfig("1500cc"); // cant access outside class with dot operator!!
myCar.fuelLevel= 30.5;
cout << "My car config: " << endl
<< myCar.getDoors() << endl
<< myCar.getFuelAmount() << endl
<< myCar.getEngineConfig();
}
CONSTRUCTOR AND DESTRUCTOR CALL SEQUENCE IN
INHERITANCE
#include <iostream> Constructor
using namespace std;
int main(){
class Base{ Derived cDerived;
public: return 0;
int m_id;
}
Base(int id=0)
{
m_id = id; In inheritance:
cout << "Constructing Base\n";
}
Creating derived class object always calls base class constructor first, then calls
int getId() { return m_id; } Its constructor (last)!
};
class Derived: public Base{
public:
double m_cost;
Derived(double cost=0.0) {
m_cost = cost; Constructing Base
cout << "Constructing Derived\n"; Constructing Derived
}
double getCost() const { return m_cost; }
};
#include <iostream> Destructor
using namespace std;
int main(){
class Base{ Derived cDerived;
public: return 0;
int m_id; }
Base(int id=0)
{
m_id = id; In inheritance:
cout << "Constructing Base\n";
}
Destructor is called exactly the reverse of the constructor call!!
~Base(){cout<<"Destructing base \n";}
int getId() { return m_id; }
};
class Derived: public Base{
public:
double m_cost; Constructing Base
Derived(double cost=0.0) { Constructing Derived
m_cost = cost; Destructing derived
cout << "Constructing Derived\n";
} Destructing base
~Derived(){cout<<"Destructing derived \n";}
double getCost() const { return m_cost; }
};
#include<iostream>
using namespace std;
class A
{ What is the output??
public:
A() { cout << " constructing A" << endl; }
~A() { cout << " destructing A" << endl; }
};
class B: public A int main()
{ {
public: cout << "Constructing A: " << endl;
B() { cout << " constructing B" << endl; } A cA;
~B() { cout << " destructing B" << endl; }
}; cout << "Constructing B: " << endl;
B cB;
class C: public B
{ cout << "Constructing C: " << endl;
public: C cC;
C(){ cout << " constructing C" << endl; }
~C() { cout << " destructing C" << endl; } cout << "Constructing D: " << endl;
}; D cD;
}
class D: public C
{
public:
D() { cout << " constructing D" << endl; }
~D() { cout << " destructing D" << endl; }
};
Constructing A:
constructing A
Constructing B:
constructing A
destructing D
constructing B
destructing C
Constructing C:
destructing B
constructing A
destructing A
constructing B
destructing C
constructing C
destructing B
Constructing D:
destructing A
constructing A
destructing B
constructing B
destructing A
constructing C
destructing A
constructing D