CS-2012
Object Oriented Programming
FALL-2023
Lecture 11
Data vs Class Access Specifier
● Two levels of access class Point{
control over class protected: int x, y;
members public: void set(int a, int b);
1. class definition };
2. inheritance type
class Circle : public Point{
……
};
2
Class access specifier
● Public
● Private
● protected
3
Public Inheritance
● With public inheritance,
○ public and protected members of the base class become respectively public
and protected members of the derived class
class derived : public base{
……
};
4
Protected Inheritance
● Public and protected members of the base class become protected
members of the derived class.
class derived : protected base{
……
};
5
Private Inheritance
● With private inheritance, public and protected members of the base
class become private members of the derived class.
class derived : private base{
……
};
6
public, protected and private Inheritance
7
Inheritance vs. Access
8
Class Access Specifiers – When to use?
1) public – object of derived class can be treated as object of base
class (not vice-versa)
2) protected – more restrictive than public, but allows derived
classes to know details of parents
3) private – prevents objects of derived class from being treated
as objects of base class.
9
Method Overriding
● A derived class can override methods defined in its parent class.
○ the method in the subclass must have the identical signature to the method
in the base class.
○ a subclass implements its own version of a base class method.
10
Method Overriding
11
Types of Inheritance
● Single inheritance
○ Inherits from one base class
● Multi-level inheritance
○ Chain of inheritance
● Multiple inheritance
○ Inherits from multiple classes
12
Types of Inheritance: University Example
13
Multiple Inheritance
14
What is Multiple Inheritance?
● If class A inherits from
more than one class,
○ i.e., A (B1, B2, ...,
Bn), we speak of
multiple inheritance.
15
Multiple Inheritance
● This may introduce naming conflicts:
○ if at least two of its base classes define properties (data members or member
functions) with the same name
16
Ambiguity in Multiple Inheritance
class Student {
int id;
int age;
public:
int GetAge() const { return age; }
int GetId() const { return id; }
void SetAge( int n ) { age = n; }
void SetId( int n ) { id=n; }
};
17
Ambiguity in Multiple Inheritance
class Employee {
public:
int GetAge() const { return age; }
int SetAge( int n ) { age = n; }
void SetId( int n) { id=n; }
int GetId(void) const { return id; }
private:
int age;
int id;
};
18
Ambiguity in Multiple Inheritance
class Salaried : public Employee {
float salary;
public:
float GetSalary() const { return salary; }
void SetSalary( float s ) { salary=s; }
};
class GradAssistant :public Student, public Salaried {
public:
void Display() const
{
cout<<GetId()<<","<<GetSalary()<<","<<GetAge(); //ambiguity
}
19
Ambiguity in Multiple Inheritance
int main(void) {
GradAssistant ga;
ga.SetAge(20); //ambiguity
ga.SetId(15); //ambiguity
ga.Display(); //ambiguity inside display()
}
//program will not compile and will generate errors
20
What is the solution?
● Call functions explicitly by specifying name of class and using scope
resolution operator to remove ambiguity:
1. Direct solution:
Student::SetAge() or Salaried::SetAge()
21