0% found this document useful (0 votes)
103 views

11 Inheritance PDF

The document discusses inheritance in C++ object oriented programming. It provides an example of defining a base Vehicle class with properties like wheels and speed, and deriving an Automobile class from Vehicle by making it a public subclass. This allows Automobile to inherit properties from Vehicle while also defining its own properties like engine size and gears. The example demonstrates how inheritance avoids duplicating code and allows different classes to reuse properties from a base class.

Uploaded by

Utsav Vedant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

11 Inheritance PDF

The document discusses inheritance in C++ object oriented programming. It provides an example of defining a base Vehicle class with properties like wheels and speed, and deriving an Automobile class from Vehicle by making it a public subclass. This allows Automobile to inherit properties from Vehicle while also defining its own properties like engine size and gears. The example demonstrates how inheritance avoids duplicating code and allows different classes to reuse properties from a base class.

Uploaded by

Utsav Vedant
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Object Oriented Programming with C++

11. Inheritance

By: Prof. Pandav Patel

Third Semester, August 2020


Computer Engineering Department
Dharmsinh Desai University
Inheritance: Introduction

Vehicle – has wheels (diff num), has maximum speed, manufacturer etc..

Automobile – has engine, has gears etc...

PassangeVehicle – passanger capacity etc...

GoodCarrier – max load in weight, max load in volume etc...

Cart – Some animal to pull the cart, number of animals required etc...

class Vehicle { class Automobile { class Cart {


int wheels_count; int wheels_count; int wheels_count;
float max_speed; float max_speed; float max_speed;
string manufacturer; string manufacturer; string manufacturer;
public: float engine_cc; string animal_type;
Vehicle(int, float, int gear_count; int animal_count;
string); public: public:
void print(); Automobile(int, float, string, float, Cart(int, float, string, string,
}; int); int);
void print(); void print();

Is there duplication of}; code? };

Can we resuse code instead?
Inheritance: Introduction

Inheritance (IS-A relationship)

Mechanism of deriving new class from existing class is called Inheritance
(or derivation)

Existing class used in inheritance is called – base class / parent class /
super class

Newly created class is called – derived class / child class / sub class

Syntax of inheritance is as follows:
class derived-class : visibility-mode base-class
{ base-class
// Members of derived class ↑
} derived-

derived-class IS-A base-class. class

visibility-mode could be private / protected / public

Its optional, by default its private

Though visibility mode is often public, will look into it first
#include<iostream> int main() {
#include<string>
Vehicle vehicle(2, 7.1, "Ambica carts");
using std::cout; vehicle.vehicle_print();
using std::endl;
using std::string; return 0;
}
class Vehicle {
int wheels_count;
float max_speed;
string manufacturer;
public:
Vehicle(int, float, string);
void vehicle_print();
};
Vehicle::Vehicle(int wheels_count, float max_speed, string manufacturer) {
this->wheels_count = wheels_count;
this->max_speed = max_speed;
this->manufacturer = manufacturer;
}
void Vehicle::vehicle_print() { Wheel Count: 2
cout << "Wheel Count: " << wheels_count << endl; Max Speed: 7.1
cout << "Max Speed: " << max_speed << endl; Manufacturer: Ambica carts
cout << "Manufacturer: " << manufacturer << endl;
}
class Vehicle { Automobile(int wheels_count, float max_speed,
string manufacturer,
int wheels_count; float engine_cc, int gear_count):
float max_speed; engine_cc(engine_cc),
string manufacturer; gear_count(gear_count),
public: // error: no matching function for call to ‘Vehicle::Vehicle()’
Vehicle(int, float, string); {}
void vehicle_print();
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed, cout << "Wheel Count: " << wheels_count << endl;
string manufacturer): cout << "Max Speed: " << max_speed << endl;
wheels_count(wheels_count), cout << "Manufacturer: " << manufacturer << endl;
max_speed(max_speed), cout << "Engine Capacity: " << engine_cc << endl;
manufacturer(manufacturer) { cout << "Gear Count: " << gear_count << endl;
cout << "Vehicle constructor called\n"; }
} };
void Vehicle::vehicle_print() {
cout << "Wheel Count: " << wheels_count << endl; int main() {
cout << "Max Speed: " << max_speed << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Manufacturer: " << manufacturer << endl; return 0;
} }

class Automobile : public Vehicle{


float engine_cc;
int gear_count;
public:
class Vehicle { Automobile(int wheels_count, float max_speed,
string manufacturer,
int wheels_count; float engine_cc, int gear_count):
float max_speed; engine_cc(engine_cc),
string manufacturer; gear_count(gear_count),
public: Vehicle(wheels_count, max_speed, manufacturer) {
Vehicle(int, float, string); cout << "Automobile constructor called\n";
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed, // error: ‘int Vehicle::wheels_count’ is private
string manufacturer): cout << "Wheel Count: " << wheels_count << endl;
wheels_count(wheels_count), // similar error
max_speed(max_speed), cout << "Max Speed: " << max_speed << endl;
manufacturer(manufacturer) { // similar error
cout << "Vehicle constructor called\n"; cout << "Manufacturer: " << manufacturer << endl;
} cout << "Engine Capacity: " << engine_cc << endl;
void Vehicle::vehicle_print() { cout << "Gear Count: " << gear_count << endl;
cout << "Wheel Count: " << wheels_count << endl; }
cout << "Max Speed: " << max_speed << endl; };
cout << "Manufacturer: " << manufacturer << endl; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
automobile.print();
class Automobile : public Vehicle{ return 0;
float engine_cc; }
int gear_count;
public:
Vehicle constructor called
class Vehicle { Automobile(int wheels_count, float max_speed,
Automobile constructor
public: string manufacturer,
called
int wheels_count; float engine_cc, int gear_count):
Wheel Count: 2
float max_speed; engine_cc(engine_cc),
Max Speed: 100
string manufacturer; gear_count(gear_count),
Manufacturer: Honda
Engine Capacity: 125 Vehicle(wheels_count, max_speed, manufacturer) {
Vehicle(int, float, string); cout << "Automobile constructor called\n";
Gear Count: 4
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed,
string manufacturer): cout << "Wheel Count: " << wheels_count << endl;
wheels_count(wheels_count),
max_speed(max_speed), cout << "Max Speed: " << max_speed << endl;
manufacturer(manufacturer) {
cout << "Vehicle constructor called\n"; cout << "Manufacturer: " << manufacturer << endl;
} cout << "Engine Capacity: " << engine_cc << endl;
void Vehicle::vehicle_print() { cout << "Gear Count: " << gear_count << endl;
cout << "Wheel Count: " << wheels_count << endl; }
cout << "Max Speed: " << max_speed << endl; };
cout << "Manufacturer: " << manufacturer << endl; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
automobile.print();
class Automobile : public Vehicle{ return 0;
float engine_cc; }
int gear_count;
public:
Vehicle constructor called
class Vehicle { Automobile(int wheels_count, float max_speed,
Automobile constructor
called string manufacturer,
int wheels_count; float engine_cc, int gear_count):
Wheel Count: 2
float max_speed; engine_cc(engine_cc),
Max Speed: 100
string manufacturer; gear_count(gear_count),
Manufacturer: Honda
public: Vehicle(wheels_count, max_speed, manufacturer) {
Engine Capacity: 125
Vehicle(int, float, string); cout << "Automobile constructor called\n";
Gear Count: 4
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed, vehicle_print();
string manufacturer): cout << "Engine Capacity: " << engine_cc << endl;
wheels_count(wheels_count), cout << "Gear Count: " << gear_count << endl;
max_speed(max_speed), }
manufacturer(manufacturer) { };
cout << "Vehicle constructor called\n"; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
void Vehicle::vehicle_print() { automobile.print();
cout << "Wheel Count: " << wheels_count << endl; return 0;
cout << "Max Speed: " << max_speed << endl; }
cout << "Manufacturer: " << manufacturer << endl;
}

