Single inheritance in C++ with Syntax and Examples
Single inheritance in C++ OOP, Syntax of single inheritance, Example of single inheritance
What is single inheritance?
In single inheritance child class is derived from only and only one parent class.
Syntax to inherit the class from the parent class in single inheritance?
Serial# | Code |
1 | class child_class_name : Access_specifier Parent_class_name |
2 | { |
3 | Body of the class; |
4 | }; |
Example of single inheritance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include<iostream> using namespace std; class sum { protected: int n1; int n2; }; class child : public sum { public: int sum() { cout<<"enter n1?"<<endl; cin>>n1; cout<<"enter n2?"<<endl; cin>>n2
; cout<<"sum="<<n1+n2<<endl; } }; int main() { child myobject; myobject.sum(); } |
Output
enter n1?
3
enter n1?
2
sum=5
Difference between Single and multiple Inheritance in C++
Here, i am showing you a comparison of Single and multiple Inheritance in C++.
SINGLE INHERITANCE | MULTIPLE INHERITANCE |
In Single inheritance derived class have only single base class. | In Multiple inheritance derived class have more than one base class. |
Less overhead in single inheritance and requires small run time as compared to multiple inheritance. | More overhead in Multiple inheritance and requires more run time as compared to single inheritance. |
Single inheritance is a lot of close to specialization. | Multiple inheritance is a lot of close to generalization. |
Syntax of single inheritance is
Class DerivedClass_name : access_specifier Base_Class{};. |
Syntax of multiple inheritance is
Class DerivedClass_name : access_specifier Base_Class1, access_specifier Base_Class2, ….{}; . |
Single inheritance is simple as compared to the multiple inheritance. | Multiple inheritance is more complex as compared to the single inheritance. |
Mostly all of the programming languages supports Single inheritance. | Multiple inheritance is supported by C++ but Multiple inheritance can’t be implemented in most of other programming languages e.g, C# and Java doesn’t support multiple inheritance through classes. |
Exercise and Solution Single inheritance
- WAP in C++ to display such a pattern for n number of rows using a number which will start with the number 1 and the first and the last number of each row will be 1 with the help of multiple inheritances.
- C++ program to print a hollow square or rectangle star pattern with the support of single inheritance..
- Single inheritance C++ to display the n terms of odd natural number and their sum.
- Single inheritance C++ program to convert a decimal number into binary.
- Single inheritance C++ program to display the pattern like a pyramid using the alphabet using single inheritance.
- Single inheritance C++ program to find the perfect numbers within a given range.
- Single inheritance C++ program to show the sum of an A.P. series.
- Single inheritance C++ program to print the string in reverse order.
- Single inheritance C+ Program Armstrong number of n digits.
- single inheritance C++ program to display patterns like the right angle.
- Single inheritance C++ program pattern like a pyramid repeat.
- Single inheritance C++ program to display the cube of the number up to a given integer.
- Single inheritance C++ Program to convert a decimal number into binary.
- Single Inheritance C++ program to find Strong Numbers within a range of numbers.
- C++ program to find HCF using single inheritance.
- Write a C++ program to print the rhombus star pattern of N rows using Single Inheritance.
- Write a program in C++ to convert a decimal number to hexadecimal using the single inheritance in object-oriented programming (OOP).
- Write a program in C++ to convert a decimal number into an octal without using an array using Single inheritance in object-oriented programming(OOP).
- C++ program to display Pascal’s triangle using the single inheritance.
- Single inheritance C++ program to print hollow rhombus, parallelogram star pattern.
- Single Inheritance C++ program to print mirrored right triangle star pattern.
- Single inheritance C++ Program to convert an octal number into binary.
- Single inheritance Floyd Triangle C++.
Test Your Understandings |
1.In single inheritance multiple child can have multiple parent classes? YES / NO
2.class child_class_name : Access_specifier parent_class_name is the correct syntax for creating child class? YES / NO
Topic Covered
Single inheritance in C++ OOP, Syntax of single inheritance, Example of singe inheritance .