Object Oriented Programming Practical 4
Question 1
Define a class Room with data members length, breadth and height. Member function calculate_area () and calculate_volume(). Calculate the area and volume of room. Define the member function inside the class.
room.cpp
C
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class room
{
public:
int length,breadth,height;
void accept()
{
cout<<"\nEnter length of a room: ";
cin>>length;
cout<<"\nEnter breadth of a room: ";
cin>>breadth;
cout<<"\nEnter height of a room: ";
cin>>height;
}
void calculate_area()
{
int area;
area=length*breadth;
cout<<"Area of room is: "<<area<<" square feets"<<endl;
}
void calculate_volume()
{
int volume;
volume=length*breadth*height;
cout<<"Volume of room is: "<<volume<<" cubic feets"<<endl;
}
};
void main()
{
clrscr();
room r;
r.accept();
r.calculate_area();
r.calculate_volume();
getch();
}
Output

Question 2
Define a class mean in which assign two numbers in assign member function (i.e. assign(4,8)) passed value from main function and define assign member function inside the class and display the mean of two number on output screen
mean.cpp
C
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class mean
{
public:
int num1,num2;
void assign(int x,int y)
{
num1=x;
num2=y;
}
void display()
{
float m;
m=(num1+num2)/2.0;
cout<<"Mean is: "<<m<<endl;
}
};
void main()
{
clrscr();
int a,b;
mean m;
cout<<"Enter number 1: ";
cin>>a;
cout<<"Enter number 2: ";
cin>>b;
m.assign(a,b);
m.display();
getch();
}
Output
