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

OOP 5 - Inheritance (cpp)

The document discusses inheritance in object-oriented programming using C++, highlighting its purpose of code reusability, extensibility, and maintainability. It defines base and derived classes, explaining how derived classes inherit properties and behaviors from base classes. Additionally, it includes practical questions for implementing inheritance through examples involving classes like Animal, Book, and Student.

Uploaded by

islamabad.rally
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

OOP 5 - Inheritance (cpp)

The document discusses inheritance in object-oriented programming using C++, highlighting its purpose of code reusability, extensibility, and maintainability. It defines base and derived classes, explaining how derived classes inherit properties and behaviors from base classes. Additionally, it includes practical questions for implementing inheritance through examples involving classes like Animal, Book, and Student.

Uploaded by

islamabad.rally
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11/18/2024 OOP Lab 05

INHERITANCE

Syed Muhammad Zafar Rizvi


HAMDARD UNIVERSITY
Inheritance
Inheritance is a feature of object-oriented programming in C++ that allows a class (called
the derived class) to inherit properties and behaviours (i.e., data members and member
functions) from another class (called the base class). It enables code reusability and
hierarchical relationships between classes.

 Purpose:
o Reusability: Allows derived classes to reuse code from the base class, avoiding
redundancy.
o Extensibility: Derived classes can extend or modify the functionality of the
base class.
o Maintainability: Changes made to the base class can propagate to derived
classes, making the system easier to maintain.

Base & Derived Classes


Base Class: The class from which other classes inherit. It contains common attributes and
methods that can be shared by derived classes.

Derived Class: A class that inherits from another class (the base class). It can access the
base class's public and protected members, and can also add new members or override base
class methods.

Examples done in Lab Lecture

SIMPLE INHERITANCE
CONSTRUCTOR INHERITANCE

NOTE: The constructor of the Base class is called first because Derived inherits from Base.
These prints:
"Base class constructor"
"Derived class constructor"

Prac ce Ques ons


Perform these tasks and attach Screenshots of Code and Output in your Document.

Q1: Create a base class called Animal with a member variables name (string) and age (int).
Create a derived class called Dog that inherits from Animal with a member variables color
(string) and getters (Get value Functions) for all these three variables. In the main function,
create an object of Dog and call these functions.

Q2: Create a base class called Book with member variables title (string), author (string), and
isbn (string). Create two derived classes, Fiction and NonFiction, which add additional
member variables (genre for Fiction and subject for NonFiction). Implement getter functions
in all classes to retrieve the values of the variables. In the main function, create objects of
Fiction and NonFiction, and print their details.

Q3: Create a base class Student with member variables name (string) and age (int). Create a
derived class GraduatedStudent that inherits from Student and adds a member variable
degree (string). Implement setter functions to set the values of all the variables. In the main
function, create an object of GraduatedStudent, set its values using setter functions, and print
its details.

You might also like