0% found this document useful (0 votes)
2 views

Inheritance_program (2)

The document provides examples of multilevel and multiple inheritance in C++. The first example demonstrates a multilevel inheritance with classes A, B, and C, where class C inherits from B and A, allowing it to access roll numbers and marks. The second example illustrates multiple inheritance with classes A and B being inherited by class C, which can perform addition of values from both parent classes.

Uploaded by

tarantula97860
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Inheritance_program (2)

The document provides examples of multilevel and multiple inheritance in C++. The first example demonstrates a multilevel inheritance with classes A, B, and C, where class C inherits from B and A, allowing it to access roll numbers and marks. The second example illustrates multiple inheritance with classes A and B being inherited by class C, which can perform addition of values from both parent classes.

Uploaded by

tarantula97860
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Example of Multilevel Inherintance

#include<iostream.h>

#include<conio.h>

class A

protected:int roll;

public:

void getroll()

cout<<"Enter roll number:";

cin>>roll;

void putroll()

cout<<"Roll No.:";

};

class B:public A

protected:int s1,s2;

public:

void getmarks()

cout<<"Enter subject marks:";


cin>>s1>>s2;

void putmarks()

cout<<"\nMarks Sub.1:"<<s1<<endl<<"\nMarks sub.2:"<<s2;

};

class C:public B

int sptm;

public:

void getsptm()

cout<<"Enter sports marks:";

cin>>sptm;

void total()

putroll();

putmarks();

cout<<"\nTotal marks:"<<s1+s2+sptm;

};
int main()

clrscr();

C c;

c.getroll();

c.getmarks();

c.getsptm();

c.total();

getch();

return 0;

Example of Multiple Inherintance:

#include<iostream.h>

#include<conio.h>

class A

protected:int a;

public:

void input()

cout<<"Enter number:";

cin>>a;

}
};

class B

protected:int b;

public:

void getdata()

cout<<"Enter number:";

cin>>b;

};

class C:public A,public B

public:

void addition()

cout<<"Addition:"<<a+b;

};

int main()

clrscr();

C c;
c.input();

c.getdata();

c.addition();

getch();

return 0;

You might also like