Objective:
- Classes and Objects in C++
- Constructors and Destructors
Activity:
Task 1:
1- Create a class named Rectangle.
2- Define its private variables length, width, variable area.
3- Create a default constructor to initialize these variables.
4- Create a parametrized constructor.
5- Create these functions:
a. Define set functions for length and width. These should check that the values are greater than zero, else this function should print “length should be greater than zero” “width should be greater than zero”. Name of functions should be like “set_length()”
b. Define calculate area function, it should set the area =length*width.
c. Define a get functions for all 3 member variables. Function names should like “get_length()”
d. Define a function set_values() that takes both length and width as input and sets those values and also sets the area.
e. Create a function is_square() it should return True if length=width of rectangle else false.
f. Define a function to display() rectangle. It should print a rectangle using 0, for example if length =3 and width = 9, display should print
000000000
000000000
000000000
Task 2:
1- Create a class named Cuboid.
2- Define its private variables length, width, height,
3- Create a default parametrized constructor to initialize these variables.
4- Create a copy constructor.
5- Inside this class make these functions:
a. Define set functions of length width and height. These values cannot be zero
b. Define get functions for all the member variables.
c. Define a function to calculate the area of cube.
d. Define a function to calculate volume of cube. This should calculate and return volume= W*H*L
e. Define a display function to display length, width, height and volume.
f. Define a function is_cube() which will return true if W=L=H else return false.
Task 3:
- Create the main.
A. Create an object of type rectangle in main using default constructor.
B. Create an object of type rectangle in main using parameterized constructor.
C. Try to set the length and width to 0, -1, it should print message.
D. Set its length and width to 3 and 9 resp.
E. Call calculate_area function
F. Use cout and get functions to print the length width and area of rectangle.
G. Call display rectangle function to print it using *s.
H. Call is_square() function for this rectangle.
I. Create an object obj1 of type cuboid in main using default parameterized constructor without passing values.
J. Create an object obj2 of type cuboid in main using default parameterized constructor by passing values in the default parameterized constructor.
K. Copy Values of obj1 in obj2 using your copy constructor function.
L. Try to set the L, W, H to 0, -1, 0 it should print message.
M. Set its height, length and width to 10, 10 and 10 resp.
N. Call get_volume() and print the volume using cout in main
O. Use cout and get functions to print the L, W, H and volume of cuboid.
P. Call display function to print the values of L, W, H and volume of cuboid.
Q. Call is cube function for this cuboid and print the returned value.
Solution:
#include<iostream>
using namespace std;
class Rectangle
{
private:
int length;
int width;
int area;
public:
Rectangle()
{
this->length = 0;
this->width = 0;
this->area = 0;
}
Rectangle(int length, int width)
{
this->length = length;
this->width = width;
this->area = 0;
}
void set_length()
{
if (length < 1)
{
cout << "Lenght should be Greater than Zero!" << endl;
}
}
void set_width()
{
if (width < 1)
{
cout << "Width should be Greater than Zero!" << endl;
}
}
void calculate_area()
{
area = length * width;
}
int get_length()
{
return this->length;
}
int get_width()
{
return this->width;
}
int get_area()
{
return this->area;
}
void set_values()
{
cout << "Enter Length:" << endl;
cin >> length;
cout << "Enter Width:" << endl;
cin >> width;
area = length * width;
}
bool is_square()
{
if (length == width)
{
return true;
}
else
return false;
}
void display()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
cout << "0";
}
cout << endl;
}
}
void display2()
{
for (int i = 0; i < length; i++)
{
for (int j = 0; j < width; j++)
{
cout << "*";
}
cout << endl;
}
}
~Rectangle() {}
};
class Cuboid
{
private:
int length;
int width;
int height;
public:
Cuboid(int length = 5, int width = 5, int height = 4)
{
this->length = length;
this->width = width;
this->height = height;
}
Cuboid(const Cuboid& obj)
{
cout << "Copy Constructor Called!!!" << endl;
this->length = obj.length;
this->width = obj.width;
this->height = obj.height;
}
void set_length()
{
do {
cout << "Enter Length greater than Zero: " << endl;
cin >> length;
if (length <= 0)
{
cout << "Error!" << endl;
}
} while (length <= 0);
}
void set_width()
{
do {
cout << "Enter Width greater than Zero: " << endl;
cin >> width;
if (width <= 0)
{
cout << "Error!" << endl;
}
} while (width <= 0);
}
void set_height()
{
do {
cout << "Enter Height greater than Zero: " << endl;
cin >> height;
if (height <= 0)
{
cout << "Error!" << endl;
}
} while (height <= 0);
}
int get_length()
{
return this->length;
}
int get_width()
{
return this->width;
}
int get_height()
{
return this->height;
}
void calculate_area()
{
int area = length * width;
cout << "Area: " << area << endl;
}
int calculate_volume()
{
int volume = length * width * height;
return volume;
}
void display(int volume)
{
cout << "Length: " << length << endl;
cout << "Width: " << width << endl;
cout << "Height: " << height << endl;
cout << "Volume: " << volume << endl;
}
bool is_cube()
{
if (length == width && length == height)
{
return true;
}
else
return false;
}
~Cuboid() {}
};
int main()
{
char choice1 = 'n';
do {
int choice2;
cout << "Press 1: For Task 1." << endl;
cout << "Press 2: For Task 2." << endl;
cout << "Press 3: For Task 3." << endl;
cin >> choice2;
if (choice2 == 1)
{
Rectangle area1, area2(10, 5);
cout << "For Default: lenght = 0 and width = 0." << endl;
area1.set_length();
area1.set_width();
area1.calculate_area();
cout << "Area = " << area1.get_area() << endl;
cout << "For Parametrized: lenght = 10 and width = 5." << endl;
area2.set_length();
area2.set_width();
area2.calculate_area();
cout << "Area = " << area2.get_area() << endl;
area1.set_values();
bool square;
square = area1.is_square();
if (square == true)
{
cout << "Length is equal to Width." << endl;
}
else
cout << "Length is not equal to Width." << endl;
area1.display();
}
if (choice2 == 2)
{
Cuboid cube1, cube2(6, 6, 3);
cout << "For Default: lenght = 5 and width = 5 and height = 4." << endl;
cout << "For Parametrized: lenght = 6 and width = 6 and height = 3." << endl;
Cuboid cube5 = cube2;
cube1.set_length();
cube1.set_width();
cube1.set_height();
cube1.calculate_area();
float volume;
volume = cube1.calculate_volume();
cube1.display(volume);
bool cube;
cube = cube1.is_cube();
if (cube == true)
{
cout << "Length is equal to Width and Height." << endl;
}
else
cout << "Length is not equal to Width and Height." << endl;
}
if (choice2 == 3)
{
Rectangle area3, area4(3, 9);
cout << "For Default: lenght = 0 and width = -1." << endl;
cout << "For Parametrized: lenght = 3 and width = 9." << endl;
cout << "When Length = 0 and Width = -1." << endl;
area3.set_length();
area3.set_width();
cout << "When Length = 3 and Width = 9." << endl;
area4.set_length();
area4.set_width();
area4.calculate_area();
cout << "Area = " << area4.get_area() << endl;
area3.set_values();
cout << "Length: " << area3.get_length() << endl;
cout << "Width: " << area3.get_width() << endl;
cout << "Area: " << area3.get_area() << endl;
bool square;
square = area3.is_square();
if (square == true)
{
cout << "Length is equal to Width." << endl;
}
else
cout << "Length is not equal to Width." << endl;
area3.display2();
Cuboid cube3, cube4(6, 6, 3);
cout << "For Default: lenght = 5 and width = 5 and height = 4." << endl;
cout << "For Parametrized: lenght = 6 and width = 6 and height = 3." << endl;
Cuboid cube6 = cube3;
cube3.set_length();
cube3.set_width();
cube3.set_height();
cube3.calculate_area();
float volume;
volume = cube3.calculate_volume();
cout << "Volume: " << volume << endl;
cout << "Length: " << cube3.get_length() << endl;
cout << "Width: " << cube3.get_width() << endl;
cout << "Area: " << cube3.get_height() << endl;
cout << endl;
cout << "Calling Display Function!" << endl;
cube3.display(volume);
bool cube;
cube = cube3.is_cube();
if (cube == true)
{
cout << "Length is equal to Width and Height." << endl;
}
else
cout << "Length is not equal to Width and Height." << endl;
}
cout << endl;
cout << "Press y: To Repeat Program!" << endl;
cin >> choice1;
} while (choice1 == 'y');
return 0;
}