C++ program using single Inheritance
Aim:
To write a program to implement Inheritance
Algorithm:
Step 1: Start
Step 2: Declare the base class student.
Step 3: Declare and define the function getdata() to get the student details.
Step 4: Declare the derived class marks.
Step 5: Declare and define the function getm() to get the marks of the student.
Step 6: Define the function display() to calculate total marks and print the student details.
Step 7: Create an object std for derived class marks.
Step 8: Call the function getdata() to read the student details.
Step 9: Call the function getm() to read the student’s marks.
Step 10: Call the function display() to print the student details and total marks.
Program Code:
#include<iostream.h> #include<conio.h> class student { public: int rno; char name[20]; void getdata() { cout<<"Enter rollno : "; cin>>rno; cout<<"Enter name : "); cin>>name; } }; class marks:public student { int m1,m2,m3,tot; public: void getm(){ cout<<"Enter 3 Marks :"; cin>>m1>>m2>>m3; } void display() { cout<<"---Student Details---\n"; cout<<"Roll Number : "<<mo<<"\n"<<"Name : "<<name<<"\n"<<"Total Marks : "<<m1+m2+m3<<"\n"; } }; void main() { marks std; clrscr(); std.getdata(); std.getm(); std.display(); getch(); }
Output:
Enter rollno: 1
Enter Name: Adeeb
Enter 3 Marks: 70 75 68
—Student Details- – –
Roll Number : 1
Name : Adeeb
Total Marks : 213