Professional Documents
Culture Documents
Design a class name Square that defines a square geometric shape. The class must have a data
member named side that defines the length of each side. Then define two member functions, getPeri
and getArea, to find the perimeter and area of the square shape. Now define a Cube that defines a cubic
shape and inherits from the Square class. The class Cube needs no new data members, but it needs the
member functions getArea and getVolume. Provide the appropriate constructors and destructors for
both classes.
#include<iostream>
class square{
public:
int side=10;
int peri;
int area;
int v;
int getperi(){
peri=4*side;
int getarea(){
area=side*side;
};
public:
int getarea(){
area=6*side*side;
int getvolume(){
v=side*side*side;
cout<<"Volume of cube"<<v<<endl;
};
int main(){
cube c;
c.getarea();
c.getperi();
c.getvolume();
square s;
s.getarea();
2.
Design a class named Rectangle with two private data members: length and width. Define
constructors and a destructor for the class and write member functions to find the perimeter and area
of a rectangle. Then define a class named Cuboid (representing a box) that inherits from the class
Rectangle with an extra data member: height. Then write constructors and a destructor for the Cuboid
class, and write member functions to find the surface and volume of the Cuboid objects.
#include<iostream>
class rectangle{
public:
int length=5;
int width=8;
public:
int area(){
cout<<"Area: "<<length*width<<endl;
};
public:
int hei=10;
int sa;
int v;
public:
int suar(){
sa=2*length*width+2*length*hei+2*hei*width;
int volume(){
v=width*length*hei;
};
int main(){
cuboid c;
c.area();
c.suar();
c.volume();
}
3.
Design Payroll System using c++ inheritance concepts
Base class name: employee
Base class Data member:
int eno;
char name[20], des[20];
Derived class name: salary
Derived class data member: float basicpay, hra, da, pf, netpay;
Demonstrate your payroll system using different access specifiers mode. (for both data member and
member function.
Write your observation for the same.
#include<iostream>
class employee{
public:
int eno;
char name[20],des[20];
public:
int getdata(){
cin>>eno;
cin>>name;
cout<<"des: ";
cin>>des;}
};
float basicpay,hra,da,pf,netpay;
public:
int getde(){
cin>>basicpay;
cout<<"HRA: ";
cin>>hra;
cout<<"DA: ";
cin>>da;
cout<<"PF: ";
cin>>pf;
int np(){
netpay=basicpay+hra+da+pf;
cout<<"Netpay: "<<netpay;
};
int main(){
salary s;
s.getdata();
s.getde();
s.np();
When base class data members are private also the code is working.
When we inherit class employee privately code is not executing.
We can access private data members of base class only from base class.