Lab 4 Tasks
Lab 4 Tasks
LAB 4 TASKS
SINGLE INHERITANCE USING CONSTRUCTOR OVERLOADING,
OVERRIDING AND ENCAPSULATION.
Inheritance.
What is the inheritance of class?
We can create child classes for a class. Child classes are called derived
classes and parent classes are called base classes.
Why do we need to inherit the classes?
Lets an example of daily life.
Suppose John is a person with following properties;
John has resource. e.g, home, car, bank balance, and a big
property.
Now John need his child for the following two reasons;
To support his daily work
To get the resources before and after his death.
So, this will leads the John to think that he should have a baby.
Similarly, in OOP, we need to create a child of the parent classes so that
when we declare any variable or function in the parent class, it can be
shared easily with his child class without wasting time and computer
memory.
Q1:- Write a C++ program to Access the Protected Data Members using
Inheritance.
Q2:- Create a base class Shape with the following properties and
methods:Properties: name (string), color (string)Methods:
Circle should also inherit from Shape. It should have an additional property radius
(float). Implement a constructor to initialize this property and override the
display() method to display the name, color, and radius of the circle.
Methods:
void display(): Method to display the details of the vehicle (make, model, year).
Create a derived class Car inheriting from Vehicle with an additional property:
Method:
Car(string make, string model, int year, int numDoors): Constructor to initialize the
properties, including those inherited from Vehicle.
Methods:
Employee(string name, int id, double salary): Constructor to initialize name, id,
and salary.
virtual void display(): Display the name, id, and salary of the employee.
Method:
Manager(string name, int id, double salary, string department, double bonus):
Constructor to initialize the properties, including those inherited from Employee.
Override the display() method to display the name, id, salary, department, and
bonus of the manager.
Q5:- Define a base class Person with the following properties and methods:
Properties: name (string), age (integer)
Superior University
Methods:
Person(string name, int age): Constructor to initialize name and age.void display():
Display the name and age of the person.
Create a derived class Student inheriting from Person with additional properties:
Method:
Student(string name, int age, int rollNumber, char grade): Constructor to initialize
the properties, including those inherited from Person.
Override the display() method to display the name, age, roll number, and grade of
the student.
Q6:- Define a base class Animal with the following properties and methods:
Properties: name (string)
Methods:
Create a derived class Dog inheriting from Animal with an additional property:
Method:
Superior University
Dog(string name, string breed): Constructor to initialize the properties, including
those inherited from Animal.
Q7:- Define a base class Account with the following properties and methods:
Properties: accountNumber (integer), balance (double)
Methods:
Method:
Override the display() method to display the account details along with the interest
rate.
Call the display() method to display the details of the savings account.
Q8:- Create a class Employee with the following attributes and member
functions:
Employee registration number
Employee name
Destination
Human resource Allowance
Basic salary
Profitable fund
Get function to take input of Registeration number, name and destination.
Inherit(public) a class Salary from employee . Salary should contain the following
member functions:
Get function to take input of human resource allowance, basic salary and
profitable fund.
Sample Sample
Input Output
Name : ali
Employee Output=60
number: 2
Destination:
Associate
Pay=20,20,20
Q9:- Imagine a publishing company that markets both book and audiocassette versions of its
works. Create a class publication that stores the title (a string) and price (type float) of a
publication. From this class derive two classes: book, which adds a page count (type int), and
tape, which adds a playing time in minutes (type float). Each of these three classes should have a
getdata() function to get its data from the user at the keyboard, and a putdata() function to display
Superior University
its data. Write a main() program to test the book and tape classes by creating instances of them,
asking the user to fill in data with getdata(), and then displaying the data with putdata().