0% found this document useful (0 votes)
11 views4 pages

Inheritance

The document explains single and multilevel inheritance in C++ with examples. It demonstrates single inheritance through a program that reads and prints student information using two classes, and multilevel inheritance by showcasing a hierarchy of classes where one class inherits from another. Each example includes class definitions, member functions, and main function implementations to illustrate the concepts.

Uploaded by

arkosinha15
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)
11 views4 pages

Inheritance

The document explains single and multilevel inheritance in C++ with examples. It demonstrates single inheritance through a program that reads and prints student information using two classes, and multilevel inheritance by showcasing a hierarchy of classes where one class inherits from another. Each example includes class definitions, member functions, and main function implementations to illustrate the concepts.

Uploaded by

arkosinha15
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

Single/Simple Inheritance in C++

In C++, the single/simple inheritance is defined as the inheritance in which a derived class is
inherited from the only one base class.
// C++ program to demonstrate example of
// simple inheritance

#include <iostream>
using namespace std;

// Base Class
class A {
public:
void Afun(void);
};

// Function definiion
void A::Afun(void)
{
cout << "I'm the body of Afun()..." << endl;
}

// Derived Class
class B : public A {
public:
void Bfun(void);
};
// Function definition
void B::Bfun(void)
{
cout << "I'm the body of Bfun()..." << endl;
}

int main()
{
// Create object of derived class - class B
B objB;

// Now, we can access the function of class A (Base class)


objB.Afun();
objB.Bfun();

return 0;
}

Reading/Printing students information using two classes and


simple inheritance
This program will demonstrate example of read and print students using simple inheritance in
c++ programming language.
// C++ program to read and print students information
// using two classes and simple inheritance

#include <iostream>
using namespace std;

// Base class
class std_basic_info {
private:
char name[30];
int age;
char gender;

public:
void getBasicInfo(void);
void putBasicInfo(void);
};

// function definitions
void std_basic_info::getBasicInfo(void)
{
cout << "Enter student's basic information:" << endl;
cout << "Name?: ";
cin >> name;
cout << "Age?: ";
cin >> age;
cout << "Gender?: ";
cin >> gender;
}

void std_basic_info::putBasicInfo(void)
{
cout << "Name: " << name << ",Age: " << age << ",Gender: " << gender <<
endl;
}

// Derived class
class std_result_info : public std_basic_info {
private:
int totalM;
float perc;
char grade;

public:
void getResultInfo(void);
void putResultInfo(void);
};

// Function definitions
void std_result_info::getResultInfo(void)
{
cout << "Enter student's result information:" << endl;
cout << "Total Marks?: ";
cin >> totalM;
perc = (float)((totalM * 100) / 500);
cout << "Grade?: ";
cin >> grade;
}

void std_result_info::putResultInfo(void)
{
cout << "Total Marks: " << totalM << ",Percentage: " << perc << ",Grade: "
<< grade << endl;
}

int main()
{
// Create object of derived class
std_result_info std;

// Read student basic and result information


std.getBasicInfo();
std.getResultInfo();

//print student basic and result information


std.putBasicInfo();
std.putResultInfo();

return 0;
}

Multilevel Inheritance in C++


In C++, the multilevel inheritance is defined as the inheritance in which one class inherits another
class which is further inherited by another class.
// C++ program to demonstrate example of
// multilevel inheritance

#include <iostream>
using namespace std;

// Base Class : class A


class A {
private:
int a;

public:
void get_a(int val_a)
{
a = val_a;
}

void disp_a(void)
{
cout << "Value of a: " << a << endl;
}
};

// Here Class B is base class for class C


// and Derived class for class A
class B : public A {
private:
int b;

public:
// assign value of a from here
void get_b(int val_a, int val_b)
{
// assign value of a by calling function of class A
get_a(val_a);
b = val_b;
}

void disp_b(void)
{
// display value of a
disp_a();
cout << "Value of b: " << b << endl;
}
};

// Here, class C is derived class and B is Base class


class C : public B {
private:
int c;

public:
// assign value of a from here
void get_c(int val_a, int val_b, int val_c)
{
// Multilevel Inheritance
// assign value of a, bby calling function of class B and Class A
// here Class A is inherited on Class B, and Class B in inherited on
Class B
get_b(val_a, val_b);
c = val_c;
}

void disp_c(void)
{
// display value of a and b using disp_b()
disp_b();
cout << "Value of c: " << c << endl;
}
};

int main()
{
// create object of final class, which is Class C
C objC;

objC.get_c(10, 20, 30);


objC.disp_c();

return 0;
}

You might also like