KEMBAR78
C++ L10-Inheritance | PDF
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14 
C++ 
Programming Language 
L10-INheritance
Inheritance
Introduction to Inheritance 
•Two common types: 
–“has –a ” 
•Composition (as we have learned) 
–Object containing one or more objects of other classes as members. 
»A car has a steering wheel 
–“Is –a ” 
•Inheritance 
–Derived class object treated as base class objects 
»A car is a vehicle 
»Vehicle properties / behaviors also car properties / behaviors
Inheritance 
Computer 
Computer 
Keyboard 
PC 
“has –a ” 
“ is –a ”
Inheritance, “ is –a ” Relationship 
The link between classes in an “is –a ” relationship comes from the fact that the subclasses share all the attributes and behaviors found in the supper class, plus more! 
Computer 
PC 
“ is –a ”
Inheritance, “ is –a ” Relationship 
Computer 
PC 
“ is –a ”
Inheritance, “ is –a ” Relationship 
Vehicle 
Car 
Bicycle 
Motorcycle 
“ is –a ”
Inheritance, “ is –a ” Relationship 
Vehicle 
Car 
Boat 
wheeled 
Bicycle 
Two-doors 
four-doors
Inheritance 
•Let’s have the following relationship 
–Student and instructor (teacher) 
•Fields: 
–Name, address, phone 
•Now, let’s build a class called person
Inheritance 
•Now, we think that we can create the following class and carry on 
•But we have the idea? 
–Inheritance! 
classStudent 
{ 
private: 
person UniversityStudent; 
intStuID; 
intStuRate; 
}; 
classInstructor 
{ 
private: 
person UniversityInstructor; 
intInsID; 
intInsSalary; 
};
Advantages of Inheritance 
Person 
Person 
Student 
Instructor 
“has –a ” relationship
Advantages of Inheritance 
Person 
Student 
Instructor 
“has –a ” relationship 
ID, StuRate 
Person 
ID, InsSalary
Advantages of Inheritance 
Person 
Person 
Student 
Instructor 
“ is –a ” 
“ is –a ”
Advantages of Inheritance 
Person 
Student 
Instructor 
“ is –a ”
Advantages of Inheritance 
•What we do now, is that 
–We create a “person” class then 
•Inherit from it! 
•What to inherit fromto? 
–We Inherit class person to class 
»Student 
»Instructor 
–And then, we carry on and add for each class of them their unique features! (ID, StuRate, StuSalaryetc)
Advantages of Inheritance 
•Advantages of inheritance: 
–Saving time 
–After saving time building the first part of the solution 
•We can concentrate on building more complex materials up ahead 
–Can extend or revise (edit) the parent class without corrupting the existing parent class features 
–Absorb existing parent class’s data and behaviors 
•All public, protected member variables in the main class are accessible from the derived class 
•All behaviors inherited too 
–Enhance with new capabilities 
•Customizing 
•Additional behaviors
Thinking Inheritance 
•C++ Usability 
•Creating a “Derived class” from “Base Class” * 
•Base Class usually represent: 
–Larger set of objects than derived class 
–Characteristics that are shared between all of the derived classes 
•Derived classes usually represent more specialized group of objects 
•Sometimes 
–Derived class inherit data members and data functions it does doesn’t need or should shouldn’t not have 
______________________________________ 
*Base Class: also called Parent class, Super class, Ancestor 
*Derived Class: also called Child Class, Sub Class, Descendant
Class Hierarchy types 
•Direct Indirect: 
–Direct class access 
•Inherited explicitly (one level up) 
–Indirect class access 
•(one or two levels up hierarchy) 
•Single Multiple: 
–Single Inheritance 
•From one base class 
–Multiple Inheritance 
•From multiple base class (only in C++, not in C++.NET, C#, Java, etc.)
Inheritance Examples
Inheritance
Inheritance
Inheritance 
Base class 
Derived classes 
Student 
GraduateStudent, UndergraduateStudent 
Shape 
Circle, Triangle, Rectangle, Sphere, Cube 
Loan 
CarLoan, HomeImprovementLoan, MortgageLoan 
Employee 
Faculty, Staff 
Account 
CheckingAccount, SavingsAccount
Types of Inheritance
Types of Inheritance
Creating a Derived Class
Creating a Derived Class 
•Notes: 
–When inheriting with public specifier 
•friend Functions are not inherited! 
–When inheriting with private specifier 
•Non of the public methods of the base class are accessible to the users of the derived class
Creating a Derived Class 
#include<iostream> 
usingnamespace::std; 
classBaseClass 
{ 
}; 
classDerivedClass: privateBaseClass 
{ 
}; 
#include<iostream> 
usingnamespace::std; 
classBaseClass 
{ 
}; 
classDerivedClass: publicBaseClass 
{ 
}; 
#include<iostream> 
usingnamespace::std; 
classBaseClass 
{ 
}; 
classDerivedClass: protectedBaseClass 
{ 
}; 
#include<iostream> 
usingnamespace::std; 
classBaseClass 
{ 
}; 
classDerivedClass:BaseClass 
{ 
}; 
When ommiting private, public or protected then the default is private
Creating a Derived Class 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
MyCar.SetWeight(5);// member function in base class 
MyCar.SetSpeed(10); 
cout << "Printing"<< endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
3 
2 
Printing 
10 
5 
Press any key to continue
Live Demowith Lord of the Rings(Humans and Goblins inherit from Creatures)
Don’t forgetfriendfunctions are not inherited!
Examples
Creating a Derived Class 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
cout << "After 1st change"<< endl; 
MyCar.SetWeight(5); 
MyCar.SetSpeed(10); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
foo(MyCar); 
cout << "After 2nd change"<< endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
3 
2 
After 1st change 
10 
5 
After 2nd change 
9 
5 
Press any key to continue
Creating a Derived Class 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car C); 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car C) 
{C.speed = 9; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
cout << "After 1st change"<< endl; 
MyCar.SetWeight(5); 
MyCar.SetSpeed(10); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
foo(MyCar); 
cout << "After 2nd change"<< endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
3 
2 
After 1st change 
10 
5 
After 2nd change 
10 
5 
Press any key to continue
Creating a Derived Class 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car C); 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car C) 
{C.weight= 9; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
cout << "After 1st change"<< endl; 
MyCar.SetWeight(5); 
MyCar.SetSpeed(10); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
foo(MyCar); 
cout << "After 2nd change"<< endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
Compiler error, function foo must not access private data members of Vehicle
Creating a Derived Class 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight (){returnweight; } 
voidSetWeight (intx ){weight = x;}; 
inttempWeight; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt); speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
MyCar.tempWeight = 5; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
3 
2 
3 
2 
Press any key to continue
Creating a Derived Class 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight (){returnweight; } 
voidSetWeight (intx ){weight = x;}; 
inttempWeight; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt); speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9; C.TempWeight = 10; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
cout << MyCar.tempWeight << endl; 
MyCar.tempWeight = 5; 
foo(MyCar); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
cout << MyCar.tempWeight << endl; 
} 
3 
2 
-858993460 
9 
2 
10 
Press any key to continue
protectedMembers
protectedMembers 
•Base class protected members 
–Intermediate level of protection between public and private 
–Protected data members are accessible through their own class and their own class’s derived classes 
•Accessible to 
–Base class members(like public) 
–Base class friends(like public, private) 
–Derived class members (like public) 
–Derived class friends(like public) 
•BUT NO ONE using base class directly can access them directly (like private)
protectedMembers 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight (){returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt); speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidPrint() const{ cout << speed << "n"<< weight << endl; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
MyCar.Print(); 
} 
Compile error Car can’t access private data membes declared in its base class
protectedMembers 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight (){returnweight; } 
voidSetWeight (intx ){weight = x;}; 
protected: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt); speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidPrint() const{ cout << speed << "n"<< weight << endl; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
MyCar.Print(); 
} 
3 
2
protectedMembers 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight (){returnweight; } 
voidSetWeight (intx ){weight = x;}; 
protected: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt); speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidPrint() const{ cout << speed << "n"<< weight << endl; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
MyCar.weight = 3; 
cout << MyCar.weight << endl; 
} 
Compiler error, can’t access protected data members directly from outside the class
protectedMembers 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight (){returnweight; } 
voidSetWeight (intx ){weight = x;}; 
protected: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){weight = wt; speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidPrint() const{ cout << speed << "n"<< weight << endl; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
MyCar.Print(); 
} 
3 
2
protectedMembers 
•Problems of protected 
–No validity checking between derived class and the base one 
–Derived class can assign illegal values to protected members (since they can access them directly.) 
–Implementation dependent 
•Fragile (brittle) software
protectedMembers 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
voidGetName1() const{ cout << "I'm a vehicle "<< endl; } 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidGetName2() const{ cout << "I'm a car "<< endl; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
MyCar.GetName2(); 
} 
I'm a car 
Press any key to continue
protectedMembers 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
voidGetName1() const{ cout << "I'm a vehicle "<< endl; } 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidGetName2() const{ cout << "I'm a car "<< endl; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle MyVehicle; 
MyVehicle.GetName1(); 
Car MyCar(2,3); 
MyCar.GetName2(); 
} 
I'm a vehicle 
I'm a car 
Press any key to continue
protectedMembers 
classVehicle 
{ 
public: 
Vehicle (){weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
voidOverFun(){cout<< "In Vehicle class n";} 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
voidOverFun() 
{ 
cout << "In car class"<< endl; Vehicle::OverFun(); 
// overriding here with:: 
} 
private:intspeed;}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle MyVehicle; 
Car MyCar(2,3); 
cout << "Printing with Vehicle:"<< endl; 
MyVehicle.OverFun(); 
cout << "Printing with car:"<< endl; 
MyCar.OverFun(); 
} 
Printing with Vehicle: 
In Vehicle class 
Printing with car: 
In car class 
In Vehicle class 
Press any key to continue
What You Can’t Inherit
What You Can’t Inherit 
•The following never ever can be inherited 
–Constructors 
–Destructors 
–Friends 
–Static data members 
–Static member functions 
–new operator 
–“=“ operator
Overriding Base Class Behavior
Overriding Base Class Behavior 
•What’s overriding overloading? 
–Any child class function with the same name and same argument list as the parent 
•overrides the parent function 
–Any child class with the same name, yet with different argument list as for the parent 
•overloads the parent function
Constructors and Inheritance
Constructors and Inheritance 
•When instantiate a derived class, a constructor for its base class is called first followed by the derived class constructor. 
•Chains of constructors calls 
–Base class constructor called last, but 
–First to finish executing 
•Example: Point Circle Cylinder 
–Point constructor called last, but 
–Point constructor body finishes executing first
Constructors and Inheritance 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle MyVehicle; 
} 
I'm a vehicle! 
Press any key to continue
Constructors and Inheritance 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar; 
} 
I'm a vehicle! 
I'm a car! 
Press any key to continue
Constructors and Inheritance 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(); 
} 
Press any key to continue
Constructors and Inheritance 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle My(); 
} 
Press any key to continue
Constructors and Inheritance 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle My(); 
My.GetWeight(); 
} 
Compiler error
Constructors and Inheritance 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {Vehicle::Vehicle(); returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar; 
cout << MyCar.GetSpeed() << endl; 
} 
I'm a vehicle! 
I'm a car! 
I'm a vehicle! 
0 
Press any key to continue
Constructors and Inheritance 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){cout << "I'm a vehicle!"<< endl; weight += wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {Vehicle::Vehicle(5); returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar; 
cout << MyCar.GetWeight() << endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
I'm a vehicle! 
I'm a car! 
0 
I'm a vehicle! 
0 
I'm a vehicle! 
0 
0 
Press any key to continue
Constructors and Inheritance 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){cout << "I'm a vehicle!"<< endl; weight += wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {Vehicle::Vehicle(5); returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar; 
cout << MyCar.GetWeight() << endl; 
cout << MyCar.GetSpeed() << endl; 
MyCar.SetWeight(4); 
cout << MyCar.GetWeight() << endl; 
} 
I'm a vehicle! 
I'm a car! 
0 
I'm a vehicle! 
0 
4 
Press any key to continue
Constructors and Inheritance 
classVehicle 
{ 
public: 
Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } 
Vehicle (intwt){cout << "I'm a vehicle!"<< endl; weight += wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(){ cout << "I'm a car!"<< endl; speed = 0; } 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {Vehicle::Vehicle(5); returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar; 
cout << MyCar.GetWeight() << endl; 
cout << MyCar.GetSpeed() << endl; 
MyCar.SetWeight(4); 
cout << MyCar.GetWeight() << endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
I'm a vehicle! 
I'm a car! 
0 
I'm a vehicle! 
0 
4 
I'm a vehicle! 
0 
4 
Press any key to continue
Constructors and Inheritance 
classVehicle 
{ 
public: 
Vehicle (){ weight = 0; } 
Vehicle (intwt){ weight = wt; }; 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(intwt, intsp){SetWeight(wt);speed = sp; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar (2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
3 
2 
Press any key to continue
Code Cracking
Code Cracking 
classVehicle 
{ 
public: 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
Compiler error. We didn’t define the 0 parameter Vehicle constructor and but we used it in the (0 parameter constructor of the) derived class
Code Cracking 
classVehicle 
{ 
public: 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
The derived class implicitly call the base class constructor!
Code Cracking 
classVehicle 
{ 
public: 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
The compiler looks for a matching non- parameter base class constructor but can’t find one!
Code Cracking 
classVehicle 
{ 
public: 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
To solve this, 2 ways: 
1.define an non-parameter base class constructor. 
2.call the parametric base class constructor (the one with parameters) and make an explicit call for it from the derived class.
Code Cracking 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: private Vehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
Error1error C2247: 'Vehicle::GetWeight' not accessible because 'Car' uses 'private' to inherit from 'Vehicle'c:userszmeedocumentsvisual studio 2008projectsexamexamexam.cpp11Exam 
Error2error C2247: 'Vehicle::SetWeight' not accessible because 'Car' uses 'private' to inherit from 'Vehicle'c:userszmeedocumentsvisual studio 2008projectsexamexamexam.cpp13Exam
Code Cracking 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{weight = wt; speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
Compiler error. Can’t access private data member of the base class directly
Creating a Derived ClassAs we have seen, derived class can affect state changes in private base-class members. BUT ONLY THROUGH, Non-private functions provided in the base class and inherited into the derived class.
Code Cracking 
classVehicle 
{ 
public: 
friendvoidfoo(Vehicle &V); 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Vehicle &V) 
{V.weight = 9; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle V; 
Car MyCar(2,3); 
MyCar.SetWeight(5);// member function in base class 
MyCar.SetSpeed(10); 
MyCar.foo(V); 
cout << "Printing"<< endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
Printing 
10 
5 
Press any key to continue
Code Cracking 
classVehicle 
{ 
public: 
friendvoidfoo(Vehicle &V); 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Vehicle &V) 
{V.weight = 9; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle V; 
Car MyCar(2,3); 
MyCar.SetWeight(5);// member function in base class 
MyCar.SetSpeed(10); 
foo(V); 
cout << "Printing"<< endl; 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
Compiler error. foo is not a member functions in Car. It’s not inherited!
Constructors and Destructors
Constructors and Destructors 
•Chain of destructors calls 
–Reverse order of constructor chain 
–Destructor of the derived class called first 
–Destructor of the next class up hierarchy called next 
•Continue up hierarchy until final base class is reached 
•After final base class destructor, object is removed from memory
Constructors and Destructors 
classVehicle 
{ 
public: 
Vehicle (){ weight = 0; } 
Vehicle (intwt){ weight = wt; cout <<"Vehicle class "<< weight << endl;}; 
~Vehicle(){cout << "Destructing vehicle class for "<< weight << endl; } 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(intwt, intsp):Vehicle(wt){speed = sp; cout <<"Car class for " 
<< speed << "-"<< GetWeight() << endl; } 
~Car(){cout << "Destructing car class for "<< speed << "-" << GetWeight() << endl; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Vehicle V1; 
{ 
Vehicle V2; 
Car MyCar1 (2,3); 
} 
Car MyCar2 (2,3); 
Car MyCar3 (2,3); 
Vehicle V3; 
} 
Vehicle class 2 
Car class for 3-2 
Destructing car class for 3-2 
Destructing vehicle class for 2 
Destructing vehicle class for 0 
Vehicle class 2 
Car class for 3-2 
Vehicle class 2 
Car class for 3-2 
Destructing vehicle class for 0 
Destructing car class for 3-2 
Destructing vehicle class for 2 
Destructing car class for 3-2 
Destructing vehicle class for 2 
Destructing vehicle class for 0 
Press any key to continue
Constructors and Destructors 
classVehicle 
{ 
public: 
Vehicle (){ weight = 0; } 
Vehicle (intwt){ weight = wt; cout <<"Vehicle class "<< weight << endl;}; 
~Vehicle(){cout << "Destructing vehicle class for "<< weight << endl; } 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car(intwt, intsp):Vehicle(wt){speed = sp; cout <<"Car class for " 
<< speed << "-"<< GetWeight() << endl; } 
~Car(){cout << "Destructing car class for "<< speed << "-" << weight << endl; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{C.speed = 9;} 
Compiler error, accessing private data member of vehicle
Multiple Inheritance
Single VS Multiple Inheritance
Single VS Multiple Inheritance
Multiple Inheritance 
•Looks like the single inheritance 
•Except! 
–There’s multiple base classes 
–Separated by commas 
–Individually can be declared as public, protected or private 
•Default is private 
–Inherited member variables are accessible according to the rules of single inheritance 
•Name conflicts 
–Can result in a member of a base class being hidden by the member of the derived class with the same name.
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
OptionList(){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
Window(){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicOptionList, publicWindow 
{ 
public: 
Menu(){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M; 
} 
OptionList constructor 
Window constructor 
Menu constructor 
Menu destructor 
Window destructor 
OptionList destructor 
Press any key to continue
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
OptionList(){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
Window(){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M; 
} 
Window constructor 
OptionList constructor 
Menu constructor 
Menu destructor 
OptionList destructor 
Window destructor 
Press any key to continue 
The constructors runs in order of declaration
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
OptionList(){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
Window(){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M(); 
} 
Press any key to continue
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
//OptionList(){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
//Window(){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M; 
} 
Menu constructor 
Menu destructor 
OptionList destructor 
Window destructor 
Press any key to continue
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
//OptionList(){cout << "OptionList constructor n";}; 
OptionList(intx){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
//Window(){cout << "Window constructor n";}; 
Window(inty){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
Compiler error
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
//OptionList(){cout << "OptionList constructor n";}; 
OptionList(intx){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
//Window(){cout << "Window constructor n";}; 
Window(inty){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(intn, intm):Window(n),OptionList(m){cout<<"Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M; 
} 
Window constructor 
OptionList constructor 
Menu constructor 
Menu destructor 
OptionList destructor 
Window destructor 
Press any key to continue
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
//OptionList(){cout << "OptionList constructor n";}; 
OptionList(intx){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
}; 
classWindow 
{ 
public: 
//Window(){cout << "Window constructor n";}; 
Window(inty){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(intn, intm):OptionList(m),Window(n){cout<<"Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M; 
} 
Window constructor 
OptionList constructor 
Menu constructor 
Menu destructor 
OptionList destructor 
Window destructor 
Press any key to continue
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
OptionList(intx){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
voidHighLighting(){cout << "In OptionList n";}; 
}; 
classWindow 
{ 
public: 
Window(inty){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
voidHighLighting(){cout << "In Windown";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(intn, intm):OptionList(n), Window(m){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M(2,3); 
} 
Compile and run
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
OptionList(intx){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
voidHighLighting(){cout << "In OptionList n";}; 
}; 
classWindow 
{ 
public: 
Window(inty){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
voidHighLighting(){cout << "In Windown";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(intn, intm):OptionList(n), Window(m){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M(2,3); 
M.HighLighting(); 
} 
Now, it’s a compiler error
Multiple Inheritance 
#include<iostream> 
usingnamespace::std; 
classOptionList 
{ 
public: 
OptionList(intx){cout << "OptionList constructor n";}; 
~OptionList(){cout << "OptionList destructor n";}; 
voidHighLighting(){cout << "In OptionList n";}; 
}; 
classWindow 
{ 
public: 
Window(inty){cout << "Window constructor n";}; 
~Window(){cout << "Window destructor n";}; 
voidHighLighting(){cout << "In Windown";}; 
}; 
classMenu: publicWindow, publicOptionList 
{ 
public: 
Menu(intn, intm):OptionList(n), Window(m){cout << "Menu constructor n";}; 
~Menu(){cout << "Menu destructor n";}; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Menu M(2,3); 
M.Window::HighLighting(); 
M.OptionList::HighLighting(); 
} 
Window constructor 
OptionList constructor 
Menu constructor 
In Window 
In OptionList 
Menu destructor 
OptionList destructor 
Window destructor 
Press any key to continue
Float, double, long double 
C++ data types 
Structured 
Simple 
Address 
Pointer 
Reference 
enum 
Floating 
Array 
Struct 
Union 
Class 
Char, Short, int, long, bool 
Integral
Nested ClassesClassesthat are defined inside another classes are called nested classesIn the public, private or protected sectionSuch a nested class can be considered a member of the outer class
Nested classes 
•Visibility 
–In public section: 
•Visible outside the outer class 
–In private section: 
•Visible only for the member of the outer class 
–In protected section: 
•Subclasses, derived from the outer class
Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst() 
{returnxFirst; } 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond() 
{returnxSecond 
} 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
Class FirstWithin 
Visibility 
Both outside and inside 
Member functions are globally visible 
Intvariable is only visible to the class FirstWithin 
Neither Surroundnor SecondWithincan access the variable of the class FirstWithin directly
Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst() 
{returnxFirst; } 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond() 
{returnxSecond 
} 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
Class FirstWithin 
Visibility 
Both outside and inside 
Member functions are globally visible 
Intvariable is only visible to the class FirstWithin 
Neither Surroundnor SecondWithincan access the variable of the class FirstWithin directly 
Class SecondWith 
Visibility 
Just inside surround 
Member functions can only be reached by the members of class SecondWithinonly 
Intvariable is only visible to the class SecondWithin 
Neither Surroundnor FirstWithincan access the variable of the class SecondWithindirectly
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
intSurround::FirstWithin::GetVarFirst() 
{returnxFirst; } 
intSurround::SecondWithin::GetVarSecond() 
{returnxSecond; } 
Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst() 
{returnxFirst; } 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond() 
{returnxSecond 
} 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
Note how we define the 
member functions here
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
intSurround::FirstWithin::GetVarFirst() 
{returnxFirst; } 
intSurround::SecondWithin::GetVarSecond() 
{returnxSecond; } 
Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst() 
{returnxFirst; } 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond() 
{returnxSecond 
} 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
#include<iostream> 
#include"MyFile.h" 
usingnamespace::std; 
voidmain() 
{ 
Surround s; 
Surround::FirstWithin First; 
} 
Compile & Run
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
intSurround::FirstWithin::GetVarFirst() 
{returnxFirst; } 
intSurround::SecondWithin::GetVarSecond() 
{returnxSecond; } 
Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst() 
{returnxFirst; } 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond() 
{returnxSecond 
} 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
voidmain() 
{ 
Surround s; 
Surround::FirstWithin First; 
Surround::SecondWithin Second; 
} 
Compiler error, can’t access private data members SecondWithin
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
intSurround::FirstWithin::GetVarFirst() 
{returnxFirst; } 
intSurround::SecondWithin::GetVarSecond() 
{returnxSecond; } 
Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
public: 
FirstWithin(); 
intGetVarFirst() 
{returnxFirst; } 
private: 
intxFirst; 
}; 
private: 
classSecondWithin 
{ 
public: 
SecondWithin(); 
intGetVarSecond() 
{returnxSecond 
} 
private: 
intxSecond; 
}; 
SecondWithin obj; 
intxSurround; 
}; 
voidmain() 
{ 
Surround::FirstWithin First; 
Surround s; 
s.obj.GetVarSecond(); 
} 
Compile & Run
How can we access private data members is Nested Classes?
friend!
friend! Allowing the surrounding class to access private data members of the nested classes. The nested class to access the private data members of the surrounding class and other nested classes.
Access Private Members in Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
friendclassSurround; 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
staticintxFirst; 
}; 
private: 
classSecondWithin 
{ 
friendclasssurround; 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
staticintxSecond; 
}; 
public: 
SecondWithin obj; 
intxSurround; 
}; 
intSurround::FirstWithin::GetVarFirst() 
{FirstWithin::xFirst = SecondWithin::xSecond; returnxFirst; } 
intSurround::SecondWithin::GetVarSecond() 
{FirstWithin::xFirst = 9; returnxSecond; } 
Allowing The surrounding class to access private data members of the nested classes
Access Private Members in Nested classes 
classSurround 
{ 
public: 
classFirstWithin 
{ 
friendclassSurround; 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
staticintxFirst; 
}; 
private: 
classSecondWithin 
{ 
friendclasssurround; 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
staticintxSecond; 
}; 
public: 
SecondWithin obj; 
intxSurround; 
}; 
intSurround::FirstWithin::GetVarFirst() 
{FirstWithin::xFirst = SecondWithin::xSecond; returnxFirst; } 
intSurround::SecondWithin::GetVarSecond() 
{FirstWithin::xFirst = 9; returnxSecond; } 
But we are still missing something 
What is it?
Access Private Members in Nested classes 
classSurround 
{ 
class FirstWithin; 
class SecondWithin; 
friend class FirstWithin; 
friend class SecondWithin; 
public: 
classFirstWithin 
{ 
friendclassSurround; 
public: 
FirstWithin(); 
intGetVarFirst(); 
private: 
staticintxFirst; 
}; 
private: 
classSecondWithin 
{ 
friendclasssurround; 
public: 
SecondWithin(); 
intGetVarSecond(); 
private: 
staticintxSecond; 
}; 
public: 
SecondWithin obj; 
intxSurround; 
}; 
We should use Forward Declaration 
So that classes can know other classes exist
Quiz
Quiz #1 
classVehicle 
{ 
public: 
Vehicle (char*n){ name = n; weight = 0; } 
Vehicle (intwt, char* n ){ name=n; cout <<"Vehicle class "<< name << weight << endl;weight = wt; }; 
~Vehicle(){cout << "Destructing vehicle class for "<< name << weight << endl; } 
intGetWeight() const{returnweight; } 
voidSetWeight (intx ){weight = x;}; 
private: 
intweight; 
char*name; 
}; 
classCar: publicVehicle 
{ 
public: 
Car(intwt, intsp, char*n1, char*n2):Vehicle(wt,n2){name = n1; cout <<"Car class for "<< name << speed << "-"<< GetWeight() << endl; speed = sp;} 
~Car(){cout << "Destructing car class for "<< name << speed << "-"<< GetWeight() << endl; } 
intGetSpeed () {returnspeed; } 
voidSetSpeed (intx ){speed = x; } 
private: 
intspeed; 
char*name; 
}; 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
staticVehicle VG1("VG1"); 
Vehicle VG2("VG2"); 
voidmain() 
{ 
Vehicle V1("1"); 
{ 
staticVehicle V2("2"); 
Car MyCar1 (2,3,"1","temp1"); 
} 
Car MyCar2 (2,3,"2","temp2"); 
Car MyCar3 (2,3,"3","temp3"); 
Vehicle V3 ("3"); 
} 
Vehicle class temp1-858993460 
Car class for 1-858993460-2 
Destructing car class for 13-2 
Destructing vehicle class for temp12 
Vehicle class temp2-858993460 
Car class for 2-858993460-2 
Vehicle class temp3-858993460 
Car class for 3-858993460-2 
Destructing vehicle class for 30 
Destructing car class for 33-2 
Destructing vehicle class for temp32 
Destructing car class for 23-2 
Destructing vehicle class for temp22 
Destructing vehicle class for 10 
Destructing vehicle class for 20 
Destructing vehicle class for VG20 
Destructing vehicle class for VG10 
Press any key to continue
Quiz #2 
#include<iostream> 
usingnamespace::std; 
classVehicle 
{ 
public: 
Vehicle () 
{weight = 0; } 
Vehicle (intwt) 
{weight = wt; }; 
intGetWeight () 
{returnweight; } 
voidSetWeight (intx ) 
{weight = x;}; 
private: 
intweight; 
}; 
classCar: publicVehicle 
{ 
public: 
friendvoidfoo(Car &C); 
Car() 
{speed = 0; } 
Car(intwt, intsp) 
{SetWeight(wt); speed = sp; } 
intGetSpeed () 
{returnspeed; } 
voidSetSpeed (intx ) 
{speed = x; } 
private: 
intspeed; 
}; 
voidfoo(Car &C) 
{staticintx = 6; C.speed =+ x;x++; } 
#include<iostream> 
#include"Testing.h" 
usingnamespace::std; 
voidmain() 
{ 
Car MyCar(2,3); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
foo(MyCar); 
MyCar.SetWeight(5); 
MyCar.SetSpeed(10); 
foo(MyCar); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
foo(MyCar); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
foo(MyCar); 
cout << MyCar.GetSpeed() << endl; 
cout << MyCar.GetWeight() << endl; 
} 
3 
2 
7 
5 
8 
5 
9 
5

C++ L10-Inheritance

  • 1.
    Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14 C++ Programming Language L10-INheritance
  • 2.
  • 3.
    Introduction to Inheritance •Two common types: –“has –a ” •Composition (as we have learned) –Object containing one or more objects of other classes as members. »A car has a steering wheel –“Is –a ” •Inheritance –Derived class object treated as base class objects »A car is a vehicle »Vehicle properties / behaviors also car properties / behaviors
  • 4.
    Inheritance Computer Computer Keyboard PC “has –a ” “ is –a ”
  • 5.
    Inheritance, “ is–a ” Relationship The link between classes in an “is –a ” relationship comes from the fact that the subclasses share all the attributes and behaviors found in the supper class, plus more! Computer PC “ is –a ”
  • 6.
    Inheritance, “ is–a ” Relationship Computer PC “ is –a ”
  • 7.
    Inheritance, “ is–a ” Relationship Vehicle Car Bicycle Motorcycle “ is –a ”
  • 8.
    Inheritance, “ is–a ” Relationship Vehicle Car Boat wheeled Bicycle Two-doors four-doors
  • 9.
    Inheritance •Let’s havethe following relationship –Student and instructor (teacher) •Fields: –Name, address, phone •Now, let’s build a class called person
  • 10.
    Inheritance •Now, wethink that we can create the following class and carry on •But we have the idea? –Inheritance! classStudent { private: person UniversityStudent; intStuID; intStuRate; }; classInstructor { private: person UniversityInstructor; intInsID; intInsSalary; };
  • 11.
    Advantages of Inheritance Person Person Student Instructor “has –a ” relationship
  • 12.
    Advantages of Inheritance Person Student Instructor “has –a ” relationship ID, StuRate Person ID, InsSalary
  • 13.
    Advantages of Inheritance Person Person Student Instructor “ is –a ” “ is –a ”
  • 14.
    Advantages of Inheritance Person Student Instructor “ is –a ”
  • 15.
    Advantages of Inheritance •What we do now, is that –We create a “person” class then •Inherit from it! •What to inherit fromto? –We Inherit class person to class »Student »Instructor –And then, we carry on and add for each class of them their unique features! (ID, StuRate, StuSalaryetc)
  • 16.
    Advantages of Inheritance •Advantages of inheritance: –Saving time –After saving time building the first part of the solution •We can concentrate on building more complex materials up ahead –Can extend or revise (edit) the parent class without corrupting the existing parent class features –Absorb existing parent class’s data and behaviors •All public, protected member variables in the main class are accessible from the derived class •All behaviors inherited too –Enhance with new capabilities •Customizing •Additional behaviors
  • 17.
    Thinking Inheritance •C++Usability •Creating a “Derived class” from “Base Class” * •Base Class usually represent: –Larger set of objects than derived class –Characteristics that are shared between all of the derived classes •Derived classes usually represent more specialized group of objects •Sometimes –Derived class inherit data members and data functions it does doesn’t need or should shouldn’t not have ______________________________________ *Base Class: also called Parent class, Super class, Ancestor *Derived Class: also called Child Class, Sub Class, Descendant
  • 18.
    Class Hierarchy types •Direct Indirect: –Direct class access •Inherited explicitly (one level up) –Indirect class access •(one or two levels up hierarchy) •Single Multiple: –Single Inheritance •From one base class –Multiple Inheritance •From multiple base class (only in C++, not in C++.NET, C#, Java, etc.)
  • 19.
  • 20.
  • 21.
  • 22.
    Inheritance Base class Derived classes Student GraduateStudent, UndergraduateStudent Shape Circle, Triangle, Rectangle, Sphere, Cube Loan CarLoan, HomeImprovementLoan, MortgageLoan Employee Faculty, Staff Account CheckingAccount, SavingsAccount
  • 23.
  • 24.
  • 25.
  • 26.
    Creating a DerivedClass •Notes: –When inheriting with public specifier •friend Functions are not inherited! –When inheriting with private specifier •Non of the public methods of the base class are accessible to the users of the derived class
  • 27.
    Creating a DerivedClass #include<iostream> usingnamespace::std; classBaseClass { }; classDerivedClass: privateBaseClass { }; #include<iostream> usingnamespace::std; classBaseClass { }; classDerivedClass: publicBaseClass { }; #include<iostream> usingnamespace::std; classBaseClass { }; classDerivedClass: protectedBaseClass { }; #include<iostream> usingnamespace::std; classBaseClass { }; classDerivedClass:BaseClass { }; When ommiting private, public or protected then the default is private
  • 28.
    Creating a DerivedClass classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; MyCar.SetWeight(5);// member function in base class MyCar.SetSpeed(10); cout << "Printing"<< endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } 3 2 Printing 10 5 Press any key to continue
  • 29.
    Live Demowith Lordof the Rings(Humans and Goblins inherit from Creatures)
  • 30.
  • 31.
  • 32.
    Creating a DerivedClass classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; cout << "After 1st change"<< endl; MyCar.SetWeight(5); MyCar.SetSpeed(10); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; foo(MyCar); cout << "After 2nd change"<< endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } 3 2 After 1st change 10 5 After 2nd change 9 5 Press any key to continue
  • 33.
    Creating a DerivedClass classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car C); Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; voidfoo(Car C) {C.speed = 9; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; cout << "After 1st change"<< endl; MyCar.SetWeight(5); MyCar.SetSpeed(10); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; foo(MyCar); cout << "After 2nd change"<< endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } 3 2 After 1st change 10 5 After 2nd change 10 5 Press any key to continue
  • 34.
    Creating a DerivedClass classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car C); Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; voidfoo(Car C) {C.weight= 9; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; cout << "After 1st change"<< endl; MyCar.SetWeight(5); MyCar.SetSpeed(10); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; foo(MyCar); cout << "After 2nd change"<< endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } Compiler error, function foo must not access private data members of Vehicle
  • 35.
    Creating a DerivedClass classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight (){returnweight; } voidSetWeight (intx ){weight = x;}; inttempWeight; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; MyCar.tempWeight = 5; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } 3 2 3 2 Press any key to continue
  • 36.
    Creating a DerivedClass classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight (){returnweight; } voidSetWeight (intx ){weight = x;}; inttempWeight; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9; C.TempWeight = 10; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; cout << MyCar.tempWeight << endl; MyCar.tempWeight = 5; foo(MyCar); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; cout << MyCar.tempWeight << endl; } 3 2 -858993460 9 2 10 Press any key to continue
  • 37.
  • 38.
    protectedMembers •Base classprotected members –Intermediate level of protection between public and private –Protected data members are accessible through their own class and their own class’s derived classes •Accessible to –Base class members(like public) –Base class friends(like public, private) –Derived class members (like public) –Derived class friends(like public) •BUT NO ONE using base class directly can access them directly (like private)
  • 39.
    protectedMembers #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight (){returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidPrint() const{ cout << speed << "n"<< weight << endl; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); MyCar.Print(); } Compile error Car can’t access private data membes declared in its base class
  • 40.
    protectedMembers #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight (){returnweight; } voidSetWeight (intx ){weight = x;}; protected: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidPrint() const{ cout << speed << "n"<< weight << endl; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); MyCar.Print(); } 3 2
  • 41.
    protectedMembers #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight (){returnweight; } voidSetWeight (intx ){weight = x;}; protected: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidPrint() const{ cout << speed << "n"<< weight << endl; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); MyCar.weight = 3; cout << MyCar.weight << endl; } Compiler error, can’t access protected data members directly from outside the class
  • 42.
    protectedMembers classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight (){returnweight; } voidSetWeight (intx ){weight = x;}; protected: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){weight = wt; speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidPrint() const{ cout << speed << "n"<< weight << endl; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); MyCar.Print(); } 3 2
  • 43.
    protectedMembers •Problems ofprotected –No validity checking between derived class and the base one –Derived class can assign illegal values to protected members (since they can access them directly.) –Implementation dependent •Fragile (brittle) software
  • 44.
    protectedMembers #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; voidGetName1() const{ cout << "I'm a vehicle "<< endl; } private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidGetName2() const{ cout << "I'm a car "<< endl; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); MyCar.GetName2(); } I'm a car Press any key to continue
  • 45.
    protectedMembers #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; voidGetName1() const{ cout << "I'm a vehicle "<< endl; } private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidGetName2() const{ cout << "I'm a car "<< endl; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle MyVehicle; MyVehicle.GetName1(); Car MyCar(2,3); MyCar.GetName2(); } I'm a vehicle I'm a car Press any key to continue
  • 46.
    protectedMembers classVehicle { public: Vehicle (){weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; voidOverFun(){cout<< "In Vehicle class n";} private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } voidOverFun() { cout << "In car class"<< endl; Vehicle::OverFun(); // overriding here with:: } private:intspeed;}; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle MyVehicle; Car MyCar(2,3); cout << "Printing with Vehicle:"<< endl; MyVehicle.OverFun(); cout << "Printing with car:"<< endl; MyCar.OverFun(); } Printing with Vehicle: In Vehicle class Printing with car: In car class In Vehicle class Press any key to continue
  • 47.
  • 48.
    What You Can’tInherit •The following never ever can be inherited –Constructors –Destructors –Friends –Static data members –Static member functions –new operator –“=“ operator
  • 49.
  • 50.
    Overriding Base ClassBehavior •What’s overriding overloading? –Any child class function with the same name and same argument list as the parent •overrides the parent function –Any child class with the same name, yet with different argument list as for the parent •overloads the parent function
  • 51.
  • 52.
    Constructors and Inheritance •When instantiate a derived class, a constructor for its base class is called first followed by the derived class constructor. •Chains of constructors calls –Base class constructor called last, but –First to finish executing •Example: Point Circle Cylinder –Point constructor called last, but –Point constructor body finishes executing first
  • 53.
    Constructors and Inheritance #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle MyVehicle; } I'm a vehicle! Press any key to continue
  • 54.
    Constructors and Inheritance #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar; } I'm a vehicle! I'm a car! Press any key to continue
  • 55.
    Constructors and Inheritance #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(); } Press any key to continue
  • 56.
    Constructors and Inheritance #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle My(); } Press any key to continue
  • 57.
    Constructors and Inheritance #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle My(); My.GetWeight(); } Compiler error
  • 58.
    Constructors and Inheritance #include<iostream> usingnamespace::std; classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {Vehicle::Vehicle(); returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar; cout << MyCar.GetSpeed() << endl; } I'm a vehicle! I'm a car! I'm a vehicle! 0 Press any key to continue
  • 59.
    Constructors and Inheritance classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){cout << "I'm a vehicle!"<< endl; weight += wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {Vehicle::Vehicle(5); returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar; cout << MyCar.GetWeight() << endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } I'm a vehicle! I'm a car! 0 I'm a vehicle! 0 I'm a vehicle! 0 0 Press any key to continue
  • 60.
    Constructors and Inheritance classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){cout << "I'm a vehicle!"<< endl; weight += wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {Vehicle::Vehicle(5); returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar; cout << MyCar.GetWeight() << endl; cout << MyCar.GetSpeed() << endl; MyCar.SetWeight(4); cout << MyCar.GetWeight() << endl; } I'm a vehicle! I'm a car! 0 I'm a vehicle! 0 4 Press any key to continue
  • 61.
    Constructors and Inheritance classVehicle { public: Vehicle (){cout << "I'm a vehicle!"<< endl; weight = 0; } Vehicle (intwt){cout << "I'm a vehicle!"<< endl; weight += wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(){ cout << "I'm a car!"<< endl; speed = 0; } Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {Vehicle::Vehicle(5); returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar; cout << MyCar.GetWeight() << endl; cout << MyCar.GetSpeed() << endl; MyCar.SetWeight(4); cout << MyCar.GetWeight() << endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } I'm a vehicle! I'm a car! 0 I'm a vehicle! 0 4 I'm a vehicle! 0 4 Press any key to continue
  • 62.
    Constructors and Inheritance classVehicle { public: Vehicle (){ weight = 0; } Vehicle (intwt){ weight = wt; }; intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(intwt, intsp){SetWeight(wt);speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar (2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } 3 2 Press any key to continue
  • 63.
  • 64.
    Code Cracking classVehicle { public: Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; Compiler error. We didn’t define the 0 parameter Vehicle constructor and but we used it in the (0 parameter constructor of the) derived class
  • 65.
    Code Cracking classVehicle { public: Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; The derived class implicitly call the base class constructor!
  • 66.
    Code Cracking classVehicle { public: Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; The compiler looks for a matching non- parameter base class constructor but can’t find one!
  • 67.
    Code Cracking classVehicle { public: Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; To solve this, 2 ways: 1.define an non-parameter base class constructor. 2.call the parametric base class constructor (the one with parameters) and make an explicit call for it from the derived class.
  • 68.
    Code Cracking classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: private Vehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; Error1error C2247: 'Vehicle::GetWeight' not accessible because 'Car' uses 'private' to inherit from 'Vehicle'c:userszmeedocumentsvisual studio 2008projectsexamexamexam.cpp11Exam Error2error C2247: 'Vehicle::SetWeight' not accessible because 'Car' uses 'private' to inherit from 'Vehicle'c:userszmeedocumentsvisual studio 2008projectsexamexamexam.cpp13Exam
  • 69.
    Code Cracking classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {weight = wt; speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; Compiler error. Can’t access private data member of the base class directly
  • 70.
    Creating a DerivedClassAs we have seen, derived class can affect state changes in private base-class members. BUT ONLY THROUGH, Non-private functions provided in the base class and inherited into the derived class.
  • 71.
    Code Cracking classVehicle { public: friendvoidfoo(Vehicle &V); Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; voidfoo(Vehicle &V) {V.weight = 9; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle V; Car MyCar(2,3); MyCar.SetWeight(5);// member function in base class MyCar.SetSpeed(10); MyCar.foo(V); cout << "Printing"<< endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } Printing 10 5 Press any key to continue
  • 72.
    Code Cracking classVehicle { public: friendvoidfoo(Vehicle &V); Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; voidfoo(Vehicle &V) {V.weight = 9; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle V; Car MyCar(2,3); MyCar.SetWeight(5);// member function in base class MyCar.SetSpeed(10); foo(V); cout << "Printing"<< endl; cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } Compiler error. foo is not a member functions in Car. It’s not inherited!
  • 73.
  • 74.
    Constructors and Destructors •Chain of destructors calls –Reverse order of constructor chain –Destructor of the derived class called first –Destructor of the next class up hierarchy called next •Continue up hierarchy until final base class is reached •After final base class destructor, object is removed from memory
  • 75.
    Constructors and Destructors classVehicle { public: Vehicle (){ weight = 0; } Vehicle (intwt){ weight = wt; cout <<"Vehicle class "<< weight << endl;}; ~Vehicle(){cout << "Destructing vehicle class for "<< weight << endl; } intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(intwt, intsp):Vehicle(wt){speed = sp; cout <<"Car class for " << speed << "-"<< GetWeight() << endl; } ~Car(){cout << "Destructing car class for "<< speed << "-" << GetWeight() << endl; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Vehicle V1; { Vehicle V2; Car MyCar1 (2,3); } Car MyCar2 (2,3); Car MyCar3 (2,3); Vehicle V3; } Vehicle class 2 Car class for 3-2 Destructing car class for 3-2 Destructing vehicle class for 2 Destructing vehicle class for 0 Vehicle class 2 Car class for 3-2 Vehicle class 2 Car class for 3-2 Destructing vehicle class for 0 Destructing car class for 3-2 Destructing vehicle class for 2 Destructing car class for 3-2 Destructing vehicle class for 2 Destructing vehicle class for 0 Press any key to continue
  • 76.
    Constructors and Destructors classVehicle { public: Vehicle (){ weight = 0; } Vehicle (intwt){ weight = wt; cout <<"Vehicle class "<< weight << endl;}; ~Vehicle(){cout << "Destructing vehicle class for "<< weight << endl; } intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car(intwt, intsp):Vehicle(wt){speed = sp; cout <<"Car class for " << speed << "-"<< GetWeight() << endl; } ~Car(){cout << "Destructing car class for "<< speed << "-" << weight << endl; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; }; voidfoo(Car &C) {C.speed = 9;} Compiler error, accessing private data member of vehicle
  • 77.
  • 78.
    Single VS MultipleInheritance
  • 79.
    Single VS MultipleInheritance
  • 80.
    Multiple Inheritance •Lookslike the single inheritance •Except! –There’s multiple base classes –Separated by commas –Individually can be declared as public, protected or private •Default is private –Inherited member variables are accessible according to the rules of single inheritance •Name conflicts –Can result in a member of a base class being hidden by the member of the derived class with the same name.
  • 81.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: OptionList(){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: Window(){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicOptionList, publicWindow { public: Menu(){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M; } OptionList constructor Window constructor Menu constructor Menu destructor Window destructor OptionList destructor Press any key to continue
  • 82.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: OptionList(){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: Window(){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicWindow, publicOptionList { public: Menu(){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M; } Window constructor OptionList constructor Menu constructor Menu destructor OptionList destructor Window destructor Press any key to continue The constructors runs in order of declaration
  • 83.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: OptionList(){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: Window(){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicWindow, publicOptionList { public: Menu(){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M(); } Press any key to continue
  • 84.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: //OptionList(){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: //Window(){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicWindow, publicOptionList { public: Menu(){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M; } Menu constructor Menu destructor OptionList destructor Window destructor Press any key to continue
  • 85.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: //OptionList(){cout << "OptionList constructor n";}; OptionList(intx){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: //Window(){cout << "Window constructor n";}; Window(inty){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicWindow, publicOptionList { public: Menu(){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; Compiler error
  • 86.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: //OptionList(){cout << "OptionList constructor n";}; OptionList(intx){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: //Window(){cout << "Window constructor n";}; Window(inty){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicWindow, publicOptionList { public: Menu(intn, intm):Window(n),OptionList(m){cout<<"Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M; } Window constructor OptionList constructor Menu constructor Menu destructor OptionList destructor Window destructor Press any key to continue
  • 87.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: //OptionList(){cout << "OptionList constructor n";}; OptionList(intx){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; }; classWindow { public: //Window(){cout << "Window constructor n";}; Window(inty){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; }; classMenu: publicWindow, publicOptionList { public: Menu(intn, intm):OptionList(m),Window(n){cout<<"Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M; } Window constructor OptionList constructor Menu constructor Menu destructor OptionList destructor Window destructor Press any key to continue
  • 88.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: OptionList(intx){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; voidHighLighting(){cout << "In OptionList n";}; }; classWindow { public: Window(inty){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; voidHighLighting(){cout << "In Windown";}; }; classMenu: publicWindow, publicOptionList { public: Menu(intn, intm):OptionList(n), Window(m){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M(2,3); } Compile and run
  • 89.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: OptionList(intx){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; voidHighLighting(){cout << "In OptionList n";}; }; classWindow { public: Window(inty){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; voidHighLighting(){cout << "In Windown";}; }; classMenu: publicWindow, publicOptionList { public: Menu(intn, intm):OptionList(n), Window(m){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M(2,3); M.HighLighting(); } Now, it’s a compiler error
  • 90.
    Multiple Inheritance #include<iostream> usingnamespace::std; classOptionList { public: OptionList(intx){cout << "OptionList constructor n";}; ~OptionList(){cout << "OptionList destructor n";}; voidHighLighting(){cout << "In OptionList n";}; }; classWindow { public: Window(inty){cout << "Window constructor n";}; ~Window(){cout << "Window destructor n";}; voidHighLighting(){cout << "In Windown";}; }; classMenu: publicWindow, publicOptionList { public: Menu(intn, intm):OptionList(n), Window(m){cout << "Menu constructor n";}; ~Menu(){cout << "Menu destructor n";}; }; #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Menu M(2,3); M.Window::HighLighting(); M.OptionList::HighLighting(); } Window constructor OptionList constructor Menu constructor In Window In OptionList Menu destructor OptionList destructor Window destructor Press any key to continue
  • 91.
    Float, double, longdouble C++ data types Structured Simple Address Pointer Reference enum Floating Array Struct Union Class Char, Short, int, long, bool Integral
  • 92.
    Nested ClassesClassesthat aredefined inside another classes are called nested classesIn the public, private or protected sectionSuch a nested class can be considered a member of the outer class
  • 93.
    Nested classes •Visibility –In public section: •Visible outside the outer class –In private section: •Visible only for the member of the outer class –In protected section: •Subclasses, derived from the outer class
  • 94.
    Nested classes classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst() {returnxFirst; } private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond() {returnxSecond } private: intxSecond; }; SecondWithin obj; intxSurround; }; Class FirstWithin Visibility Both outside and inside Member functions are globally visible Intvariable is only visible to the class FirstWithin Neither Surroundnor SecondWithincan access the variable of the class FirstWithin directly
  • 95.
    Nested classes classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst() {returnxFirst; } private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond() {returnxSecond } private: intxSecond; }; SecondWithin obj; intxSurround; }; Class FirstWithin Visibility Both outside and inside Member functions are globally visible Intvariable is only visible to the class FirstWithin Neither Surroundnor SecondWithincan access the variable of the class FirstWithin directly Class SecondWith Visibility Just inside surround Member functions can only be reached by the members of class SecondWithinonly Intvariable is only visible to the class SecondWithin Neither Surroundnor FirstWithincan access the variable of the class SecondWithindirectly
  • 96.
    classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst(); private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond(); private: intxSecond; }; SecondWithin obj; intxSurround; }; intSurround::FirstWithin::GetVarFirst() {returnxFirst; } intSurround::SecondWithin::GetVarSecond() {returnxSecond; } Nested classes classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst() {returnxFirst; } private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond() {returnxSecond } private: intxSecond; }; SecondWithin obj; intxSurround; }; Note how we define the member functions here
  • 97.
    classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst(); private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond(); private: intxSecond; }; SecondWithin obj; intxSurround; }; intSurround::FirstWithin::GetVarFirst() {returnxFirst; } intSurround::SecondWithin::GetVarSecond() {returnxSecond; } Nested classes classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst() {returnxFirst; } private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond() {returnxSecond } private: intxSecond; }; SecondWithin obj; intxSurround; }; #include<iostream> #include"MyFile.h" usingnamespace::std; voidmain() { Surround s; Surround::FirstWithin First; } Compile & Run
  • 98.
    classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst(); private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond(); private: intxSecond; }; SecondWithin obj; intxSurround; }; intSurround::FirstWithin::GetVarFirst() {returnxFirst; } intSurround::SecondWithin::GetVarSecond() {returnxSecond; } Nested classes classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst() {returnxFirst; } private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond() {returnxSecond } private: intxSecond; }; SecondWithin obj; intxSurround; }; voidmain() { Surround s; Surround::FirstWithin First; Surround::SecondWithin Second; } Compiler error, can’t access private data members SecondWithin
  • 99.
    classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst(); private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond(); private: intxSecond; }; SecondWithin obj; intxSurround; }; intSurround::FirstWithin::GetVarFirst() {returnxFirst; } intSurround::SecondWithin::GetVarSecond() {returnxSecond; } Nested classes classSurround { public: classFirstWithin { public: FirstWithin(); intGetVarFirst() {returnxFirst; } private: intxFirst; }; private: classSecondWithin { public: SecondWithin(); intGetVarSecond() {returnxSecond } private: intxSecond; }; SecondWithin obj; intxSurround; }; voidmain() { Surround::FirstWithin First; Surround s; s.obj.GetVarSecond(); } Compile & Run
  • 100.
    How can weaccess private data members is Nested Classes?
  • 101.
  • 102.
    friend! Allowing thesurrounding class to access private data members of the nested classes. The nested class to access the private data members of the surrounding class and other nested classes.
  • 103.
    Access Private Membersin Nested classes classSurround { public: classFirstWithin { friendclassSurround; public: FirstWithin(); intGetVarFirst(); private: staticintxFirst; }; private: classSecondWithin { friendclasssurround; public: SecondWithin(); intGetVarSecond(); private: staticintxSecond; }; public: SecondWithin obj; intxSurround; }; intSurround::FirstWithin::GetVarFirst() {FirstWithin::xFirst = SecondWithin::xSecond; returnxFirst; } intSurround::SecondWithin::GetVarSecond() {FirstWithin::xFirst = 9; returnxSecond; } Allowing The surrounding class to access private data members of the nested classes
  • 104.
    Access Private Membersin Nested classes classSurround { public: classFirstWithin { friendclassSurround; public: FirstWithin(); intGetVarFirst(); private: staticintxFirst; }; private: classSecondWithin { friendclasssurround; public: SecondWithin(); intGetVarSecond(); private: staticintxSecond; }; public: SecondWithin obj; intxSurround; }; intSurround::FirstWithin::GetVarFirst() {FirstWithin::xFirst = SecondWithin::xSecond; returnxFirst; } intSurround::SecondWithin::GetVarSecond() {FirstWithin::xFirst = 9; returnxSecond; } But we are still missing something What is it?
  • 105.
    Access Private Membersin Nested classes classSurround { class FirstWithin; class SecondWithin; friend class FirstWithin; friend class SecondWithin; public: classFirstWithin { friendclassSurround; public: FirstWithin(); intGetVarFirst(); private: staticintxFirst; }; private: classSecondWithin { friendclasssurround; public: SecondWithin(); intGetVarSecond(); private: staticintxSecond; }; public: SecondWithin obj; intxSurround; }; We should use Forward Declaration So that classes can know other classes exist
  • 106.
  • 107.
    Quiz #1 classVehicle { public: Vehicle (char*n){ name = n; weight = 0; } Vehicle (intwt, char* n ){ name=n; cout <<"Vehicle class "<< name << weight << endl;weight = wt; }; ~Vehicle(){cout << "Destructing vehicle class for "<< name << weight << endl; } intGetWeight() const{returnweight; } voidSetWeight (intx ){weight = x;}; private: intweight; char*name; }; classCar: publicVehicle { public: Car(intwt, intsp, char*n1, char*n2):Vehicle(wt,n2){name = n1; cout <<"Car class for "<< name << speed << "-"<< GetWeight() << endl; speed = sp;} ~Car(){cout << "Destructing car class for "<< name << speed << "-"<< GetWeight() << endl; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ){speed = x; } private: intspeed; char*name; }; #include<iostream> #include"Testing.h" usingnamespace::std; staticVehicle VG1("VG1"); Vehicle VG2("VG2"); voidmain() { Vehicle V1("1"); { staticVehicle V2("2"); Car MyCar1 (2,3,"1","temp1"); } Car MyCar2 (2,3,"2","temp2"); Car MyCar3 (2,3,"3","temp3"); Vehicle V3 ("3"); } Vehicle class temp1-858993460 Car class for 1-858993460-2 Destructing car class for 13-2 Destructing vehicle class for temp12 Vehicle class temp2-858993460 Car class for 2-858993460-2 Vehicle class temp3-858993460 Car class for 3-858993460-2 Destructing vehicle class for 30 Destructing car class for 33-2 Destructing vehicle class for temp32 Destructing car class for 23-2 Destructing vehicle class for temp22 Destructing vehicle class for 10 Destructing vehicle class for 20 Destructing vehicle class for VG20 Destructing vehicle class for VG10 Press any key to continue
  • 108.
    Quiz #2 #include<iostream> usingnamespace::std; classVehicle { public: Vehicle () {weight = 0; } Vehicle (intwt) {weight = wt; }; intGetWeight () {returnweight; } voidSetWeight (intx ) {weight = x;}; private: intweight; }; classCar: publicVehicle { public: friendvoidfoo(Car &C); Car() {speed = 0; } Car(intwt, intsp) {SetWeight(wt); speed = sp; } intGetSpeed () {returnspeed; } voidSetSpeed (intx ) {speed = x; } private: intspeed; }; voidfoo(Car &C) {staticintx = 6; C.speed =+ x;x++; } #include<iostream> #include"Testing.h" usingnamespace::std; voidmain() { Car MyCar(2,3); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; foo(MyCar); MyCar.SetWeight(5); MyCar.SetSpeed(10); foo(MyCar); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; foo(MyCar); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; foo(MyCar); cout << MyCar.GetSpeed() << endl; cout << MyCar.GetWeight() << endl; } 3 2 7 5 8 5 9 5