Labreport 4
Labreport 4
Telecommunication Engineering
Experiment No. : 04
Date of Exp. : 30.10.2024
Date of Submission : 13.11.24
Remarks : Name : ALI HOSEN
ID : 2108039
Level : 02
Term : 02
Group : 02
Experiment Name:
Inheritance in C++.
Objectives
• Familiarize with Inheritance.
1
Example 1:
A C++ program to demonstrate single-level inheritance.
4 class Shape {
5 protected :
6 int width ;
7 int height ;
8 public :
9 void setWidth ( int w ) {
10 width = w ;
11 }
12 void setHeight ( int h ) {
13 height = h ;
14 }
15 };
16
24 int main () {
25 Rectangle rect ;
26 rect . setWidth (10) ;
27 rect . setHeight (12) ;
28 cout << " Total area : " << rect . getArea () << endl ;
29 return 0;
30 }
2
Output Screenshot
Example 2:
A C++ program to demonstrate multilevel inheritance.
4 class Shape {
5 protected :
6 int width ;
7 int height ;
8 public :
9 void setWidth ( int w ) {
10 width = w ;
11 }
12 void setHeight ( int h ) {
13 height = h ;
14 }
15 };
16
3
25 rect . setWidth (10) ;
26 rect . setHeight (12) ;
27
28 cout << " Total area : " << rect . getArea () << endl ;
29 return 0;
30 }
Output Screenshot
Example 3:
A program to demonstrate the use of a derived class to calculate the sum of two numbers.
4 class Base {
5 protected :
6 int num1 , num2 ;
7
8 public :
9 void inputNumbers () {
10 cout << " Enter the first number : " ;
11 cin >> num1 ;
12 cout << " Enter the second number : " ;
13 cin >> num2 ;
14 }
15 };
16
4
17 class Derived : public Base {
18 public :
19 void displaySum () {
20 int sum = num1 + num2 ;
21 cout << " The sum of " << num1 << " and " << num2 << " is :
" << sum << endl ;
22 }
23 };
24
25 int main () {
26 Derived obj ;
27 obj . inputNumbers () ;
28 obj . displaySum () ;
29 return 0;
30 }
Output Screenshot
5
Example 4:
Write a C++ program to calculate the percentage of a student. Accept the marks of
five subjects (Physics, Chemistry, Math, Biology, and English) in base class. A class will
derived from the base class which includes a function to find the total marks obtained
and another class derived from this first derived class which calculates and displays the
percentage of student.
4 class Base {
5 protected :
6 int marks [5];
7
8 public :
9 void inputMarks () {
10 cout << " Enter marks for Physics , Chemistry , Math ,
Biology , and English : " ;
11 for ( int i = 0; i < 5; i ++) {
12 cin >> marks [ i ];
13 }
14 }
15 };
16
21 public :
22 void calculateTotal () {
23 totalMarks = 0;
24 for ( int i = 0; i < 5; i ++) {
25 totalMarks += marks [ i ];
26 }
6
27 }
28 };
29
40 int main () {
41 Derived2 student ;
42 student . inputMarks () ;
43 student . di splayP ercen tage () ;
44 return 0;
45 }
Output Screenshot
7
Discussion
• Through these exercises, we explored inheritance in C++, specifically single-level
and multilevel inheritance.
• The code examples provided practical insights into how base and derived classes
function, particularly in arithmetic operations and inheritance.
• Issues encountered included the need to initialize member variables correctly and
handle inheritance syntax.