Illustrating working of public and private class
Create a class in C++ and name it room. Declare its data members: (length, breadth and height) inside the class as
private
Create a function to initialize the private variables (void getData)
Create two methods/functions called calculateArea (that returns: length * breadth) and calculateVolume (that returns: length * breadth * height ).
Create object of the room class
Pass the values of the private variables as arguments Display the calculated Area of room and Volume of room.
2
using namespace std;
class Room
{
public:
int length;
int breadth;
int Height;
void setLength( int l );
void setBreadth( int b );
void setHeight( int h )
int getArea();
int getVolume();
};
/* defining member functions */
void Rectangle::setLength( int l )
{
length = l;
}
void Rectangle::setBreadth( int b )
{
breadth = b;
}
void Rectangle::setHeight( int h )
{
breadth = b;
}
int Rectangle::getArea()
{
return length * breadth;
}
int Rectangle::getVolume()
{
return length * breadth*Height;
int main()
{
int area = rt.getArea();
cout << "Area : " << area << endl;
int Volume = rt.getVolume();
cout << "Volume : " << Volume << end2;
return 0;
}
Comments
Leave a comment