I am really stuck on the last part of this. Write a class called Rectangle with
ID: 3668644 • Letter: I
Question
I am really stuck on the last part of this.
Write a class called Rectangle with double fields for its length and width. It should have set methods for both fields. It should have a constructor that takes two double parameters and passes them to the set methods. It should also have a method called area that returns the area of the Rectangle and a method called perimeter that returns the perimeter of the Rectangle.
Write a class called Square that inherits from Rectangle. It should have a constructor that takes one double parameter and passes it to the base class constructor for both parameters (the body of the constructor will be empty). Square will also need to override the setLength() and setWidth() functions of its base class such that if either of its dimensions is set to a new value, then both of its dimensions will be set to that new value (so that it remains a square). Hint: you can have the overridden versions call the versions in the base class.
The files must be called: Rectangle.hpp,Rectangle.cpp, Square.hpp and Square.cpp
Explanation / Answer
Rectangle.hpp
//*****************************************************************
// Rectangle.h
// This header file contains the Rectangle class declaration.
//*****************************************************************
#ifndef Rectangle_h
#define Rectangle_h
class Rectangle
{
// Retangle two data members called width,length double parameters
protected:double width, length;
public:
Rectangle(double length2, double width2);
//set function
void setLength(double length2);
//set function
void setWidth(double width2);
//get function
double getLength();
//get function
double getWidth();
//Calculate area of Rectangle
double area() ;
//calculate perimeter of the Rectangle
double perimeter() ;
};
#endif
//*****************************************************************
// End of File
//*****************************************************************
Rectangle.cpp
#include <iostream>
using namespace std;
/*Base class Rectangle*/
class Rectangle {
public:
Rectangle(double length2, double width2){
setLength(length2);
setWidth(width2);
}
//set function
void setLength(double length2){
length = length2;
}
//set function
void setWidth(double width2){
width = width2;
}
//get function
double getLength() const{
return length;
}
//get function
double getWidth() const{
return width;
}
//Calculate area of Rectangle
double area(){
return length * width;
}
//calculate perimeter of the Rectangle
double perimeter(){
return 2*(length + width);
}
protected: //note this is "protected" and not "private"
double length;
double width;
}; //end of class Rectangle
Write a class called Rectangle with double fields for its length and width. It should have set methods for both fields. It should have a constructor that takes two double parameters and passes them to the set methods. It should also have a method called area that returns the area of the Rectangle and a method called perimeter that returns the perimeter of the Rectangle.
Write a class called Square that inherits from Rectangle. It should have a constructor that takes one double parameter and passes it to the base class constructor for both parameters (the body of the constructor will be empty). Square will also need to override the setLength() and setWidth() functions of its base class such that if either of its dimensions is set to a new value, then both of its dimensions will be set to that new value (so that it remains a square). Hint: you can have the overridden versions call the versions in the base class.
The files must be called: Rectangle.hpp,Rectangle.cpp, Square.hpp and Square.cpp
/****************************************************************/
Square.h
//*****************************************************************
// Square.h
// Inheritance
// This header file contains the Square class declaration.
// Square is derived from the Rectangle class.
//*****************************************************************
#ifndef Square_h
#define Square_h
#include "Rectangle.h"
//---------------------------------------------------------------------------
// Square represents a rectangle whose length and width are the same length.
//---------------------------------------------------------------------------
class Square: public Rectangle
{
// Square inherits the data members and methods of base class Rectangle
//no new data members , length and height are store in base class Rectangle
public:
Square(double side): Rectangle(side,side);
//new set function
void setSide(double side);
Rectangle::setLength(side);
Rectangle::setWidth(side);
//override Rectangle's set function
void setLength(double length2);
//override Rectangle's set function
void setWidth(double width2);
//get function
double getSide() ;
};
#endif
//*****************************************************************
// End of File
//*****************************************************************
Square.cpp
/*Derived class Square with base class Rectangle.Syntax for 1st line of class definition*/
class Square: public Rectangle{
public:
/*Constructor for derived class Square.
Because Square is a derived class of Rectangle,
we can use this syntax to call the base class's constructor:
DerivedClass(datatypes parameters): BaseClass(parameters){ ... }
*/
Square(double side): Rectangle(side,side){
/*data members initialized in base
class Rectangle, so no code here*/
}
//new set function
void setSide(double side){
/*
use Rectangle's setLength() and setWidth() functions,
to set both length and width to same value
*/
Rectangle::setLength(side);
Rectangle::setWidth(side);
}
//override Rectangle's set function
void setLength(double length2){
setSide(length2);
}
//override Rectangle's set function
void setWidth(double width2){
setSide(width2);
}
//get function
double getSide() {
//use Rectangle's getLength() function
return getLength();
}
}
//no new data members , length and height are store in base class Rectangle
}; //end of class Square
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.