This type of inheritance is also known as simple inheritance,as there is only one base class and only one derived class, the simple form  of inheritance.

class A is the base class and class B the derived class.Class B inherits all the public and protected members from class A. If the derivation is public, the protected or public member of Class A are respectively protected or public in B. If the derivation is private all the protected and public members of class A are private in B, whereas if the derivation is protected, all the protected and public members of class A are protected in B.

Illustrates single inheritance using public derivation

#include<iostream>
using namespace std;
class stud
{
private:
char name[20];
char roll[8];
protected:
int age;
public:
void getstudent()
{
cout<<"Name:";
cin>>name;
cout<<"Roll:";
cin>>roll;
cout<<"Age";
cin>>age;
}
void showstudent()
{
cout<<endl<<"Name:"<<name<<endl<<"Roll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
}
};
class student:public stud
{
private:
int rank;
public:
void inputdata()
{
cout<<"Rank:";
cin>>rank;
}
void modifydata()
{
char bool;
cout<<endl<<"Do you want to modify?(y/N):";
cin>>bool;
if(bool=='Y')
cout<<"Age:";
cin>>age;
cout<<"Rank:";
cin>>rank;
}
}
void outputdata()
{
cout<<"Rank:"<<rank;
}
};
int main()
{
student s;
s.getstudent(); //accessing base class member using derived class object
s.inputdata();
s.showstudent(); // accessing base class member using derived class object
s.outputdata();
s.modifydata();
s.showstudent();
s.outputdata();
}

OUTPUT:

Name:John
Roll:10
Age:18
Rank:3
Name:John
Roll:10
Age:18
Rank:3
Do you want to modify?(Y/N):Y
Age:28
Rank:2
Name:John
Roll:10
Age:28
Posted by Unknown On 00:28 No comments

0 comments:

Post a Comment

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Blog Archive

Contact Us


Name

E-mail *

Message *