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

C++_Practicals solution_12_13

The document provides a series of C++ programming examples demonstrating various types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid inheritance. It also covers concepts such as constructors in derived classes, function overriding, virtual functions, and pure virtual functions. Additionally, it includes examples of using pointers to derived classes.

Uploaded by

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

C++_Practicals solution_12_13

The document provides a series of C++ programming examples demonstrating various types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid inheritance. It also covers concepts such as constructors in derived classes, function overriding, virtual functions, and pure virtual functions. Additionally, it includes examples of using pointers to derived classes.

Uploaded by

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

Practical Solution_12_13

1CE2302 (Programming in C++)


12. Develop programs using single, multilevel, multiple inheritance.

12.1 Single Inheritance

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
}
};
class B:public A
{
public:
int c;
void sum()
{
c=a+b;
cout<<"sum="<<c;
}
};
int main()
{
clrscr();
B b1;
b1.getdata();
b1.sum();
getch();
return 0;
}

12.2 Multilevel inheritance

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void getA()
{
cout<<"Enter a:";
cin>>a;
}
};
class B:public A
{
public:
int b;
void getB()
{
cout<<"Enter b:";
cin>>b;
}
};
class C:public B
{
public:
int c;
void sum()
{
c=a+b;
cout<<"sum="<<c;
}
};
int main()
{
clrscr();
C c1;
c1.getA();
c1.getB();
c1.sum();
getch();
return 0;
}
12.3 Multiple inheritance

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void getA()
{
cout<<"Enter a:";
cin>>a;
}

};
class B
{
public:
int b;
void getB()
{
cout<<"Enter b:";
cin>>b;
}
};
class C:public A, public B
{
public:
int c;
void sum()
{
c=a+b;
cout<<"sum="<<c;
}
};
int main()
{
clrscr();
C c1;
c1.getA( );
c1.getB( );
c1.sum( );
getch( );
return 0;
}

12.4 Hierarchical inheritance

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
}
};
class B:public A
{
public:
int c;
void sum()
{
c=a+b;
cout<<"sum:"<<c<<endl;
}
};
class C:public A
{
public:
int c;
void mul()
{
c=a*b;
cout<<"Mul="<<c;
}
};
int main()
{
clrscr();
B b1;
b1.getdata();
b1.sum();

C c1;
c1.getdata();
c1.mul();
getch();
return 0;
}

12.5 Hybrid Inheritance or virtual base class

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getdata()
{
cout<<"Enter a:";
cin>>a;
cout<<"Enter b:";
cin>>b;
}
};
class B: virtual public A
{
public:
int c;
void sum()
{
c=a+b;
}
};
class C: virtual public A
{
public:
int d;
void mul()
{
d=a*b;
}
};
class D: public B, public C
{
public:
void putdata()
{
cout<<"sum:"<<c<<endl;
cout<<"Mul:"<<d;
}
};
int main()
{
clrscr();
D d1;
d1.getdata();
d1.sum();
d1.mul();
d1.putdata();
getch();
return 0;
}

12.6 Constructor in derived class or passing parameters to base class constructor

#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
A(int x)
{
a=x;
}
};
class B: public A
{
public:
int b,c;
B(int x,int y):A(x)
{
b=y;
}
void sum()
{
c=a+b;
cout<<"sum="<<c;
}
};
int main()
{
clrscr();
B b1(10,20);
b1.sum();
getch();
return 0;
}

12.7 Function Overriding

#include<iostream.h>
#include<conio.h>
class A
{
public:
void show()
{
cout<<"Base Class";
}
};
class B: public A
{
public:
void show()
{
cout<<"Derived Class";
}
};
int main()
{
clrscr();
B b1;
b1.show();
getch();
return 0;
}

12.8 Virtual function

#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void show()
{
cout<<"Base Class"<<endl;
}
};
class B: public A
{
public:
void show()
{
cout<<"Derived Class";
}
};
int main()
{
clrscr();
A *ptr;
A a1;
B b1;
ptr=&a1;
ptr->show();
ptr=&b1;
ptr->show();
getch();
return 0;
}

12.9 Pure Virtual function or Abstract Class

#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void show()=0;
};
class D1: public Base
{
public:
void show()
{
cout<<"Derived1 Class"<<endl;
}
};
class D2: public Base
{
public:
void show()
{
cout<<"Derived2 Class";
}
};
int main()
{
clrscr();
Base *ptr;
D1 d1;
D2 d2;
ptr=&d1;
ptr->show();
ptr=&d2;
ptr->show();
getch();
return 0;
}
13 Develop programs using pointer to derived classes.

13.1 Pointer to object

#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int no;
char name[15];
public:
void getdata(int n,char nm[])
{
no=n;
strcpy(name,nm);
}
void putdata()
{
cout<<"No="<<no<<endl;
cout<<"Name="<<name;
}
};
int main()
{
clrscr();
student s;
student *s1;
s1=&s;
s1->getdata(1,"xyz");
s1->putdata();
getch();
return 0;
}

13.2 Pointer to derived Class

#include<iostream.h>
#include<conio.h>
class Base
{
public:
void show()
{
cout<<"Base Class"<<endl;
}
};
class Derived: public Base
{
public:
void show()
{
cout<<"Derived Class";
}
};
int main()
{
clrscr();
Derived *dptr;
Derived d1;
dptr=&d1;
dptr->show();
getch();
return 0;
}

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