C++ Program: Single Inheritance
/*
13 C++ Program Single Level Inheritance
Create a base class containing the data member roll number and name.
Also create a member function to read and display the data using the concept of
single level inheritance. Create a derived class that contains marks of two
subjects and total marks as the data members.
*/
#include<iostream.h>
#include<conio.h>
class Student // Base Class
{
private:
long rollnumber;
char name[20];
public:
void readdata();
void display();
};
void Student::readdata( )
{
cout<<"Enter the Roll Number: ";
cin>>rollnumber;
cout<<"Enter the Student Name:";
cin>>name;
}
void Student::display( )
{
cout<<"\nRoll Number :"<<rollnumber<<endl;
cout<<"Student Name:"<<name<<endl;
}
class Report : public Student // Derived class
{
private:
int marks1, marks2, total;
public:
void readmarks();
void compute();
};
void Report::readmarks( )
{
cout<<"\nEnter Subject 1 Marks: ";
cin>>marks1;
cout<<"Enter Subject 2 Marks: ";
cin>>marks2;
}
void Report::compute( )
{
total = marks1 + marks2;
cout<<endl<<"Total Marks : "<<total;
}
void main( )
{
Report R; // Create an object R to process Student data
clrscr( );
R.readdata( );
R.display( );
R.readmarks( );
R.compute( );
getch( );
}
Comments
Post a Comment