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





Translate

Popular Posts