class Automobile : public Vehicle{


float engine_cc;
int gear_count;
public:
Vehicle constructor called
class Vehicle { Automobile(int wheels_count, float max_speed,
Automobile constructor
protected: string manufacturer,
called
int wheels_count; float engine_cc, int gear_count):
Wheel Count: 2
float max_speed; engine_cc(engine_cc),
Max Speed: 100
string manufacturer; gear_count(gear_count),
Manufacturer: Honda
Engine Capacity: 125 Vehicle(wheels_count, max_speed, manufacturer) {
Vehicle(int, float, string); cout << "Automobile constructor called\n";
Gear Count: 4
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed, cout << "Wheel Count: " << wheels_count << endl;
string manufacturer): cout << "Max Speed: " << max_speed << endl;
wheels_count(wheels_count), cout << "Manufacturer: " << manufacturer << endl;
max_speed(max_speed), cout << "Engine Capacity: " << engine_cc << endl;
manufacturer(manufacturer) { cout << "Gear Count: " << gear_count << endl;
cout << "Vehicle constructor called\n"; }
} };
void Vehicle::vehicle_print() { int main() {
cout << "Wheel Count: " << wheels_count << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Max Speed: " << max_speed << endl; automobile.print();
cout << "Manufacturer: " << manufacturer << endl; return 0;
} }

class Automobile : public Vehicle{


float engine_cc;
int gear_count;
public:
class Vehicle { Automobile(int wheels_count, float max_speed,
protected: string manufacturer,
int wheels_count; float engine_cc, int gear_count):
float max_speed; engine_cc(engine_cc),
string manufacturer; gear_count(gear_count),
Vehicle(wheels_count, max_speed, manufacturer) {
Vehicle(int, float, string); cout << "Automobile constructor called\n";
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed, cout << "Wheel Count: " << wheels_count << endl;
string manufacturer): cout << "Max Speed: " << max_speed << endl;
wheels_count(wheels_count), cout << "Manufacturer: " << manufacturer << endl;
max_speed(max_speed), cout << "Engine Capacity: " << engine_cc << endl;
manufacturer(manufacturer) { cout << "Gear Count: " << gear_count << endl;
cout << "Vehicle constructor called\n"; }
} };
void Vehicle::vehicle_print() { int main() {
cout << "Wheel Count: " << wheels_count << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Max Speed: " << max_speed << endl; // error: 'void Vehicle::vehicle_print()' is protected
cout << "Manufacturer: " << manufacturer << endl; automobile.vehicle_print();
} return 0;
}
class Automobile : public Vehicle{
float engine_cc;
int gear_count;
public:
Inheritance: access specifiers
Access Specifier Access from methods Access from methods Acess from methods of
of class itself of derived class other classes and
independent functions
public Allowed Allowed Allowed

protected Allowed Allowed Not Allowed

private Allowed Not Allowed Not Allowed


class Vehicle { Automobile(int wheels_count, float max_speed,
int wheels_count; string manufacturer,
protected: float engine_cc, int gear_count):
float max_speed; engine_cc(engine_cc),
public: gear_count(gear_count),
string manufacturer; Vehicle(wheels_count, max_speed, manufacturer) {
Vehicle(int, float, string); cout << "Automobile constructor called\n";
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed // error: ‘int Vehicle::wheels_count’ is private
string manufacturer): cout << "Wheel Count: " << wheels_count << endl;
wheels_count(wheels_count), cout << "Max Speed: " << max_speed << endl;
max_speed(max_speed), cout << "Manufacturer: " << manufacturer << endl;
manufacturer(manufacturer){ cout << "Engine Capacity: " << engine_cc << endl;
cout << "Vehicle constructor called\n"; cout << "Gear Count: " << gear_count << endl;
} }
void Vehicle::vehicle_print() { };
cout << "Wheel Count: " << wheels_count << endl; int main() {
cout << "Max Speed: " << max_speed << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Manufacturer: " << manufacturer << endl; // error: ‘int Vehicle::wheels_count’ is private
} cout << "Wheel Count: " << automobile.wheels_count << endl;
// error: ‘float Vehicle::max_speed’ is protected
class Automobile : public Vehicle{ cout << "Max Speed: " << automobile.max_speed << endl;
float engine_cc; cout << "Manufacturer: " << automobile.manufacturer << endl;
int gear_count; automobile.print();
public: return 0;
}
Vehicle constructor called
class Vehicle { Automobile(int wheels_count, float max_speed,
Automobile constructor
int wheels_count; string manufacturer,
called
protected: float engine_cc, int gear_count):
Manufacturer: Honda
float max_speed; engine_cc(engine_cc),
Max Speed: 100
public: gear_count(gear_count),
Manufacturer: Honda
string manufacturer; Vehicle(wheels_count, max_speed, manufacturer) {
Engine Capacity: 125
Vehicle(int, float, string); cout << "Automobile constructor called\n";
Gear Count: 4
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed // error: ‘int Vehicle::wheels_count’ is private
string manufacturer): // cout << "Wheel Count: " << wheels_count << endl;
wheels_count(wheels_count), cout << "Max Speed: " << max_speed << endl;
max_speed(max_speed), cout << "Manufacturer: " << manufacturer << endl;
manufacturer(manufacturer){ cout << "Engine Capacity: " << engine_cc << endl;
cout << "Vehicle constructor called\n"; cout << "Gear Count: " << gear_count << endl;
} }
void Vehicle::vehicle_print() { };
cout << "Wheel Count: " << wheels_count << endl; int main() {
cout << "Max Speed: " << max_speed << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Manufacturer: " << manufacturer << endl; // error: ‘int Vehicle::wheels_count’ is private
} // cout << "Wheel Count: " << automobile.wheels_count << endl;
// error: ‘float Vehicle::max_speed’ is protected
class Automobile : public Vehicle{ // cout << "Max Speed: " << automobile.max_speed << endl;
float engine_cc; cout << "Manufacturer: " << automobile.manufacturer << endl;
int gear_count; automobile.print();
public: return 0;
}
Inheritance: visibility mode
class derived-class : visibility-mode base-
class
Visibility mode Base class Derived class
public public public
protected protected
private No access
protected public protected
protected protected
private No access
private public private
protected private
private No access
Vehicle constructor called
class Vehicle { Automobile(int wheels_count, float max_speed,
Automobile constructor
int wheels_count; string manufacturer,
called
protected: float engine_cc, int gear_count):
Max Speed: 100
float max_speed; engine_cc(engine_cc),
Manufacturer: Honda
public: gear_count(gear_count),
Engine Capacity: 125
string manufacturer; Vehicle(wheels_count, max_speed, manufacturer) {
Gear Count: 4
Vehicle(int, float, string); cout << "Automobile constructor called\n";
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed // error: ‘int Vehicle::wheels_count’ is private
string manufacturer): // cout << "Wheel Count: " << wheels_count << endl;
wheels_count(wheels_count), cout << "Max Speed: " << max_speed << endl;
max_speed(max_speed), cout << "Manufacturer: " << manufacturer << endl;
manufacturer(manufacturer){ cout << "Engine Capacity: " << engine_cc << endl;
cout << "Vehicle constructor called\n"; cout << "Gear Count: " << gear_count << endl;
} }
void Vehicle::vehicle_print() { };
cout << "Wheel Count: " << wheels_count << endl; int main() {
cout << "Max Speed: " << max_speed << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Manufacturer: " << manufacturer << endl; // error: ‘int Vehicle::wheels_count’ is private
} // cout << "Wheel Count: " << automobile.wheels_count << endl;
// error: ‘float Vehicle::max_speed’ is protected
class Automobile : protected Vehicle{ // cout << "Max Speed: " << automobile.max_speed << endl;
float engine_cc; // error: string Vehicle::manufacturer is inaccessible
int gear_count; // cout << "Manufacturer: " << automobile.manufacturer << endl;
public: automobile.print();
return 0;
}
Vehicle constructor called
class Vehicle { Automobile(int wheels_count, float max_speed,
Automobile constructor
int wheels_count; string manufacturer,
called
protected: float engine_cc, int gear_count):
Max Speed: 100
float max_speed; engine_cc(engine_cc),
Manufacturer: Honda
public: gear_count(gear_count),
Engine Capacity: 125
string manufacturer; Vehicle(wheels_count, max_speed, manufacturer) {
Gear Count: 4
Vehicle(int, float, string); cout << "Automobile constructor called\n";
void vehicle_print(); }
}; void print() {
Vehicle::Vehicle(int wheels_count, float max_speed // error: ‘int Vehicle::wheels_count’ is private
string manufacturer): // cout << "Wheel Count: " << wheels_count << endl;
wheels_count(wheels_count), cout << "Max Speed: " << max_speed << endl;
max_speed(max_speed), cout << "Manufacturer: " << manufacturer << endl;
manufacturer(manufacturer){ cout << "Engine Capacity: " << engine_cc << endl;
cout << "Vehicle constructor called\n"; cout << "Gear Count: " << gear_count << endl;
} }
void Vehicle::vehicle_print() { };
cout << "Wheel Count: " << wheels_count << endl; int main() {
cout << "Max Speed: " << max_speed << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Manufacturer: " << manufacturer << endl; // error: ‘int Vehicle::wheels_count’ is private
} // cout << "Wheel Count: " << automobile.wheels_count << endl;
// error: ‘float Vehicle::max_speed’ is protected
class Automobile : private Vehicle{ // cout << "Max Speed: " << automobile.max_speed << endl;
float engine_cc; // error: string Vehicle::manufacturer is inaccessible
int gear_count; // cout << "Manufacturer: " << automobile.manufacturer << endl;
public: automobile.print();
return 0;
}
class Vehicle { class Automobile : public Vehicle{ class PassangerVehicle : public Automobile {
int wheels_count; float engine_cc; int max_passangers;
protected: protected: public:
float max_speed; int gear_count; PassangerVehicle(int wheels_count, float max_speed,
public: public: string manufacturer, float engine_cc,
string manufacturer; Automobile(int wheels_count, float max_speed, int gear_count, int max_passangers):
Vehicle(int, float, string); string manufacturer, float engine_cc, max_passangers(max_passangers),
void vehicle_print(); int gear_count): Automobile(wheels_count, max_speed, manufacturer,
}; engine_cc(engine_cc), engine_cc, gear_count) {
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count), cout << "PassangerVehicle constructor called\n";
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) { }
wheels_count(wheels_count), cout << "Automobile constructor called\n"; void print() {
max_speed(max_speed), } // error: ‘int Vehicle::wheels_count’ is private
manufacturer(manufacturer){ void automobile_print() { // cout << "Wheel Count: " << wheels_count << endl;
cout << "Vehicle constructor called\n"; // error: ‘int Vehicle::wheels_count’ is private cout << "Max Speed: " << max_speed << endl;
} // cout << "Wheel Count: " << wheels_count << endl; cout << "Manufacturer: " << manufacturer << endl;
void Vehicle::vehicle_print() { cout << "Max Speed: " << max_speed << endl; // error: 'float Automobile::engine_cc' is private
cout << "Wheel Count: " << wheels_count << endl; cout << "Manufacturer: " << manufacturer << endl; // cout << "Eng Cap: " << engine_cc << endl;
cout << "Max Speed: " << max_speed << endl; cout << "Engine Capacity: " << engine_cc << endl; cout << "Gear Count: " << gear_count << endl;
cout << "Manufacturer: " << manufacturer << endl; cout << "Gear Count: " << gear_count << endl; cout << "Max Passan: " << max_passangers << endl;
} } }
Vehicle constructor called }; };
Automobile constructor called
PassangerVehicle constructor int main() {
called PassangerVehicle pv(2, 100, "Honda", 125, 4, 60);
Manufacturer: Honda Vehicle Automobile PassangerVehicle pv (object in main) // error: ‘int Vehicle::wheels_count’ is private
Max Speed: 100 wheels_count wheels_count wheels_count wheels_count // cout << "Wheel Count: " << pv.wheels_count << endl;
Manufacturer: Honda max_speed max_speed max_speed max_speed // error: ‘float Vehicle::max_speed’ is protected
Gear Count: 4 manufacturer manufacturer manufacturer manufacturer // cout << "Max Speed: " << pv.max_speed << endl;
Max Passan: 60 vehicle_print vehicle_print vehicle_print vehicle_print
engine_cc engine_cc engine_cc cout << "Manufacturer: " << pv.manufacturer << endl;
gear_count gear_count gear_count // error: 'float Automobile::engine_cc' is private
Vehicle automobile_print automobile_print automobile_print // cout << "Engine Capacity: " << pv.engine_cc << endl;
max_passangers max_passangers
↑ // error: 'int Automobile::gear_count' is protected
print print
// cout << "Gear Count: " << pv.gear_count << endl;
Automobile pv.print();
↑ return 0;
PassangerVehicle }
class Vehicle { class Automobile : protected Vehicle{ class PassangerVehicle : public Automobile {
int wheels_count; float engine_cc; int max_passangers;
protected: protected: public:
float max_speed; int gear_count; PassangerVehicle(int wheels_count, float max_speed,
public: public: string manufacturer, float engine_cc,
string manufacturer; Automobile(int wheels_count, float max_speed, int gear_count, int max_passangers):
Vehicle(int, float, string); string manufacturer, float engine_cc, max_passangers(max_passangers),
void vehicle_print(); int gear_count): Automobile(wheels_count, max_speed, manufacturer,
}; engine_cc(engine_cc), engine_cc, gear_count) {
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count), cout << "PassangerVehicle constructor called\n";
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) { }
wheels_count(wheels_count), cout << "Automobile constructor called\n"; void print() {
max_speed(max_speed), } // error: ‘int Vehicle::wheels_count’ is private
manufacturer(manufacturer){ void automobile_print() { // cout << "Wheel Count: " << wheels_count << endl;
cout << "Vehicle constructor called\n"; // error: ‘int Vehicle::wheels_count’ is private cout << "Max Speed: " << max_speed << endl;
} // cout << "Wheel Count: " << wheels_count << endl; cout << "Manufacturer: " << manufacturer << endl;
void Vehicle::vehicle_print() { cout << "Max Speed: " << max_speed << endl; // error: 'float Automobile::engine_cc' is private
cout << "Wheel Count: " << wheels_count << endl; cout << "Manufacturer: " << manufacturer << endl; // cout << "Eng Cap: " << engine_cc << endl;
cout << "Max Speed: " << max_speed << endl; cout << "Engine Capacity: " << engine_cc << endl; cout << "Gear Count: " << gear_count << endl;
cout << "Manufacturer: " << manufacturer << endl; cout << "Gear Count: " << gear_count << endl; cout << "Max Passan: " << max_passangers << endl;
} } }
Vehicle constructor called }; };
Automobile constructor called
PassangerVehicle constructor int main() {
called PassangerVehicle pv(2, 100, "Honda", 125, 4, 60);
Max Speed: 100 Vehicle Automobile PassangerVehicle pv (object in main) // error: ‘int Vehicle::wheels_count’ is private
Manufacturer: Honda wheels_count wheels_count wheels_count wheels_count // cout << "Wheel Count: " << pv.wheels_count << endl;
Gear Count: 4 max_speed max_speed max_speed max_speed // error: ‘float Vehicle::max_speed’ is protected
Max Passan: 60 manufacturer manufacturer manufacturer manufacturer // cout << "Max Speed: " << pv.max_speed << endl;
vehicle_print vehicle_print vehicle_print vehicle_print // error: string Vehicle::manufacturer is inaccessible
engine_cc engine_cc engine_cc // cout << "Manufacturer: " << pv.manufacturer << endl;
gear_count gear_count gear_count // error: 'float Automobile::engine_cc' is private
automobile_print automobile_print automobile_print
Vehicle max_passangers max_passangers
// cout << "Engine Capacity: " << pv.engine_cc << endl;
// error: 'int Automobile::gear_count' is protected
↑ print print
// cout << "Gear Count: " << pv.gear_count << endl;
Automobile pv.print();
↑ return 0;
}
PassangerVehicle
class Vehicle { class Automobile : private Vehicle{ class PassangerVehicle : public Automobile {
int wheels_count; float engine_cc; int max_passangers;
protected: protected: public:
float max_speed; int gear_count; PassangerVehicle(int wheels_count, float max_speed,
public: public: string manufacturer, float engine_cc,
string manufacturer; Automobile(int wheels_count, float max_speed, int gear_count, int max_passangers):
Vehicle(int, float, string); string manufacturer, float engine_cc, max_passangers(max_passangers),
void vehicle_print(); int gear_count): Automobile(wheels_count, max_speed, manufacturer,
}; engine_cc(engine_cc), engine_cc, gear_count) {
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count), cout << "PassangerVehicle constructor called\n";
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) { }
wheels_count(wheels_count), cout << "Automobile constructor called\n"; void print() {
max_speed(max_speed), } // error: ‘int Vehicle::wheels_count’ is private
manufacturer(manufacturer){ void automobile_print() { // cout << "Wheel Count: " << wheels_count << endl;
cout << "Vehicle constructor called\n"; // error: ‘int Vehicle::wheels_count’ is private // error: 'float Vahicle::max_speed' is protected
} // cout << "Wheel Count: " << wheels_count << endl; // cout << "Max Speed: " << max_speed << endl;
void Vehicle::vehicle_print() { cout << "Max Speed: " << max_speed << endl; // error: 'string Vehicle::manufacturer' is inaccessible
cout << "Wheel Count: " << wheels_count << endl; cout << "Manufacturer: " << manufacturer << endl; // cout << "Manufacturer: " << manufacturer << endl;
cout << "Max Speed: " << max_speed << endl; cout << "Engine Capacity: " << engine_cc << endl; // error: 'float Automobile::engine_cc' is private
cout << "Manufacturer: " << manufacturer << endl; cout << "Gear Count: " << gear_count << endl; // cout << "Eng Cap: " << engine_cc << endl;
} } cout << "Gear Count: " << gear_count << endl;
Vehicle constructor called }; cout << "Max Passan: " << max_passangers << endl;
Automobile constructor called }
PassangerVehicle constructor };
called
Gear Count: 4 Vehicle Automobile PassangerVehicle pv (object in main) int main() {
Max Passan: 60 wheels_count wheels_count wheels_count wheels_count PassangerVehicle pv(2, 100, "Honda", 125, 4, 60);
max_speed max_speed max_speed max_speed // error: ‘int Vehicle::wheels_count’ is private
manufacturer manufacturer manufacturer manufacturer // cout << "Wheel Count: " << pv.wheels_count << endl;
vehicle_print vehicle_print vehicle_print vehicle_print // error: ‘float Vehicle::max_speed’ is protected
engine_cc engine_cc engine_cc // cout << "Max Speed: " << pv.max_speed << endl;
gear_count gear_count gear_count // error: string Vehicle::manufacturer is inaccessible
automobile_print automobile_print automobile_print
Vehicle max_passangers max_passangers
// cout << "Manufacturer: " << pv.manufacturer << endl;
// error: 'float Automobile::engine_cc' is private
↑ print print
// cout << "Engine Capacity: " << pv.engine_cc << endl;
Automobile // error: 'int Automobile::gear_count' is protected
↑ // cout << "Gear Count: " << pv.gear_count << endl;
pv.print();
PassangerVehicle return 0;
}
class Vehicle { class Automobile : public Vehicle{ int main() {
int wheels_count; float engine_cc; Vehicle v(2, 100, "Honda");
protected: protected: cout << "Wheel Count: " << v.wheels_count << endl;
float max_speed; int gear_count; cout << "Max Speed: " << v.max_speed << endl;
public: public: cout << "Manufacturer: " << v.manufacturer << endl;
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc, Automobile a(2, 100, "Honda", 125, 4);
void vehicle_print(); int gear_count): cout << "Wheel Count: " << a.wheels_count << endl;
}; engine_cc(engine_cc), cout << "Max Speed: " << a.max_speed << endl;
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count), cout << "Manufacturer: " << a.manufacturer << endl;
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) { cout << "Engine Capacity: " << a.engine_cc << endl;
wheels_count(wheels_count), cout << "Automobile constructor called\n"; cout << "Gear Count: " << a.gear_count << endl;
max_speed(max_speed), }
manufacturer(manufacturer){ void automobile_print() { return 0;
cout << "Vehicle constructor called\n"; // error: ‘int Vehicle::wheels_count’ is private }
} // cout << "Wheel Count: " << wheels_count << endl;
void Vehicle::vehicle_print() { cout << "Max Speed: " << max_speed << endl;
cout << "Wheel Count: " << wheels_count << endl; cout << "Manufacturer: " << manufacturer << endl;
cout << "Max Speed: " << max_speed << endl; cout << "Engine Capacity: " << engine_cc << endl;
cout << "Manufacturer: " << manufacturer << endl; cout << "Gear Count: " << gear_count << endl;
} }
};

