Object Oriented Programming ยท Unit 5 ยท 7 hrs
Inheritance
Exam-focused notes for Inheritance (Object Oriented Programming, CSC166): what the TU syllabus asks and how it has actually been tested, with 9 solved past questions from this unit.
Inheritance in short
This unit covers how one class derives from another to reuse and extend its features: the base and derived class relationship, how access specifiers control what a derived class inherits, the five types of inheritance and how public versus private derivation changes visibility, how constructors and destructors run in an inheritance chain, and aggregation, the alternative relationship where one class simply contains objects of another.
What this unit covers
- Introduction to inheritance
- Derived Class and Base Class
- Access Specifiers (private, protected, and public)
- Types of inheritance
- Public and Private Inheritance
- Constructor and Destructor in derived classes
- Aggregation
Types of inheritance
2080 ยท 10 marks
State the use of new operator. An educational institute wishes to maintain a data of its employee. The hierarchical relationships of related classes are as follows. Define all the classes to represent above hierarchy and define functions to retrieve individual information as and when required.[10]
Show solved answerHide answer
New Operator and Employee Hierarchy in C++
Use of new Operator (2 marks)
The new operator in C++ is used for dynamic memory allocation. It allocates memory at runtime from the heap (free store) and returns a pointer to the beginning of the allocated memory block.
Key uses:
- Allocates memory for a variable or object during program execution
- Returns a pointer to the allocated memory
- Memory persists until explicitly released using
delete - Syntax:
pointer_variable = new data_type;
Example:
int *ptr = new int; // allocates memory for one integer
int *arr = new int[10]; // allocates memory for array of 10 integers
delete ptr; // free single variable
delete[] arr; // free array
Employee Hierarchy Program (8 marks)
Class Hierarchy Structure
Employee
(name, emp_id)
|
________________
| |
Teacher Officer
(subject, (department,
salary) salary)
|
Typist
(speed,
salary)
Complete C++ Program
#include<iostream>
#include<string>
using namespace std;
// โโโ Base Class โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class Employee {
protected:
string name;
int emp_id;
public:
// Function to set employee data
void getEmployeeData() {
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Employee ID: ";
cin >> emp_id;
}
// Function to display employee data
void showEmployeeData() {
cout << "\n--- Employee Information ---" << endl;
cout << "Name : " << name << endl;
cout << "Emp ID : " << emp_id << endl;
}
};
// โโโ Derived Class: Teacher โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class Teacher : public Employee {
protected:
string subject;
float salary;
public:
// Function to set teacher data
void getTeacherData() {
getEmployeeData(); // call base class function
cout << "Enter Subject Specialization: ";
cin >> subject;
cout << "Enter Salary: ";
cin >> salary;
}
// Function to display teacher data
void showTeacherData() {
showEmployeeData(); // call base class function
cout << "Subject : " << subject << endl;
cout << "Salary : " << salary << endl;
}
};
// โโโ Derived Class: Typist (derived from Teacher) โโโโโโโโโโโโโ
class Typist : public Teacher {
private:
int typingSpeed; // words per minute
public:
// Function to set typist data
void getTypistData() {
getTeacherData();
cout << "Enter Typing Speed (wpm): ";
cin >> typingSpeed;
}
// Function to display typist data
void showTypistData() {
showTeacherData();
cout << "Typing Speed: " << typingSpeed << " wpm" << endl;
}
};
// โโโ Derived Class: Officer โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class Officer : public Employee {
private:
string department;
float salary;
public:
// Function to set officer data
void getOfficerData() {
getEmployeeData(); // call base class function
cout << "Enter Department: ";
cin >> department;
cout << "Enter Salary: ";
cin >> salary;
}
// Function to display officer data
void showOfficerData() {
showEmployeeData(); // call base class function
cout << "Department: " << department << endl;
cout << "Salary : " << salary << endl;
}
};
// โโโ Main Function โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
int main() {
int choice;
cout << "===== Educational Institute Employee System =====" << endl;
cout << "1. Teacher\n2. Typist\n3. Officer" << endl;
cout << "Enter your choice: ";
cin >> choice;
// Using new operator for dynamic object creation
if (choice == 1) {
Teacher *t = new Teacher;
t->getTeacherData();
t->showTeacherData();
delete t;
}
else if (choice == 2) {
Typist *ty = new Typist;
ty->getTypistData();
ty->showTypistData();
delete ty;
}
else if (choice == 3) {
Officer *o = new Officer;
o->getOfficerData();
o->showOfficerData();
delete o;
}
else {
cout << "Invalid choice!" << endl;
}
return 0;
}
Sample Output
===== Educational Institute Employee System =====
1. Teacher
2. Typist
3. Officer
Enter your choice: 1
Enter Employee Name: Ram
Enter Employee ID: 101
Enter Subject Specialization: Mathematics
Enter Salary: 45000
--- Employee Information ---
Name : Ram
Emp ID : 101
Subject : Mathematics
Salary : 45000
Key Concepts Used
| Concept | Application |
|---|---|
| Base Class | Employee holds common data (name, emp_id) |
| Inheritance | Teacher, Officer inherit from Employee; Typist inherits from Teacher |
protected members | Allow derived classes to access base class data directly |
new operator | Used to create objects dynamically at runtime |
| Function overriding | Each derived class defines its own show and get functions |
public inheritance | Derived class inherits all public and protected members of base class |
**
2078 ยท 10 marks
How ambiguity arises in multipath inheritance? How can you remove this type of ambiguity? Explain with suitable example.[10]
2076 ยท 10 marks
Write a program according to the specification given below:- Create a class Teacher with data members id & subject and member functions for reading and displaying data members.- Create another class Staff with data members id & position, and member functions for reading and displaying data members.- Derive a class Coordinator from Teacher and Staff and the class must have its own data member department and member functions for reading and displaying data members.- Create two object of Coordinator class and read and display their details.[10]
2075 ยท 5 marks
Briefly explain types of inheritance used in object oriented programming. [5]
Constructor and Destructor in derived classes
2080 ยท 5 marks
Describe the chain of constructors and destructors in inheritance. [5]
Aggregation
2079 ยท 10 marks
What is aggregation? Write a program for implementing following: Create a class author with attributes name and qualification. Also create a class publication with name. Form these classes to derive a class book having attribute title and price. Each of three classes should have a getdata() method to get their data from user. The classes should have putdata() method to display the data. Create instances of the class book in main.[10]
Access Specifiers
2079 ยท 5 marks
What are the various class access specifies? How public inheritance differs from private inheritance? [5]
Show solved answerHide answer
Class Access Specifiers and Public vs Private Inheritance
Access Specifiers in C++
A class uses access specifiers to control the visibility of its data members and member functions. There are three access specifiers in C++:
1. Private
- Members declared as
privateare visible only to member functions of their own class. - They cannot be accessed from outside the class or by derived classes directly.
- If no access specifier is mentioned, members are private by default.
class Example {
int x; // private by default
private:
int y; // explicitly private
};
2. Protected
- Members declared as
protectedare visible to member functions of their own class and derived classes. - They cannot be accessed from outside these classes (data hiding is maintained).
- Useful when inheritance is involved.
class Example {
protected:
int x; // accessible in derived class
};
3. Public
- Members declared as
publicare visible to all functions in the program. - They can be accessed from anywhere using the dot operator (
.) with an object.
class Example {
public:
int x; // accessible from anywhere
};
Summary Table
| Access Specifier | Own Class | Derived Class | Outside Class |
|---|---|---|---|
| private | Yes | No | No |
| protected | Yes | Yes | No |
| public | Yes | Yes | Yes |
Public Inheritance vs Private Inheritance
When a derived class inherits from a base class, the mode of inheritance determines how the base class members are accessible in the derived class.
Public Inheritance
class Derived : public Base {
// ...
};
publicmembers of Base remain public in Derived.protectedmembers of Base remain protected in Derived.privatemembers of Base are not accessible in Derived.- Objects of the derived class can access the public members of the base class.
- This represents an "is-a" relationship.
Private Inheritance
class Derived : private Base {
// ...
};
publicmembers of Base become private in Derived.protectedmembers of Base become private in Derived.privatemembers of Base are not accessible in Derived.- Objects of the derived class cannot access any inherited members from outside.
- This represents a "has-a" (implementation) relationship.
Comparison Table
| Feature | Public Inheritance | Private Inheritance |
|---|---|---|
| Base public members | Remain public in Derived | Become private in Derived |
| Base protected members | Remain protected in Derived | Become private in Derived |
| Base private members | Not accessible | Not accessible |
| Access via object | Allowed | Not allowed |
| Relationship | "is-a" | "has-a" |
Example
class Base {
public:
int x;
protected:
int y;
private:
int z;
};
class PublicDerived : public Base {
// x is public, y is protected, z is not accessible
};
class PrivateDerived : private Base {
// x is private, y is private, z is not accessible
};
int main() {
PublicDerived pd;
pd.x = 10; // Allowed (x is public)
PrivateDerived pvd;
pvd.x = 10; // ERROR: x is private in PrivateDerived
}
Key Point: Public inheritance preserves the access levels of base class members, while private inheritance makes all inherited members private, restricting access from outside the derived class.
2080.1 ยท 5 marks
Explain the practical implication of protected specifier in inheritance. List advantages and disadvantages of inheritance. [5]
Public and Private Inheritance
2075 ยท 10 marks
Depict the difference between private and public derivation. Explain derived class constructor with suitable program.[10]
Make Unit 5 stick
Practice CSC166 with flashcards & quizzes