Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Inheritance.

Q1: Write a c++ program that contain base class of shape, this class should include:

·   Width and height attributes (protected)


·   Set function for height and width.

 Then create  sub class and call it rectangle that should contain a function for area,
which area= width*height.

Another sub class call triangle that also have an area function, area = (width*height)/2.

Inside the main create new object from triangle, rectangle classes, then call set
functions and print the area of them.

**************************************************************************************************

Q2: Write c++ program that contain Animal class, the class should have:

Empty constructor that print this message: (Empty constructor: Animal).

 Parameterized constructor that contains parameter a as integer and print


(Parameterized constructor: Animal).

Create sub class and name it Bird, this class inherits all the attribute from the base
class.

Bird class should contain:

empty constructor that will print: (Empty constructor: Bird).


 Parameterized constructor with parameter a as integer (parameterized constructor:
Bird).

 Inside the main create an object from Bird and call the constructors.

*************************************************************************************************

Q3: Create a C++ program with two classes:


 Shape
 Rectangle
Shape is the base class which includes these following attributes:
 Area (double)
 Perimeter (double)
With the following member functions:
 Empty constructor which initialize the area, perimeter to zero.
Rectangle class is a subclass from shape class which contain the following attributes:
 Length (double)
 Width (double)
 Parametrized constructor that takes 2 parameters (Length, Width)
 ComputeArea() function that compute the area of rectangle. Area = length *width.
 CopmputePerimeter () function that compute the perimeter of rectangle.

Perimeter = 2*(length+width).

Inside the main


 Create an object from rectangle class.
 Call the parametrized constructor.
 Call the ComputeArea() and ComputePerimeter functions to print the area and
perimeter of rectangle.

*************************************************************************************************

Q4: write a C++ program to read and print students information using two classes and
simple inheritance. The program contains:

Base class of students that store student information as protected.


Student Name (string).
Student ID (int).
Gender(string). 
get_info() function to ask the student to enter his info,.
print_info() that prints student information.
Create a derived class and name it result, this class contains in protected:
total of 3 marks(int)
percentage(float)
grade(string)
where percentage =(( total marks*100)/500)
GetResult_info() function that asks to enter marks and grade then calculates the
percentage.
PrintResult_info() that prints the results.
Inside the main
create object from derived class
read student and result info by calling get_info()  and GetResult_info()
print student information by calling print_info() and PrintResult_info()

HW:

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
(in private scope). 
From this class derive two classes:

 book, which adds a page count (type int)


 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 its data. 
Write a main () program to test the book and tape classes by creating instances of them

You might also like