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

Multilevel Inheritance

This document discusses different types of inheritance in C++ including multilevel inheritance, multiple inheritance, hybrid inheritance, and virtual base class. Multilevel inheritance involves deriving one class from another derived class. Multiple inheritance involves deriving a class from more than one base class. Hybrid inheritance combines multilevel and multiple inheritance where a class is derived from two unrelated classes. Virtual base class solves issues with multiple inheritance by making the common base class as virtual.

Uploaded by

rkukg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Multilevel Inheritance

This document discusses different types of inheritance in C++ including multilevel inheritance, multiple inheritance, hybrid inheritance, and virtual base class. Multilevel inheritance involves deriving one class from another derived class. Multiple inheritance involves deriving a class from more than one base class. Hybrid inheritance combines multilevel and multiple inheritance where a class is derived from two unrelated classes. Virtual base class solves issues with multiple inheritance by making the common base class as virtual.

Uploaded by

rkukg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

MULTILEVEL INHERITANCE:

#include <iostream.h>

class student
{
protected:
int roll;
public:
void getno();
int putno();
};

void student::getno()
{
cout<<"enter roll numbe"<<endl;
cin>>roll;
}

int student::putno()
{
return(roll);
}

class test:public student


{
protected:
int mark1;
int mark2;
public:
void getmark();
void putmark();
};

void test::getmark()
{
cout<<"enter mark"<<endl;
cin>>mark1>>mark2;
}

void test::putmark()
{
cout<<mark1<<"\n"<<mark2;
}

class result:public test


{

int total;
public:
void display();
};
void result::display()
{
total = mark1+mark2;
cout<<"rollno:"<<putno()<<"\n"<<"marks"<<"\n";
putmark();
cout<<"total\n"<<total<<endl;
}

void main()
{
result stdx;
stdx.getno();
stdx.getmark();
stdx.display();
}

MULTIPLE INHERITANCE

class m
{
protected:
int a;
public:
void get1();
};

void m::get1()
{
cout<<"enter number1"<<"\n";
cin>>a;
}
class n
{
protected:
int b;
public:
void get2();
};

void n::get2()
{
cout<<"enter number2"<<"\n";
cin>>b;
}
class p:public m,public n
{
int c;
public:
void mul();
};

void p::mul()
{
cout<<"result is "<<a*b;
}

void main()
{
p data;
data.get1();
data.get2();
data.mul();
}

HYBRID INHERITANCE
#include <iostream.h>

class student
{
protected:
int roll;
public:
void getno();
};

void student::getno()
{
cout<<"enter roll"<<"\n";
cin>>roll;
}

class test:public student


{
protected:
int mark1,mark2;
public:
void getmark();
};

void test::getmark()
{
cout<<"enter marks"<<"\n";
cin>>mark1>>mark2;
}

class sports
{
protected:
int weight;
public:
void getweight();
};

void sports::getweight()
{
cout<<"enter weightage"<<"\n";
cin>>weight;
}

class result:public test,public sports


{
int total;

public:
void display();

};

void result::display()
{
total = mark1+mark2+weight;
cout<<"total is "<<total;
}

void main()
{
result study;
study.getno();
study.getmark();
study.getweight();
study.display();
}

VIRTUAL BASE CLASS


#include <iostream.h>

class student
{
protected:
int roll;
public:
void getno();
};

void student::getno()
{
cout<<"enter number"<<"\n";
cin>>roll;
}

class test:public virtual student


{
protected:
int mark1,mark2;
public:
void getmarks();
};

void test::getmarks()
{
cout<<"enter marks"<<"\n";
cin>>mark1>>mark2;
}

class sports:public virtual student


{
protected:
int weight;
public:
void getweight();
};

void sports::getweight()
{
cout<<"enter weightage"<<"\n";
cin>>weight;
}

class result:public test,public sports


{
int total;
public:
void display();
};

void result::display()
{
cout<<"\n"<<"the sum is "<<(mark1+mark2+weight)<<"\n";
}

void main()
{
result student;
student.getno();
student.getmarks();
student.getweight();
student.display();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy