0% found this document useful (0 votes)
6 views9 pages

Labreport 4

OOP labreport

Uploaded by

jobayer mahmud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Labreport 4

OOP labreport

Uploaded by

jobayer mahmud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Department of Electronics &

Telecommunication Engineering

Name of the Experiment:

Course No. : CSE 284


Course Title : Object Oriented Programming

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.

• Explain the concept of single and Multi level inheritance in OOP.

• Solve various problems in order to comprehend the above topics.

1
Example 1:
A C++ program to demonstrate single-level inheritance.

1 # include < iostream >


2 using namespace std ;
3

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

17 class Rectangle : public Shape {


18 public :
19 int getArea () {
20 return ( width * height ) ;
21 }
22 };
23

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.

1 # include < iostream >


2 using namespace std ;
3

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

17 class Rectangle : public Shape {


18 public :
19 int getArea () {
20 return ( width * height ) ;
21 }
22 };
23 int main ( void ) {
24 Rectangle rect ;

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.

1 # include < iostream >


2 using namespace std ;
3

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.

1 # include < iostream >


2 using namespace std ;
3

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

17 class Derived1 : public Base {


18 protected :
19 int totalMarks ;
20

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

30 class Derived2 : public Derived1 {


31 public :
32 void di splayP ercent age () {
33 calculateTotal () ;
34 float percentage = ( static_cast < float >( totalMarks ) / 500)
* 100;
35 cout << " Total Marks : " << totalMarks << " /500 " << endl ;
36 cout << " Percentage : " << percentage << " % " << endl ;
37 }
38 };
39

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.

You might also like