23 Nov 2019

  • November 23, 2019
  • Amitraj
1.) single inheritance

-> A derived class with only one base class, is known as single inheritance.

-> When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance.

For example,

-> Car is derived from vehicle

-> Typist is derived from staff

-> Animal is derived from living things

Syntax of Single Inheritance-


class base_classname
{
    properties;
    methods;
};

class derived_classname : visibility_mode base_classname
{
    properties;
    methods;
};



// cpp program on single inheritance.

#include<iostream>
using namespace std;
class A
{
int a;
public:
void show()
{
cout<<" parent show";
}
};
class B: public A
{
int b;
public:
void display()
{
cout<<"child display";
}
};
int main()
{
B b1;
b1.display();
b1.show();
return 0;
}

input/output:-
child display parent show



//cpp program to demonstrate single- inheritance.


#include<iostream>
using namespace std;
class student
{
private:
int rollno;
char name[20];
public:
void getdata()
{
cout<<"Enter roll no:";
cin>>rollno;
cout<<"Enter Name:";
cin>>name;
}
void showdata()
{
cout<<"\n roll no:"<<rollno;
cout<<"\n Name:"<<name;
}
};
class Details :public student
{
private:
int  m1,m2,m3,total;
public:
void  getmarks()
{
getdata();
cout<<"Enter marks m1,m2,m3";
cin>>m1>>m2>>m3;
}
void sum()
{
total=m1+m2+m3;
}
void display()
{
sum();
showdata();
cout<<"\n Total marks="<<total;
}
};
int main()
{
Details d;
d.getmarks();
d.display();
return 0;
}


input/output:-
Enter roll no:12
Enter Name:john
Enter marks m1,m2,m3 4 5 6

 roll no:12
 Name:john
 Total marks=15




Related Posts:

  • Catch All Exceptions in C++ Catch All Exceptions in C++ -> In some situations, we may not be able to anticipate types of exceptions and therefore may not be able to design independent catch handlers to catch them. In such circumstances, we can forc… Read More
  • Exception objects in C++ Exception objects The exception object is available only in catch block. You cannot use the exception object outside the catch block. Following steps happen when you throw an exception and catch: try {  MyException … Read More
  • Exception Handling Mechanism in C++ ( throwing an exception, the try block, catching an exception ) Exception Handling Mechanism in C++ -> C++ exception handling mechanism is basically built upon 3 keywords namely  try, throw and catch. -> The keyword try is used to preface a block of statements which may gene… Read More
  • Exception Handling in C++ Exception Handling in C++ -> We often come across some peculiar problems other than logic or syntax error. They are known as exceptions. -> Exceptions are run time anomalies(errors) or unusual conditions that A progr… Read More
  • Rethrowing an Exception in C++ Rethrowing an Exception in C++ -> A handler may decide to rethrow the exception caught without processing it. In such situations, we may simply invoke throw without any arguments as shown below:       thro… Read More

Translate

Popular Posts