Question

In: Computer Science

Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....

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++;
}

Solutions

Expert Solution

// 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:


Related Solutions

9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For...
Create a class Student (in the separate c# file but in the project’s source files folder)...
Create a class Student (in the separate c# file but in the project’s source files folder) with those attributes (i.e. instance variables): Student Attribute Data type (student) id String firstName String lastName String courses Array of Course objects Student class must have 2 constructors: one default (without parameters), another with 4 parameters (for setting the instance variables listed in the above table) In addition Student class must have setter and getter methods for the 4 instance variables, and getGPA method...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...
(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp - main() function Build the ItemToPurchase class with the following specifications: Default constructor Public class functions (mutators & accessors) SetName() & GetName() (2 pts) SetPrice() & GetPrice() (2 pts) SetQuantity() & GetQuantity() (2 pts) Private data members string itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 (2)...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
You are to write a class named Rectangle. You must use separate files for the header...
You are to write a class named Rectangle. You must use separate files for the header (Rectangle.h) and implementation (Rectangle.cpp) just like you did for the Distance class lab and Deck/Card program. You have been provided the declaration for a class named Point. Assume this class has been implemented. You are just using this class, NOT implementing it. We have also provided a main function that will use the Point and Rectangle classes along with the output of this main...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem :...
Objectives: use Scite Use recursion to solve a problem Create classes to model objects Problem : The Rectangle class (Filename: TestRectangle.java) Design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height....
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT