Objectives: Use class inheritance to create new classes.Separate class definition and implementation in different files.Use include guard in class header files to avoid multiple inclusionof a header.
Tasks: In our lecture, we wrote a program that defines andimplements a class Rectangle. The source code can be found onBlackboard > Course Content > Classes and Objects > DemoProgram
2: class Rectangle in three files. In this lab, we will useclass inheritance to write a new class called Box. Separate the Boxclass definition and implementation in Box.h and Box.cpp. The classBox inherits class Rectangle with public inheritance. The class Boxhas one extra data member: private: int height; A. Write thefollowing constructors and member functions for class Box. Try touse the Rectangle class as much as possible.
1) Write a setHeight function to set the height according to theparameter. The valid value for the height should be positive. Use 1for an invalid value.
2) Write a getHeight function to return the height.
3) Override the print function to output the length, width, andheight of a box. Hints: use the print function of the Rectangleclass.
4) Write a default constructor to initialize length, width, andheight to 1.
5) Write an overloaded constructor that accepts three intarguments to initialize length, width, and height. The valid valuefor the height should be positive. Use 1 for an invalid value.Hints: call the overloaded constructor of the Rectangle class inthe heading of the overloaded constructor of the Box class.
6) Write a function getVolume to calculate and return the volumeof a Box object. Hints: call the getArea function of the Rectangleclass. Hints: use the getArea function of the Rectangle class.
7) Write a function equals to check if two Box objects have thesame dimension. Return true if they are equal in length, width, andheight; otherwise, return false. Hints: use the equals function ofthe Rectangle class. A Box object is also a Rectangle object. B. Inthe function main, write statements to declare objects of class Boxand test the above 2 constructors and 5 member functions.
Your project is expected to contain five source files:Rectangle.h, Rectangle.cpp, Box.h, Box.cpp, Lab7.cpp.
Rectangle.h
#ifndef Rectangle_H#define Rectangle_Hclass Rectangle //define a class{public: static int getCount(); //static member function void print() const; //member functions void setLength(int a); //mutator function void setWidth(int b); int getLength() const; //accessor function int getWidth() const; int getArea() const; bool equals(const Rectangle&) const;// Rectangle(); //default constructor Rectangle(int a = 1, int b = 1); //constructor with default parameters Rectangle(const Rectangle& x);private: int length; //data members int width; static int count; //static member variable};#endif
Rectangle.cpp
#include #include \"Rectangle.h\"using namespace std;int Rectangle::count = 0;int Rectangle::getCount(){ return count;}void Rectangle::print() const{ cout << \"length = \" << length; cout << \", width = \" << width << endl;}void Rectangle::setLength(int a){ if (a > 0) length = a; else length = 1;}void Rectangle::setWidth(int b){ if (b > 0) width = b; else width = 1;}int Rectangle::getLength() const{ return length;}int Rectangle::getWidth() const{ return width;}int Rectangle::getArea() const{ return length*width;}bool Rectangle::equals(const Rectangle& x) const{ return (length == x.length && width == x.width);}/*Rectangle::Rectangle(){ length = 1; width = 1; count++;}*/Rectangle::Rectangle(int a, int b){ setLength(a); setWidth(b); count++;}Rectangle::Rectangle(const Rectangle& x){ setLength(x.length); setWidth(x.width); count++;}