0% found this document useful (0 votes)
46 views

C++ Inheritance - 3

This document discusses C++ single inheritance. In single inheritance, a derived class inherits from one base class in a one-to-one relationship. The derived class inherits public and protected members of the base class depending on the access specifier used. The document provides a diagram illustrating single inheritance and a C++ code example demonstrating a derived class publicly inheriting from a base class and accessing members of both.

Uploaded by

md shankar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

C++ Inheritance - 3

This document discusses C++ single inheritance. In single inheritance, a derived class inherits from one base class in a one-to-one relationship. The derived class inherits public and protected members of the base class depending on the access specifier used. The document provides a diagram illustrating single inheritance and a C++ code example demonstrating a derived class publicly inheriting from a base class and accessing members of both.

Uploaded by

md shankar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10/13/2020 C++ Single Inheritance (With Examples) - Trytoprogram

C++ Tutorials

C++ Single Inheritance


Ads by
Stop seeing this ad Why this ad?

If a single class is derived from one base class then it is called single inheritance. In C++ single inheritance
base and derived class exhibit one to one relation.

C++ Single Inheritance Block Diagram

As shown in the gure, in C++ single inheritance only one class can be derived from the base class. Based on
the visibility mode used or access speci er used while deriving, the properties of the base class are derived.
Access speci er can be private, protected or public.

www.trytoprogram.com/cplusplus-programming/single-inheritance/ 1/5
10/13/2020 C++ Single Inheritance (With Examples) - Trytoprogram

Click here to learn in detail about access speci ers and their use in inheritance

C++ Programming Single Inheritance Syntax

class A // base class


{
..........
};
class B : acess_specifier A // derived class
{
...........
} ;

C++ Single Inheritance Example

www.trytoprogram.com/cplusplus-programming/single-inheritance/ 2/5
10/13/2020 C++ Single Inheritance (With Examples) - Trytoprogram

// inheritance.cpp
#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter the value of x = "; cin >> x;
}
};
class derive : public base //single derived class
{
private:
int y;
public:
void readdata()
{
cout << "Enter the value of y = "; cin >> y;
}
void product()
{
cout << "Product = " << x * y;
}
};

int main()
{
derive a; //object of derived class
a.getdata();
a.readdata();
a.product();
return 0;
} //end of program

www.trytoprogram.com/cplusplus-programming/single-inheritance/ 3/5
10/13/2020 C++ Single Inheritance (With Examples) - Trytoprogram

Easy & Intuitive UML Diagrams


Ad Easy to use. Collaborative. Hundreds of
templates. No download needed. Free 7-day
Ad trial.
Lucidchart

Sign Up

Output

Enter the value of x = 3


Enter the value of y = 4
Product = 12

Explanation

In this program class derive is publicly derived from the base class base . So the class derive inherits all
the protected and public members of base class base i.e the protected and the public members of base
class are accessible from class derive .

However private members can’t be accessed, although, we haven’t used any private data members in the
base class.

With the object of the derived class, we can call the functions of both derived and base class.

Realme C2 (diamond Blue, 16 Gb, 4000...


₹6,499 ₹6,999
Touchscreen Is Yes. In The Box Is Handset,
Adapter 5v/1a, Micro-usb Cable,...
Flipkart

www.trytoprogram.com/cplusplus-programming/single-inheritance/ 4/5
10/13/2020 C++ Single Inheritance (With Examples) - Trytoprogram

All rights reserved @trytoprogram.com

www.trytoprogram.com/cplusplus-programming/single-inheritance/ 5/5

You might also like