You are on page 1of 6

1.

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>

using namespace std;

class square{

public:

int side=10;

int peri;

int area;

int v;

int getperi(){

peri=4*side;

cout<<"Perimeter of square: "<<peri<<endl;

int getarea(){

area=side*side;

cout<<"Area of square: "<<area<<endl;

};

class cube: public square{

public:

int getarea(){

area=6*side*side;

cout<<"area of cube: "<<area<<endl;

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>

using namespace std;

class rectangle{

public:

int length=5;

int width=8;

public:
int area(){

cout<<"Area: "<<length*width<<endl;

};

class cuboid: public rectangle{

public:

int hei=10;

int sa;

int v;

public:

int suar(){

sa=2*length*width+2*length*hei+2*hei*width;

cout<<"Surface area of cuboid= "<<sa<<endl;

int volume(){

v=width*length*hei;

cout<<"Volume of cuboid= "<<v;

};

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.

When both data member and member function are public:

#include<iostream>

using namespace std;

class employee{

public:

int eno;

char name[20],des[20];

public:

int getdata(){

cout<<"employee ID: ";

cin>>eno;

cout<<"Employee name: ";

cin>>name;

cout<<"des: ";

cin>>des;}

};

class salary: public employee{

float basicpay,hra,da,pf,netpay;

public:
int getde(){

cout<<"basic pay: ";

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.

You might also like