Inheritance Programs (1) .PPTX - Read-Only
Inheritance Programs (1) .PPTX - Read-Only
Q4) A class Student defines the personal data of a student while another
class Marks defines the roll number, name of subject and marks obtained by
the student. The details of the classes are as follows:
Class Name: Student
Data Members/Instance variables
• Name, gender - String variable to store the name and gender of
the student
• Age – integer variable to store age of the student
Member Function/Methods
• Student() – constructor to assign initial values to the data members
• void inputDetails() – to accept values for the data members
• void show() – to print personal data of the students
Class Name: Marks
Data Members/Instance variables
• Rollno, mark – integer to store roll number and marks of the student
• Subject – String variable to store the name of the subject
Member Function/Methods
• void readDetails() – to accept values for data members
• int point() – to return the point obtained according to the following
Mark Point
>=90 1
70-89 2
50-69 3
40-49 4
<40 5
• void show() – to display name, gender, age, roll, mark, subject and point of the student by invoking
suitable function.
Specify the class Student giving details of all the functions. Using the concept of inheritance define
Q5)A super class Worker has been defined to store the details of a
worker. Define a subclass Wages to compute the monthly wages for the
worker. The details of both the classes are given below:
Class name: Worker
Data Members
Name: To store the name of the worker
Basic : to store the basic pay in decimal
Methods
WORKER(…) : Parameterised constructor to assign values to the
instance variables
void display() – displays the worker details
Class name: Wages
Data Members
Hrs : to store the overtime hours worked
Rate : to store the rate per hour
Wage : to store the overall wage of the worker
Member Functions
Wages(…) – Parameterise constructor to assign values to the instance
variables of both the classes
double overtime() – calculates and returns the overtime amount as (hours *
rate)
void display() – Calculates the wage and displays it along with the other
details. Formula to calculate the wage is given as,
Wage = overtime amount + Basic pay
Q 6) A super class Perimeter has been defined to calculate the perimeter of a
parallelogram. Define a subclass Area to compute the area of the parallelogram by using the
required data members of the superclass. The details are given below:
Class name: Perimeter
Data Members/Instance Variables:
a - to store the length in decimal
b – to store the breadth in decimal
Member Functions/Methods
1) Perimeter(…) – parameterized constructor to assign values to data members
2) double calculate() – calculate and return the perimeter of a parallelogram as 2*(len+brea)
3) void show() – to display the data members along with the perimeter of the parallelogram
Class name: Area
Data Members/Instance Variables:
h - to store the length in decimal
area – to store the breadth in decimal
Member Functions/Methods
1) Area(…) - parameterized constructor to assign values to data
members of both the classes
2) void doarea() – compute the area as (breadth * height)
3) void show() – display the data members of both classes along with
the area and perimeter of the parallelogram
Q 7)A super class Record has been defined to store the names and
marks of 50 students. Define a subclass Rank to find the highest rank
along with the name. The details of both the classes ae given below:
Class Name: Record
Data Members/Instance Variables:
name[] : to store the names of the students
mk[] : to store the rank of the students
Member functions/Methods
Record() –constructor to initialize the data members
void readValues() – to store the name and ranks
void display() – to display the names and the corresponding ranks
Class Name: Rank
Data Members/Instance Variables:
index: integer to store the index of the topmost rank
Member functions/Methods
Rank() –constructor to invoke the base class constructor and to
initialize index=0
void highest() – finds the index/location of the topmost rank and
storesit in index without sorting the array
void display() – to display the names and ranks along with the name
having the topmost rank
LAB WORK
• Q) A superclass Detail has been defined to store the details of a customer.
Define a subclass Bill to compute the monthly charge of the customer as
per the chart given below:
• Number of calls Rate
• 1-100 Only rental charge
• 101-200 80ps per call+ rental charge
• 201-300 1 Re per call+ rental charge
• Above 300 2 Rs per call+ rental charge
• Details of both the classes are given below.
Class Name: Detail
Data Members/Instance Variables:
Name : to store the name of the customer
Address : to store the address of the customer
Telno : to store the phone number of the customer
Rent : to store the monthly rental charge
Member functions/Methods
Detail(…) – parameterized constructor to assign values to data members
void show() – to display the details of the customer
Class Name: Bill
Data Members/Instance Variables:
n : to store the number of calls
Amt : to store the amount to be paid by the customer
Member functions/Methods
1)Bill(…) – parameterized constructor to assign values to data members
of both the classes and to initialize amt=0.0
2)void cal() – calculates the monthly telephone charge as per the chart
given above.
3)void show() – to display the details of the customer and the amount
to be paid