Vehicle v (object in main) Automobile a (object in main)


wheels_count ? wheels_count ?
max_speed ? max_speed ?
manufacturer ? manufacturer ?
vehicle_print ? vehicle_print ?
engine_cc ?
gear_count ?
automobile_print ?
Vehicle

Automobile

PassangerVehicle
class Vehicle { int main() {
int wheels_count; Vehicle vehicle(2, 100, "Honda");
protected: vehicle.print();
float max_speed; vehicle.print(true);
public: vehicle.print(false);
string manufacturer; return 0;
Vehicle(int, float, string); }
void print();
void print(bool);
};
Vehicle::Vehicle(int wheels_count, float max_speed,
string manufacturer):
wheels_count(wheels_count),
max_speed(max_speed),
manufacturer(manufacturer){
Guess the output of this
cout << "Vehicle constructor called\n"; code
}
void Vehicle::print() {
cout << "Wheel Count: " << wheels_count << endl;
cout << "Max Speed: " << max_speed << endl;
cout << "Manufacturer: " << manufacturer << endl;
}
void Vehicle::print(bool in_single_line = true) {
if(in_single_line) {
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl;
}
else {
cout << wheels_count << endl << max_speed;
cout << endl << manufacturer << endl;
}
}
class Vehicle { int main() {
int wheels_count; Vehicle vehicle(2, 100, "Honda");
protected: // error: call of overloaded ‘print()’ is ambiguous
float max_speed; vehicle.print();
public: vehicle.print(true);
string manufacturer; vehicle.print(false);
Vehicle(int, float, string); return 0;
void print(); }
void print(bool);
};
Vehicle::Vehicle(int wheels_count, float max_speed,
string manufacturer):
wheels_count(wheels_count),
max_speed(max_speed),
manufacturer(manufacturer){
cout << "Vehicle constructor called\n";
}
void Vehicle::print() {
cout << "Wheel Count: " << wheels_count << endl;
cout << "Max Speed: " << max_speed << endl;
cout << "Manufacturer: " << manufacturer << endl;
}
void Vehicle::print(bool in_single_line = true) {
if(in_single_line) {
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl;
}
else {
cout << wheels_count << endl << max_speed;
cout << endl << manufacturer << endl;
}
}
class Vehicle { int main() {
int wheels_count; Vehicle vehicle(2, 100, "Honda");
protected: vehicle.print();
float max_speed; vehicle.print(true);
public: vehicle.print(false);
string manufacturer; return 0;
Vehicle(int, float, string); }
void print();
void print(bool);
};
Vehicle::Vehicle(int wheels_count, float max_speed,
string manufacturer):
wheels_count(wheels_count),
max_speed(max_speed),
manufacturer(manufacturer){
Guess the output of this
cout << "Vehicle constructor called\n"; code
}
void Vehicle::print() {
cout << "Wheel Count: " << wheels_count << endl;
cout << "Max Speed: " << max_speed << endl;
cout << "Manufacturer: " << manufacturer << endl;
}
void Vehicle::print(bool in_single_line) {
if(in_single_line) {
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl;
}
else {
cout << wheels_count << endl << max_speed;
cout << endl << manufacturer << endl;
}
}
class Vehicle { int main() {
int wheels_count; Vehicle vehicle(2, 100, "Honda");
protected: vehicle.print();
float max_speed; vehicle.print(true);
public: vehicle.print(false);
string manufacturer; return 0;
Vehicle(int, float, string); }
void print();
void print(bool);
};
Vehicle::Vehicle(int wheels_count, float max_speed,
string manufacturer):
wheels_count(wheels_count),
max_speed(max_speed),
manufacturer(manufacturer){
Vehicle constructor
cout << "Vehicle constructor called\n"; called
} Wheel Count: 2
void Vehicle::print() {
cout << "Wheel Count: " << wheels_count << endl; Max Speed: 100
cout << "Max Speed: " << max_speed << endl; Manufacturer: Honda
cout << "Manufacturer: " << manufacturer << endl;
} 2 100 Honda
void Vehicle::print(bool in_single_line) { 2
if(in_single_line) {
cout << wheels_count << " " << max_speed;
100
cout << " " << manufacturer << endl; Honda
}
else {
cout << wheels_count << endl << max_speed;
cout << endl << manufacturer << endl;
}
}
class Vehicle { class Automobile : public Vehicle{
int wheels_count; float engine_cc;
protected: protected:
float max_speed; int gear_count;
public: public:
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc,
void print(bool); int gear_count):
}; engine_cc(engine_cc),
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count),
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) {
wheels_count(wheels_count), cout << "Automobile constructor called\n";
max_speed(max_speed), }
manufacturer(manufacturer){ void print() {
cout << "Vehicle constructor called\n"; cout << max_speed << endl << manufacturer << endl;
} cout << engine_cc << endl << gear_count << endl;
void Vehicle::print(bool in_single_line) { }
if(in_single_line) { };
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
else { automobile.print();
cout << wheels_count << endl << max_speed; return 0;
cout << endl << manufacturer << endl; }
}
}
Vehicle constructor called
Automobile constructor
called
100
Honda
125
4
class Vehicle { class Automobile : public Vehicle{
int wheels_count; float engine_cc;
protected: protected:
float max_speed; int gear_count;
public: public:
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc,
void print(bool); int gear_count):
}; engine_cc(engine_cc),
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count),
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) {
wheels_count(wheels_count), cout << "Automobile constructor called\n";
max_speed(max_speed), }
manufacturer(manufacturer){ void print() {
cout << "Vehicle constructor called\n"; cout << max_speed << endl << manufacturer << endl;
} cout << engine_cc << endl << gear_count << endl;
void Vehicle::print(bool in_single_line) { }
if(in_single_line) { };
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
else { automobile.print(true);
cout << wheels_count << endl << max_speed; return 0;
cout << endl << manufacturer << endl; }
}
}

