In: Computer Science
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 inclusion of a header.
Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program
2: class Rectangle in three files. In this lab, we will use class inheritance to write a new class called Box. Separate the Box class definition and implementation in Box.h and Box.cpp. The class Box inherits class Rectangle with public inheritance. The class Box has one extra data member: private: int height; A. Write the following constructors and member functions for class Box. Try to use the Rectangle class as much as possible.
1) Write a setHeight function to set the height according to the parameter. The valid value for the height should be positive. Use 1 for an invalid value.
2) Write a getHeight function to return the height.
3) Override the print function to output the length, width, and height of a box. Hints: use the print function of the Rectangle class.
4) Write a default constructor to initialize length, width, and height to 1.
5) Write an overloaded constructor that accepts three int arguments to initialize length, width, and height. The valid value for the height should be positive. Use 1 for an invalid value. Hints: call the overloaded constructor of the Rectangle class in the heading of the overloaded constructor of the Box class.
6) Write a function getVolume to calculate and return the volume of a Box object. Hints: call the getArea function of the Rectangle class. Hints: use the getArea function of the Rectangle class.
7) Write a function equals to check if two Box objects have the same dimension. Return true if they are equal in length, width, and height; otherwise, return false. Hints: use the equals function of the Rectangle class. A Box object is also a Rectangle object. B. In the function main, write statements to declare objects of class Box and 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_H class 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 <iostream> #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++; }
// Rectangle.h
#ifndef Rectangle_H
#define Rectangle_H
class Rectangle //define a class
{
public:
static int getCount(); //static member function
virtual 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, int b); //parameterized constructor
Rectangle(const Rectangle& x);
private:
int length; //data members
int width;
static int count; //static member variable
};
#endif
//end of Rectangle.h
// Rectangle.cpp
#include <iostream>
#include "Rectangle.h"
using namespace std;
int Rectangle::count = 0;
int Rectangle::getCount()
{
return count;
}
void Rectangle::print() const
{
cout << "length = " << length<<endl;
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++;
}
//end of Rectangle.cpp
// Box.h
#ifndef Box_H
#define Box_H
#include "Rectangle.h"
class Box : public Rectangle
{
private:
int height;
public:
Box();
Box(int l, int w, int h);
void setHeight(int h);
int getHeight() const;
void print() const;
int getVolume() const;
bool equals(const Box&) const;
};
#endif
//end of Box.h
// Box.cpp
#include "Box.h"
#include <iostream>
using namespace std;
// default constructor
Box::Box(): Rectangle()
{
height = 1;
}
// parameterized constructor
Box::Box(int l, int w, int h) : Rectangle(l, w)
{
height = h;
}
// set the height
void Box:: setHeight(int h)
{
if(h > 0) // validate h > 0, if it is update height else no
change
height = h;
}
// return height
int Box:: getHeight() const
{
return height;
}
// display the box dimensions
void Box:: print() const
{
Rectangle::print(); // call Rectangle's print function
cout<<"height: "<<height<<endl;
}
// return volume
int Box:: getVolume() const
{
// calculate ares*height
return getArea()*height;
}
// returns true if both boxes are equal else return false
bool Box:: equals(const Box& b) const
{
// call Rectangle's equals function and check heights are equal of
both boxes
return ((((Rectangle)*this).equals((Rectangle)b)) &&
(height == b.height));
}
//end of Box.cpp
// main.cpp : C++ program to test the Box class
#include <iostream>
#include "Box.h"
using namespace std;
int main()
{
Box b1, b2(5, 4, 8);
cout<<"Box B1: "<<endl;
b1.print();
cout<<endl<<"Box B2: "<<endl;
b2.print();
b1.setHeight(8);
cout<<"Updated height of Box B1:
"<<b1.getHeight()<<endl;
cout<<endl<<"Volume of Box B1:
"<<b1.getVolume()<<endl;
cout<<"Volume of Box B2:
"<<b2.getVolume()<<endl;
cout<<endl<<"B1 == B2:
"<<boolalpha<<b1.equals(b2)<<endl;
b1.setLength(5);
b1.setWidth(4);
cout<<"B1 == B2:
"<<boolalpha<<b1.equals(b2)<<endl;
return 0;
}
//end of main.cpp
Output: