Illustrating working of class ad objects
Create a class in C++ and name it room. Declare some data members: (length, breadth and height) inside the class.
Create two methods/functions called calculateArea (that returns: length * breadth) and calculateVolume (that returns: length * breadth * height ).
Create object of the room class.
Assign values to the data members declared
Display the calculated Area of room and Volume of room.
#include <iostream>
using namespace std;
class Room
{
private:
double length;
double breadth;
double height;
public:
double calculateArea()
{
return length*breadth;
}
double calculateVolume()
{
return length*breadth*length;
}
void setLength(double length)
{
this->length = length;
}
void setBreadth(double breadth)
{
this->breadth = breadth;
}
void setHeight(double height)
{
this->height = height;
}
double getLength()
{
return length;
}
double getBreadth()
{
return breadth;
}
double getHeight()
{
return height;
}
};
int main()
{
Room room;
room.setLength(3);
room.setBreadth(4);
room.setHeight(5);
cout << "length: " << room.getLength() << endl;
cout << "breadth: " << room.getBreadth() << endl;
cout << "height: " << room.getHeight() << endl;
cout << endl << "Area: " << room.calculateArea();
cout << endl << "Volume: " << room.calculateVolume();
return 0;
}
Comments
Leave a comment