Guess the
output
class Vehicle { class Automobile : public Vehicle{
int wheels_count; float engine_cc;
protected: protected:
float max_speed; int gear_count;
public: public:
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc,
void print(bool); int gear_count):
}; engine_cc(engine_cc),
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count),
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) {
wheels_count(wheels_count), cout << "Automobile constructor called\n";
max_speed(max_speed), }
manufacturer(manufacturer){ void print() {
cout << "Vehicle constructor called\n"; cout << max_speed << endl << manufacturer << endl;
} cout << engine_cc << endl << gear_count << endl;
void Vehicle::print(bool in_single_line) { }
if(in_single_line) { };
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
else { // error: no matching function for call to
cout << wheels_count << endl << max_speed; // ‘Automobile::print(bool)
cout << endl << manufacturer << endl; automobile.print(true);
} return 0;
} }


Functions/methods can be overloaded within the same scope only

Here Vehicle and Automobile are two differenct classes and hence different scopes

When we define print function in Automobile then all the print functions of the Vehicle class are
inaccessible in Automobile class
class Vehicle { class Automobile : public Vehicle{
int wheels_count; float engine_cc;
protected: protected:
float max_speed; int gear_count;
public: public:
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc,
void print(bool); int gear_count):
void print(); engine_cc(engine_cc),
}; gear_count(gear_count),
Vehicle::Vehicle(int wheels_count, float max_speed, Vehicle(wheels_count, max_speed, manufacturer) {
string manufacturer): cout << "Automobile constructor called\n";
wheels_count(wheels_count), }
max_speed(max_speed), void print() {
manufacturer(manufacturer){ cout << max_speed << endl << manufacturer << endl;
cout << "Vehicle constructor called\n"; cout << engine_cc << endl << gear_count << endl;
} }
void Vehicle::print(bool in_single_line) { };
if(in_single_line) {
cout << wheels_count << " " << max_speed; int main() {
cout << " " << manufacturer << endl; Automobile automobile(2, 100, "Honda", 125, 4);
} automobile.print();
else { return 0;
cout << wheels_count << endl << max_speed; }
cout << endl << manufacturer << endl;
}
} Vehicle constructor called
void Vehicle::print() { Automobile constructor
cout << "Wheel Count: " << wheels_count << endl;
cout << "Max Speed: " << max_speed << endl; called
cout << "Manufacturer: " << manufacturer << endl; 100
} Honda
125
class Vehicle { class Automobile : public Vehicle{
int wheels_count; float engine_cc;
protected: protected:
float max_speed; int gear_count;
public: public:
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc,
void print(bool); int gear_count):
void print(); engine_cc(engine_cc),
}; gear_count(gear_count),
Vehicle::Vehicle(int wheels_count, float max_speed, Vehicle(wheels_count, max_speed, manufacturer) {
string manufacturer): cout << "Automobile constructor called\n";
wheels_count(wheels_count), }
max_speed(max_speed), void print(bool in_single_line) {
manufacturer(manufacturer){ if(in_single_line) {
cout << "Vehicle constructor called\n"; cout << max_speed << " " << manufacturer;
} cout << " " << engine_cc << " " << gear_count;
void Vehicle::print(bool in_single_line) { cout << endl;
if(in_single_line) { }
cout << wheels_count << " " << max_speed; else {
cout << " " << manufacturer << endl; cout << max_speed << endl << manufacturer;
} cout << endl << engine_cc <<endl << gear_count;
else { cout << endl;
cout << wheels_count << endl << max_speed; }
cout << endl << manufacturer << endl;
} }
} };
void Vehicle::print() { int main() {
cout << "Wheel Count: " << wheels_count << endl; Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Max Speed: " << max_speed << endl; automobile.print(true);
cout << "Manufacturer: " << manufacturer << endl; return 0; Vehicle constructor called
} } Automobile constructor called
100 Honda 125 4
class Vehicle { class Automobile : public Vehicle{ int main() {
int wheels_count; float engine_cc; Automobile automobile(2, 100, "Honda", 125, 4);
protected: float wheels_count = 16; automobile.print(true);
float max_speed; protected: return 0;
public: int gear_count; }
string manufacturer; int max_speed = 1236; // supersonic
Vehicle(int, float, string); public:
void print(bool); string manufacturer = "Magic Corp.";
void print(); Automobile(int wheels_count, float max_speed,
}; string manufacturer, float engine_cc,
Vehicle::Vehicle(int wheels_count, float max_speed, int gear_count):
string manufacturer): engine_cc(engine_cc),
wheels_count(wheels_count), gear_count(gear_count),
max_speed(max_speed), Vehicle(wheels_count, max_speed, manufacturer) {
manufacturer(manufacturer){ cout << "Automobile constructor called\n";
cout << "Vehicle constructor called\n"; }
} void print(bool in_single_line) {
void Vehicle::print(bool in_single_line) { if(in_single_line) {
if(in_single_line) { cout << wheels_count << " ";
cout << wheels_count << " " << max_speed; cout << max_speed << " " << manufacturer;
cout << " " << manufacturer << endl; cout << " " << engine_cc << " " << gear_count;
} cout << endl;
else { }
cout << wheels_count << endl << max_speed; else {
cout << endl << manufacturer << endl; cout << wheels_count << endl;
} cout << max_speed << endl << manufacturer;
} cout << endl << engine_cc <<endl << gear_count;
void Vehicle::print() { cout << endl;
cout << "Wheel Count: " << wheels_count << endl; }
cout << "Max Speed: " << max_speed << endl;
cout << "Manufacturer: " << manufacturer << endl; } Vehicle constructor called
} }; Automobile constructor called
16 1236 Magic Corp. 125 4
class Vehicle { class Automobile : public Vehicle{ int main() {
int wheels_count; float engine_cc; Automobile automobile(2, 100, "Honda", 125, 4);
protected: float wheels_count = 16; automobile.print(true);
float max_speed; protected: cout << automobile.manufacturer << endl;
public: int gear_count; cout << automobile.Vehicle::manufacturer << endl;
string manufacturer; int max_speed = 1236; return 0;
Vehicle(int, float, string); public: }
void print(bool); string manufacturer = "Magic Corp.";
void print(); Automobile(int wheels_count, float max_speed,
}; string manufacturer, float engine_cc,
Vehicle::Vehicle(int wheels_count, float max_speed, int gear_count):
string manufacturer): engine_cc(engine_cc),
wheels_count(wheels_count), gear_count(gear_count),
max_speed(max_speed), Vehicle(wheels_count, max_speed, manufacturer) {
manufacturer(manufacturer){ cout << "Automobile constructor called\n";
cout << "Vehicle constructor called\n"; }
} void print(bool in_single_line) {
void Vehicle::print(bool in_single_line) { if(in_single_line) {
if(in_single_line) { // error:‘int Vehicle::wheels_count’ is private
cout << wheels_count << " " << max_speed; // cout << Vehicle::wheels_count << " "; Vehicle constructor called
cout << " " << manufacturer << endl; cout << Vehicle::max_speed << " "; Automobile constructor called
} cout << Vehicle::manufacturer; 100 Honda 125 4
else { cout << " " << engine_cc << " " << gear_count;
cout << wheels_count << endl << max_speed; cout << endl; Magic Corp.
cout << endl << manufacturer << endl; } else { Honda
} // error:‘int Vehicle::wheels_count’ is private
} // cout << Vehicle::wheels_count << endl;
void Vehicle::print() { cout << Vehicle::max_speed << endl;
cout << "Wheel Count: " << wheels_count << endl; cout << Vehicle::manufacturer;
cout << "Max Speed: " << max_speed << endl; cout << endl << engine_cc << endl << gear_count;
cout << "Manufacturer: " << manufacturer << endl; cout << endl;
} }
}
};
class Vehicle { class Automobile : public Vehicle{
int wheels_count; float engine_cc;
protected: protected:
float max_speed; int gear_count;
public: public:
string manufacturer; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); string manufacturer, float engine_cc,
void print(bool); int gear_count):
}; engine_cc(engine_cc),
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count),
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) {
wheels_count(wheels_count), cout << "Automobile constructor called\n";
max_speed(max_speed), }
manufacturer(manufacturer){ void print() {
cout << "Vehicle constructor called\n"; Vehicle::print(false);
} cout << engine_cc << endl << gear_count << endl;
void Vehicle::print(bool in_single_line) { }
if(in_single_line) { };
cout << wheels_count << " " << max_speed;
cout << " " << manufacturer << endl; int main() {
} Automobile automobile(2, 100, "Honda", 125, 4);
else { // error: no matching function for call to
cout << wheels_count << endl << max_speed; // ‘Automobile::print(bool)
cout << endl << manufacturer << endl; // automobile.print(true);
} automobile.Vehicle::print(true); Vehicle constructor called
} automobile.print(); Automobile constructor called
return 0;
2 100 Honda
}
2
100
Honda
125
4
Inheritance
Constructors, destructor, friend functions, friend classes, and static members
of base class are not inherited by derived class.
Friend fuctions and classes have access to protected members of the class
along with private and public members of the class
class Vehicle { class Automobile : public Vehicle{ void friend_fun1() {
int wheels_count; float engine_cc; cout << "friend_fun1 called\n";
protected: protected: Automobile automobile(2, 100, "Honda", 125, 4);
float max_speed; int gear_count; cout << automobile.wheels_count << endl;
public: public: cout << automobile.max_speed << endl;
string manufacturer; friend void friend_fun2(); cout << automobile.manufacturer << endl;
Vehicle(int, float, string); Automobile(int wheels_count, float max_speed, // error: 'float Automobile::engine_cc' is private
void print(bool); string manufacturer, float engine_cc, // cout << automobile.engine_cc << endl;
friend void friend_fun1(); int gear_count): // error: 'int Automobile::gear_count' is protected
}; engine_cc(engine_cc), // cout << automobile.gear_count << endl;
Vehicle::Vehicle(int wheels_count, float max_speed, gear_count(gear_count), }
string manufacturer): Vehicle(wheels_count, max_speed, manufacturer) {
wheels_count(wheels_count), cout << "Automobile constructor called\n"; void friend_fun2() {
max_speed(max_speed), } cout << "friend_fun2 called\n";
manufacturer(manufacturer){ void print() { Automobile automobile(2, 100, "Honda", 125, 4);
cout << "Vehicle constructor called\n"; Vehicle::print(false); // error: 'int Vehicle::wheels_count' is private
} cout << engine_cc << endl << gear_count << endl; // cout << automobile.wheels_count << endl;
void Vehicle::print(bool in_single_line) { } cout << automobile.max_speed << endl;
if(in_single_line) { }; cout << automobile.manufacturer << endl;
cout << wheels_count << " " << max_speed; cout << automobile.engine_cc << endl;
cout << " " << manufacturer << endl;
}
friend_fun1 called cout << automobile.gear_count << endl;
}
else { Vehicle constructor called
cout << wheels_count << endl << max_speed;
cout << endl << manufacturer << endl;
Automobile constructor called int main() {
friend_fun1();
} 2 friend_fun2();
}
100 return 0;
}
Honda
friend_fun2 called
Vehicle constructor called
Automobile constructor called
100
Honda
125
4
class Vehicle { class Engine { class Automobile : public Vehicle{
int wheels_count; int cylinders_count; Engine e;
protected: protected: protected:
float max_speed; string cylinders_arrangement; //v-type, inline etc... int gear_count;
public: public: public:
string manufacturer; float engine_cc; Automobile(int wheels_count, float max_speed,
Vehicle(int, float, string); Engine(int cylinders_count, string manufacturer, int cylinders_count,
void vehicle_print(); string cylinders_arrangement, float engine_cc): string cylinders_arrangement, float engine_cc,
~Vehicle() { cylinders_count(cylinders_count), int gear_count):
cout << "Vehicle Destructor called\n"; cylinders_arrangement(cylinders_arrangement), gear_count(gear_count),
} engine_cc(engine_cc){ e(cylinders_count, cylinders_arrangement,
}; cout << "Engine constructor called\n"; engine_cc),
Vehicle::Vehicle(int wheels_count, float max_speed, } Vehicle(wheels_count, max_speed, manufacturer) {
string manufacturer): ~Engine() { cout << "Automobile constructor called\n";
wheels_count(wheels_count), cout << "Engine Destructor called\n"; }
max_speed(max_speed), } ~Automobile() {
manufacturer(manufacturer){ void print() { cout << "Automobile Destructor called\n";
cout << "Vehicle constructor called\n"; cout << "cyl cnt: " << cylinders_count << endl; }
} cout << "cyl arr: " << cylinders_arrangement; void print() {
void Vehicle::vehicle_print() { cout << endl << "eng cc: " << engine_cc << endl; vehicle_print();
cout << "Wheel Count: " << wheels_count << endl; } e.print();
cout << "Max Speed: " << max_speed << endl; }; cout << "Gear Count: " << gear_count << endl;
cout << "Manufacturer: " << manufacturer << endl; }
} };

