Assignment help, new to C++, is to use a base class, derived classes, overridden functions, and polymorphism.

The purpose of this code is to use a base class, derived classes, overridden functions, and polymorphism. Modify this code for each member function given for each class and your main function. Do not modify the declaration section given for each class by adding more data members or member functions.


CODE:
#include <iostream>
#include <iomanip>
using namespace std;

/******************Parallelogram Class****************************/
//class declaration section
class Parallelogram
{
private:
void setAngles(int, int);
protected:
//data members
int angleA;
int angleB;
int sideA;
int sideB;

public:
Parallelogram(int, int, int = 90, int = 90); //constructor with default arguments for angleA and angleB
int getAngleA();
int getAngleB();
void setSideA(int);
void setSideB(int);
int getSideA();
int getSideB();
int computePerimeter();
int computeArea();
};




/******************Rectangle Class****************************/
//class declaration section
class Rectangle
{
public:
Rectangle(int, int); //constructor
};




/******************Square Class****************************/
//class declaration section
class Square
{
public:
Square(int); //constructor
void setSideA(int);
void setSideB(int);
};





void displayData(Parallelogram); // function prototype

int main()
{



}

/****************************************************************************/
/* Because of inheritance and polymorphism the displayData function can be
passed either a Parallelogram object, Rectangle object, or a Square
object to display each object's data. */
void displayData(Parallelogram shape)
{
cout << "\nThe length of side A is " << shape.getSideA() << endl;
cout << "The length of side B is " << shape.getSideB() << endl;
cout << "Angle A is " << shape.getAngleA() << " degrees" << endl;
cout << "Angle B is " << shape.getAngleB() << " degrees" << endl;
cout << "The perimeter is " << shape.computePerimeter() << endl;
cout << "The area is " << shape.computeArea() << endl << endl;
}

INSTRUCTIONS
The Parallelogram.class is the base class and contains four protected data members to store a sideA, sideB, angleA, and angleB.
a. Code the getAngleA, getAngleB, getSideA, getSideB member functions to return back the value of the associated data member.
b. Code the setSideA and setSideB member functions to assign a value for the associated data member.
c. Code the computePerimeter member function to compute and return back the perimeter of a Parallelogram. This formula is given in the hyperlink for step 1.
d. Code the computeArea memberr function to compute and return back the area of a Parallelogram. This formula is given in the hyperlink for step 1.
e. Code the setAngles member function (this is a private function) to assign a value for angleA and angleB based on the values passed to the two parameters of this function. Only assign a value to angleA and angleB if the sum of the two values passed to this function are 180. If the sum of the two values passed to this function are not 180 degrees then assign both angleA and angleB the default value of 90.
f. The Parallelogram class constructor will accept four arguments which will contain a value for each data member. Call the setAngles member function from within this constructor to assign a value for angleA and angleB using two of the parameters. Assign a value to sideA and sideB using the other two parameters.

Code the Rectangle class to inherit from the Parallelogram class. The inheritance will be public. No additional data members should be added to the Rectangle class because it will inherit the data members from the Parallelogram class.
a. The Rectangle class constructor will accept two arguments which will contain a value for each side of the rectangle (sideA and sideB). Use a base/member initialization list to call the Parallelogram constructor to assign values for sideA and sideB of the Rectangle.

Code the Square class to inherit from the Rectangle class. The inheritance will be public. No additional data members should be added to the Square class because it will inherit the data members from the Rectangle class (which in turn inherited its data members from the Parallelogram class).
a. The setSideA member function will override this same function from the parent class. Within this function assign the value of the data members sideA and sideB to the argument of this function. This means both data members will be assigned the same value because sideA and sideB of a square are equal.
b. The setSideB member function will override this same function from the parent class. Within this function assign the value of the data members sideA and sideB to the argument of this function. This means both data members will be assigned the same value because sideA and sideB of a square are equal.
c. The Square class constructor will accept one argument which will contain a value for each side of the square (sideB will be assigned the same value as sideA). Use a base/member initialization list to call the Rectangle constructor to assign values for sideA and sideB of the Square. Keep in mind that sideA and sideB of a square are equal.

The main function will be used to test your class hierarchy.
a. Test the Square class first. Prompt the user to enter in the side length of a square. Instantiate a Square object using the data entered in by the user. Call the displayData function passing it the Square object to display the values for this square.
b. Test the Rectangle class second. Prompt the user to enter in the first side length of a rectangle (sideA). Prompt the user to enter in the second side length of a rectangle (sideB). Instantiate a Rectangle object using the data entered in by the user. Call the displayData function passing it the Rectangle object to display the values for this rectangle.
c. Test the Parallelogram class third. Prompt the user to enter in the first side length of a parallelogram (sideA). Prompt then prompt the user to enter in the second side length of a parallelogram (sideB). Prompt the user to enter in the first angle of a parallelogram (angleA). Prompt the user to enter in the second angle of a parallelogram (angleB). Instantiate a Parallelogram object using the data entered in by the user. Call the displayData function passing it the Parallelogram object to display the values for this parallelogram.

Shape Class Hierarchy Guide

A square is a rectangle. A rectangle is a parallelogram. For the purposes of this assignment, all three shapes share the common characteristics of having an angle A, an angle B, a side A, and a side B. Information about each shape is given below.



Parallelogram

A parallelogram has a sideA and a sideB.

A parallelgoram has an angleA and angleB where the addition of these two angles is 180 degress.

The perimeter of a parallelogram is 2 * (sideA + sideB)

The area of a parallelogram is sideA * sideB




Rectangle
A rectangle has a sideA and a sideB.

A rectangle has an angleA and angleB where both angles are 90 degrees.

The perimeter of a rectangle is 2 * (sideA + sideB)

The area of a rectangle is sideA * sideB






Square
A square has a sideA and sideB where both sides are equal.

A square has an angleA and angleB where both angles are 90 degrees.

The perimeter of a square is 2 * (sideA + sideB)

The area of a square is sideA * sideB

Topic archived. No new replies allowed.