Public Inheritance
Public Inheritance

public inheritance

is-a relationship

base class: Employee

derived class: Manager

You can do with inheritance

add data

ex. dapartment

add functionality

ex. getDapartment(), setDepartment()

Modify method's behavior

ex. print()

Employee
- firstName: string
- lastName: string
- salary: double
+ Employee(string, string, double)
+ getFirstName(): string{query}
+ ...
+ setFirstName(string firstName): void
+ ...
+ print(iostream&):void{query}
Manager
-department: string
+ Manager()
+ Manager(string, string, double, string)
+ setDepartment(string):void
+ getDepartment(): string{query}
+ print(iostream&):void{query}
Public Inheritance

Protected access

Base class's private members can not be accessed in a derived class.

Base class's protected members can be accessed in a derived class.

Base class's public members can be accessed from anywhere.

Public Inheritance

public Inheritance

class Employee{
public:
	Employee(string firstName, string lastname, double salary):
		firstName(firstName), lastName(lastName), salary(salary){

	}
};
class Manager : public Employee{
	string department;
public:
	Manage();
	Manager(string firstName, string lastName, double salary, string department);	
};
			
Public Inheritance

Derived class's constructors

Manager::Manager(){
	
}
Employee's constructor invocation -> default constructor can be invoked implicity
Public Inheritance

Derived class's constructor

Manager::Manager(){
}

Employee's constructor invacation -> default constructor can be invoked implicitly

Manager::Manager(string firstName, string lastName, double salary, string department):
	Employee(firstName, lastName, salary), department(department){
	
}

base class's constructor invocation - constructor initializer list arguments for the base class's constructor are specified in the definition of a derived class's constructor

Public Inheritance

How to derived class's objects constructed?

Bottom up order:

Base class constructor invocation

Member initialization

Devived class's constructor block

Destruction

in the opposite order

Public Inheritance

Method overriding

class Employee{
public:
	virtual void print(ostream& os) const;
};
			
class Manager : public Employee{
public:
	virtual void print(ostream& os) const;	
};
			
Public Inheritance

Method overriding

class Employee{
public:
	virtual void print(ostream& os) const;
};
void Employee:print(ostream &os) const{
	os << firstName << " " << lastName << " " << salary << endl;
}
class Manager: public Employee{
public:
	virtual void print(ostream& os)const;
};
void Manager::print(ostream& os)const{
	Employee::print(os);
	os << " " << department;
}
			
Public Inheritance

Method overriding - virtual functions

non virtual functions are bound statically

compile time

virtual functions are bound dynamically

run time

Public Inheritance

Polymorphism

A type with virtual functions is called a polymorphic type

Polymorphic behavior preconditions

The member function must be virtual

Objects must be manipulated through

Pointers

References

Employee :: print(os) //static binding - no polymorphism
			
Public Inheritance

Polymorphism - Virtual function table

class Employee{
public:
	virtual void print(ostream& os) const;	
};
class Manager: public Employee{
	virtual void print(ostream& os) const;
};
Employee e1, e2;
Manger m1, m2;
			
Discussion!!!
Employee* pe;
pe = &e1; pe->print();
pe = &e2; pe->print();
			

Each class with virtual function has its own virtual function table

Public Inheritance

Abstract class

Used for representing abstract concepts

Used as base class for other classes

No intances can be created

Public Inheritance

Abstract classes - pure virtual functions

class Shape{
public:
	virtual void rotate(int) = 0;	//pure virtual function
	virtual void draw() = 0;	//pure virtual function
};
			
Shape s;//???
			
Public Inheritance

Abstract class → concrete class

class Point{
};
class Circle: public Shape{
public:
	void rotate(int); 	//override shape:: rotate
	void draw(); 		//override shape: draw
	Circle(Point p, int r);
private:
	Point center;
	int radius;	
};
			
Public Inheritance

Abstract class → abtract class

class Polygon : public Shape{
public:	
	//draw() and rotate() are not overridden
};
			
Polygon p; //Compiler error
			
Public Inheritance

Virtual destructor

Every class having at least one virtual function should have virtual destructor. Why?

class X{
public:
	// ...
	virtual ~X();
};
			
Public Inheritance

Virtual destructor

void deleteAll(Employee ** emps, int size){
	for( int i = 0; i < size; i++){
		delete emps[i];//Which destructors is invoked?
	}
}
//main
Employee **t = new Employee*[10];
for(int i = 0; i < 10; i++){
	if(i % 2 == 0){
		t[i] = new Employee();
	}eles{
		t[i] = new Manager(0);
	}
}
deleteAll(t, 10);