Vehicle constructor called int main() {


Engine constructor called Automobile a(4, 200, "Tata", 4, "V-shape", 1200, 6);
Automobile constructor called a.print();
Wheel Count: 4 return 0;
Max Speed: 200 }
Manufacturer: Tata
cyl cnt: 4
cyl arr: V-shape
eng cc: 1200
Gear Count: 6
Automobile Destructor called
Engine Destructor called
Vehicle Destructor called
Different types of inheritance
class A {
protected:
int num1 = 1;
};

class B {
protected:
int num2 = 2;
};

class C: public A, public B {


protected:
int num3 = 3;
public:
void print() {
cout << num1 << num2 << num3;
}
};

int main() {
C c;
c.print();
return 0;
}
123
class A { class C: public A, public B {
protected: protected:
int num1 = 1; int num3 = 3;
public: public:
A() { C() {
cout << "A constructor called\n"; cout << "C constructor called\n";
} }
~A() { ~C() {
cout << "A destructor called\n"; cout << "C destructor called\n";
} }
}; void print() {
cout << num1 << num2 << num3 << endl;
class B { }
protected: };
int num2 = 2;
public: int main() {
B() { C c;
cout << "B constructor called\n"; c.print();
} return 0; A constructor called
~B() { } B constructor called
cout << "B destructor called\n"; C constructor called
} 123
}; C destructor called
B destructor called
A destructor called
class A { class C: public B, public A {
protected: protected:
int num1; int num3 = 3;
public: public:
A(int n): num1(n) { B b;
cout << "A constructor called\n"; C(int num1, int num2, int num3, int num4):
} A(num2),
~A() { B(num1),
cout << "A destructor called\n"; num3(num3),
} b(num4) {
}; cout << "C constructor called\n";
}
class B { ~C() {
protected: cout << "C destructor called\n";
int num2 = 2; }
public: void print() {
B(int n): num2(n) { cout << num1 << num2 << num3;
cout << "B constructor called\n"; } B constructor called
} }; A constructor called
~B() { B constructor called
cout << "B destructor called\n"; int main() { C constructor called
} C c(1, 3, 2, 4); 3124
void print() { c.print(); C destructor called
cout << num2 << endl; c.b.print(); B destructor called
} return 0; A destructor called
}; } B destructor called
class A { class C: public B, public A {
protected: protected:
int num1; int num3;
public: public:
A(int n): num1(n) { B b;
cout << "A constructor called\n"; C(int num1, int num2, int num3, int num4):
} A(num2),
~A() { B(num1),
cout << "A destructor called\n"; num3(num3),
} b(num4) {
}; cout << "C constructor called\n";
}
class B { ~C() {
protected: cout << "C destructor called\n";
int num2; }
public: void print() {
B(int n): num2(n) { cout << num1 << num2 << num3;
cout << "B constructor called\n"; } B constructor called
} }; A constructor called
~B() { B constructor called
cout << "B destructor called\n"; int main() { C constructor called
} C c(1, 3, 2, 4); 3124
void print() { c.print(); C destructor called
cout << num2 << endl; c.b.print(); B destructor called
} return 0; A destructor called
}; } B destructor called
class A { class C: public B, public A {
protected: protected:
int num1; int num3 = 1;
int num4;
public:
public:
A(int n): num1(n) { B b;
cout << "A constructor called\n"; C(int n1, int n2, int n3, int n4):
} A(n2), B constructor called
~A() { B(n1),
A constructor called
cout << "A destructor called\n"; num4(num3 * 2),
num3(n3),
B constructor called
} C constructor called
}; b(n4) {
cout << "C constructor called\n"; 31244
} C destructor called
class B { ~C() { B destructor called
protected: cout << "C destructor called\n"; A destructor called
int num2; } B destructor called
public: void print() {
B(int n): num2(n) { cout << num1 << num2 << num3 << num4;
cout << "B constructor called\n"; }
} };
int main() {
~B() {
C c(1, 3, 2, 4);
cout << "B destructor called\n"; c.print();
} c.b.print();
void print() { return 0;
cout << num2 << endl; }
}
};
class A { class C: public B, public A {
protected: protected:
int num1; int num3 = 1;
int num4;
public:
public:
A(int n): num1(n) { B b;
cout << "A constructor called\n"; C(int n1, int n2, int n3, int n4):
} A(n2),
~A() { B(n1),
cout << "A destructor called\n"; num4(num3 * 2),
} num3(n3),
}; b(n4) {
cout << "C constructor called\n";
}
class B { ~C() {
protected: cout << "C destructor called\n";
int num1; }
public: void print() {
B(int n): num1(n) { // error: reference to 'num1' is ambiguous
cout << "B constructor called\n"; cout << num1 << num1 << num3 << num4;
} }
};
~B() {
int main() {
cout << "B destructor called\n"; C c(1, 3, 2, 4);
} c.print();
void print() { c.b.print();
cout << num1 << endl; return 0;
} }
};
class A { class C: public B, public A {
protected: protected:
int num1; int num3 = 1;
int num4;
public:
public:
A(int n): num1(n) { B b;
cout << "A constructor called\n"; C(int n1, int n2, int n3, int n4):
} A(n2), B constructor called
~A() { B(n1),
A constructor called
cout << "A destructor called\n"; num4(num3 * 2),
num3(n3),
B constructor called
} C constructor called
}; b(n4) {
cout << "C constructor called\n"; 31244
} C destructor called
class B { ~C() { B destructor called
protected: cout << "C destructor called\n"; A destructor called
int num1; } B destructor called
public: void print() {
B(int n): num1(n) { cout << A::num1 << B::num1 << num3 << num4;
cout << "B constructor called\n"; }
} };
int main() {
~B() {
C c(1, 3, 2, 4);
cout << "B destructor called\n"; c.print();
} c.b.print();
void print() { return 0;
cout << num1 << endl; }
}
};
class A { class D: public B, public C {
public: public:
int num1 = 1; D(){
A(){
cout << "D constructor called\n";
cout << "A constructor called\n";
} }
~A() { ~D() {
cout << "A destructor called\n"; cout << "D destructor called\n";
} }
}; void print() {
class B: public A { // error: reference to num1 is ambiguous
public: cout << num1 << endl;
B(){
}
cout << "B constructor called\n";
} };
~B() {
cout << "B destructor called\n"; int main() {
} D d;
}; d.C::num1 = 7;
class C: public A { d.print();
public: return 0;
C(){
}
cout << "C constructor called\n";
}
~C() {
cout << "C destructor called\n";
}
};
class A { class D: public B, public C {
public: public:
int num1 = 1; D(){
A(){
cout << "D constructor called\n";
cout << "A constructor called\n";
} }
~A() { ~D() {
cout << "A destructor called\n"; cout << "D destructor called\n";
} }
}; void print() {
class B: public A { // error: 'A' is an ambiguous base of 'D'
public: cout << A::num1 << endl;
B(){
}
cout << "B constructor called\n";
} };
~B() {
cout << "B destructor called\n"; int main() {
} D d;
}; d.C::num1 = 7;
class C: public A { d.print();
public: return 0;
C(){
}
cout << "C constructor called\n";
}
~C() {
cout << "C destructor called\n";
}
};
class A { class D: public B, public C {
public: public:
int num1 = 1; D(){
A(){
cout << "D constructor called\n";
cout << "A constructor called\n";
} }
~A() { ~D() {
cout << "A destructor called\n"; cout << "D destructor called\n";
} }
}; void print() {
class B: public A {
public: cout << B::num1 << " " << C::num1 << endl;
B(){
}
cout << "B constructor called\n";
} };
~B() { A constructor called
cout << "B destructor called\n"; int main() { B constructor called
} D d; A constructor called
}; d.C::num1 = 7; C constructor called
class C: public A { d.print();
public:
D constructor called
return 0; 17
C(){
} D destructor called
cout << "C constructor called\n";
} C destructor called
~C() { A destructor called
cout << "C destructor called\n"; B destructor called
} A destructor called
};
class A { class D: public B, public C {
public: public:
int num1 = 1; D(){
A(){
cout << "D constructor called\n";
cout << "A constructor called\n";
} }
~A() { ~D() {
cout << "A destructor called\n"; cout << "D destructor called\n";
} }
}; void print() {
class B: virtual public A {
public: cout << B::num1 << " " << C::num1 << endl;
B(){
}
cout << "B constructor called\n";
} };
~B() { A constructor called
cout << "B destructor called\n"; int main() { B constructor called
} D d; C constructor called
}; d.C::num1 = 7; D constructor called
class C: virtual public A { d.print();
public:
77
return 0; D destructor called
C(){
} C destructor called
cout << "C constructor called\n";
} B destructor called
~C() { A destructor called
cout << "C destructor called\n";
}
};
class A { class E {
public: public:
int num1 = 1; E(){ A constructor called
A(){ cout << "E constructor called\n"; E constructor called
cout << "A constructor called\n"; } B constructor called
} ~E() { C constructor called
~A() { cout << "E destructor called\n"; D constructor called
cout << "A destructor called\n"; } 77
} }; D destructor called
}; class D: public E, public B, public C {
C destructor called
class B: virtual public A { public:
public: D(){ B destructor called
B(){ cout << "D constructor called\n"; E destructor called
cout << "B constructor called\n"; } A destructor called
} ~D() {
~B() { cout << "D destructor called\n";
cout << "B destructor called\n"; }
} void print() {
}; cout << B::num1 << " " << C::num1 << endl;
class C: virtual public A { }
public: };
C(){ int main() {
cout << "C constructor called\n"; D d;
} d.C::num1 = 7;
~C() { d.print();
cout << "C destructor called\n"; return 0;
} }
};
Virtual base class is constructed before any other class in multiple inheritance
class A { class E: virtual public A {
public: public:
int num1 = 1; E(){ A constructor called
A(){ cout << "E constructor called\n"; E constructor called
cout << "A constructor called\n"; } A constructor called
} ~E() { B constructor called
~A() { cout << "E destructor called\n"; C constructor called
cout << "A destructor called\n"; } D constructor called
} }; 177
}; class D: public E, public B, public C {
D destructor called
class B: public A { public:
public: D(){ C destructor called
B(){ cout << "D constructor called\n"; B destructor called
cout << "B constructor called\n"; } A destructor called
} ~D() { E destructor called
~B() { cout << "D destructor called\n"; A destructor called
cout << "B destructor called\n"; }
} void print() {
}; cout << B::num1 << " " << C::num1;
class C: virtual public A { cout << " " << E::num1 << endl;
public: }
C(){ };
cout << "C constructor called\n"; int main() {
} D d;
~C() { d.C::num1 = 7;
cout << "C destructor called\n"; d.print();
} return 0;
}; }
Virtual base class is constructed before any other class in multiple inheritance
Interesting reads

Accessing protected members in a derived class trhough object of base
class is not permitted

https://stackoverflow.com/questions/3247671/accessing-protected-
members-in-a-derived-class

Inheritance of constructors and destructor

https://stackoverflow.com/questions/14184341/c-constructor-destructor-
inheritance

You might also like