C++ Primer
Sample Code

by
HPO SOFT
November 2003

More Complex Code Using Classes

The problem:
Create a base class Quadrilateral. This class will have a default constructor that will initialize the (x, y) coordinates for the four endpoints. The coordinate pair attributes will be placed in the private portion of the class.

Develop a public behavior that returns all the endpoints Develop a derived class Rectangle that will inherit from the base class Quadrilateral. The default constructor for Rectangle will call the constructor of Quadrilateral for the endpoints of Rectangle. The class Rectangle will have two attributes that store the lengths of the sides. The constructor of the class Rectangle will initialize the length attributes. Finally, the class Rectangle will have a public behavior that calculates and returns the area of the figure.

Lastly, develop a class Square that inherits from its base class Rectangle. The default constructor for Square will include a call to the default constructor for Rectangle. The class Square will include a behavior that determines the center of the figure based on the four endpoints of the figure and an attribute that stores the center of the figure. Also include a call to determine the area of the figure based on a call to the behavior of the class Rectangle.

Input:
The four endpoints may be either prompted for using cout and cin statements or input from a file.

Output:
Output should include statements from the constructors that display the four endpoints for each figure. Additionally, you are to output the calculated area from the class Rectangle and the center point from the class Square.

Pseudo Code:

1. Create Base Class "Quadrilateral"
2. Create Derived Class "Rectangle"
3. Create Derived Class "Square"
4. Input point coordinates, 4 sides and area as float
5. Default values will be 0
6. Calculate sides and diagonals
7. If diagonals are within 0.0001 the figure is considered a rectangle
8. Calculate and print the area of the rectangle.
9. Determine if the rectangle is a square (if side a - side b < 0.0001)
10. If the figure is a square, calculate the center point and display data
.

*******************************************************************************************************************************************

File Name: main.cp

// Main Program
//
#include "square.h" // Derived Class square
int main()
{
//*******************************************************************************
cout << "\t\t\t+y";
cout << "\n\t\t\t +";
cout << "\n\t\t\t +" << "\tpoint a" << "\t\t\t\tpoint b";
cout << "\n\t\t\t +" << "\t ax,ay" << "\t\t\t\t bx,by";
cout << "\n\t\t\t +";
cout << "\n\t\t\t +";
cout << "\n\t\t\t +" << "\t\t\t point e";
cout << "\n\t\t\t +" << "\t\t\t ex,ey";
cout << "\n\t\t\t +";
cout << "\n\t\t\t +";
cout << "\n\t\t\t +" << "\tpoint c" << "\t\t\t\tpoint d";
cout << "\n\t\t\t +" << "\t cx,cy" << "\t\t\t\t dx,dy";
cout << "\n\t\t\t +";
cout << "\n\t\t\t +";
cout << "\n\t\t\t + + + + + + + + + + + + + + + + +";
cout << "\n\t\t\t0.0" << "\t\t\t\t\t\t\t\t +x";
cout << "\n\t\t\t\tCartesian Coordinate System";
cout << "\n";
cout << "\nPoint a is upper left, b is upper right" << endl;
cout << "c is lower left, d if lower right" << endl;
cout << "\nSide a is ad, b is ba, c is bd and d is dc" << endl;
cout << "\nDiagonal a is cb, Diagonal b is ad" << endl;
cout << "************************************************************" << endl;
cout << "For a rectangle use points 4,12,11,5,-12,-4,-5,-11" << endl;
cout << "\nFor a square use points 1,9,8,2,-6,2,1,-5" << endl;
cout << "************************************************************" << endl;
Square Square_Results;
Square_Results.Square_Data();
return 0;
}
******************************************************************************************************************************************

File Name: quad.h

// Base Class Quadrilateral
//
// //*************************************************************
//The program will use cartesian coordinate system,
//The axises will be x,y as follows:
//
//
// ............................+
//................... +y.... +.. point a ..........side_a ..........point b
// ............................+.. ax,ay .......................................bx,by
//............................ +
// ............................+
// ............................+ ..side_c........... point e ...........side_b
// ............................+............................. ex,ey
// ............................+
//............................ +
//............................ + ..point c............ side d ...........point d
// ............................+ ..cx,cy ........................................dx,dy
// ............................+
// ............................+ + + + + + + + + + + + + + + + + +
// ..........................0.0............................................... +x
// Point a is upper left *** side_a is between points a and c
// Point b is upper right *** side_b is between points a and b
// Point c is lower left *** side_c is between points b and d
// Point d is lower right *** side_d is between points c and d
//
// *************************************************************

#include<iostream>
#include <cmath> // Needed fo "fabs" & "sqrt" functions
using namespace std;
class Quadrilateral
{
public:
// Declare coordinate point variables
float ax, ay, bx, by, cx, cy, dx, dy; // Corner points
Quadrilateral() // Default constructor
{
// Initialize variables to 0 ax = ay = bx = by= cx = cy = dx = dy = 0;
// Enter coordinate data points for corners
cout << "\nEnter coordinate ax: ";
cin >> ax;
cout << "Enter coordinate ay: ";
cin >> ay;
cout << "Enter coordinate bx: ";
cin >> bx;
cout << "Enter coordinate by: ";
cin >> by;
cout << "Enter coordinate cx: ";
cin >> cx;
cout << "Enter coordinate cy: ";
cin >> cy;
cout << "Enter coordinate dx: ";
cin >> dx;
cout << "Enter coordinate dy: ";
cin >> dy; };
}
~Quadrilateral() // Call Destructor
{
cout << "\n\nThe Base Class Rectangle has been destroyed!" << endl;
};
};
******************************************************************************************************************************************

File Name: rect.h

//
//DERIVED CLASS RECTANGLE
//*************************************************************
//
//Derived Class rect.h

#include <iomanip>

#include "quad.h" // Base Class file

class Rectangle : public Quadrilateral // Inherit from Class Quadrilateral
{

public:

float dx1, dy1, dx2, dy2, dx3, dy3, dx4, dy4; // differences
double side_a, side_b, side_c, side_d;
double diagonal_a, diagonal_b; // Diagonals
int typeo; // Type of Figure
double area;
double diag_diff;

 

Rectangle() // Default constructor
{

// Initialize variables to 0
area= 0;
cout << "\n********************" << endl;

// Calculate sides - dxn and dyn are x and y components
cout << "\n\tCalculating Sides and x/y Components" << endl;

dy1= ay - cy;
cout << "\ndy1= " << dy1;
dx1= ax - cx;
cout << "\ndx1= " << dx1;
side_a= sqrt(dx1 * dx1 + dy1 * dy1);
cout << "\nSide a = " << side_a;

dy2= by - ay;
cout << "\n\ndy2= " << dy2;
dx2= bx - ax;
cout << "\ndx2= " << dx2;
side_b= sqrt(dx2 * dx2 + dy2 * dy2);
cout << "\nSide b = " << side_b;

dy3= dy - by;
cout << "\n\ndy3= " << dy3;
dx3= dx - bx;
cout << "\ndx3= " << dx3;
side_c= sqrt(dx3 * dx3 + dy3 * dy3);
cout << "\nSide c = " << side_c;

dy4= dy - cy;
cout << "\n\ndy4= " << dy4;
dx4= dx - cx;
cout << "\ndx4= " << dx4;
side_d= sqrt(dx4 * dx4 + dy4 * dy4);
cout << "\nSide d = " << side_d;
cout << "\n\n********************" << endl;

// Calculate diagonals to check for rectangle
cout << "\n\tCalculating Diagonals" << endl;

diagonal_a = sqrt (side_a * side_a + side_b * side_b);
diagonal_b = sqrt (side_c * side_c + side_d * side_d);
diag_diff= fabs(diagonal_a - diagonal_b);
cout << "\nDiagonal a= " << diagonal_a
<< "\t\tDiagonal b= " << diagonal_b
<< "\nDifference= " << diag_diff << endl;

if (diag_diff == 0)
{

cout << "\n***********************************" << endl;
cout << "\tPlane Figure is a Rectangle!" << endl;
cout << "Diagonal difference zero" << endl;
cout << "Side a= " << side_a << "\t\tSide b= " << side_b << endl;
area = side_a * side_b; // Calculate the area
cout << "The Area of the Rectangle is = " << area << endl;

}

else

if (diag_diff < 0.0001)

{

cout << "\n***********************************" << endl;
cout << "\tPlane Figure is approximately a Rectangle!" << endl;
cout << "Diagonal difference less than 0.0001" << endl;
cout << "Side a= " << side_a << "\t\tSide b= " << side_b << endl;
area = side_a * side_b; // Calculate the area
cout << "The Area of the Rectangle is = " << area << endl;

}
else
{

cout << "\n\n\tPlane Figure is NOT a Rectangle!" << endl;
cout << "Diagonal difference greater than 0.0001" << endl;

}

};

~Rectangle() { }; // Call Destructor

};

******************************************************************************************************************************************

File Name: square.h

//
//DERIVED CLASS SQUARE
//
//*************************************************************
//Devived Class square.h

# include "rect.h" // Base Class file

class Square : Rectangle // Inherit from Class Rectangle
{

private:

// Declare coordinate point variables
float ex, ey;

public:

Square() // Default constructor
{

// Initialize variables to 0
ex= 0;
ey= 0;

};

~Square() { }; // Call Destructor

// Test for Square, Calculate Center Points and Display results

void Square_Data()
{

// Calculate sides - dxn and dyn are x and y components

dy1= ay - cy;
dx1= ax - cx;
side_a= sqrt(dx1 * dx1 + dy1 * dy1);
dy2= by - ay;
dx2= bx - ax;
side_b= sqrt(dx2 * dx2 + dy2 * dy2);
dy3= dy - by;
dx3= dx - bx;
side_c= sqrt(dx3 * dx3 + dy3 * dy3);
dy4= dy - cy;
dx4= dx - cx;
side_d= sqrt(dx4 * dx4 + dy4 * dy4);

// Calculate diagonals to check for rectangle

diagonal_a = sqrt (side_a * side_a + side_b * side_b);
diagonal_b = sqrt (side_c * side_c + side_d * side_d);

if ((diag_diff < 0.0001) && (fabs(side_a - side_b) < 0.0001))
{

ex= bx - (bx - cx)/2;
ey= ay - (ay - dy)/2;
cout << "\n***********************************" << endl;
cout << "\tPlane Figure is a Square!" << endl;
cout << "Side = " << side_a << endl;
cout << "Center Coordinates of the Square are: (" << ex << "," << ey << ")" << endl; }

else
{

cout << "\n***********************************" << endl;
cout << "\n\tObject is not a Square!" << endl;

}

};

};

 

******************************************************************************************************************************************

Program Output:

************************************************************

....+y .....+
..............+ ..point a............................................................................. point b
..............+ ..ax,ay................................................................................... bx,by
..............+
..............+
..............+ .........................................point e
..............+ ...........................................ex,ey
..............+
..............+
..............+ point c................................................................................ point d
..............+ ..cx,cy ..................................................................................dx,dy
..............+ + + + + + + + + + + + + + + + + + +++++++++++++++++++++
............0.0 ...............................................................................................+x
Cartesian Coordinate System

Point a is upper left, b is upper right c is lower left, d if lower right

Side a is ad, b is ba, c is bd and d is dc

Diagonal a is cb, Diagonal b is ad
************************************************************
For a rectangle use points 4,12,11,5,-12,-4,-5,-11

For a square use points 1,9,8,2,-6,2,1,-5
************************************************************

Enter coordinate ax: 1
Enter coordinate ay: 9
Enter coordinate bx: 8
Enter coordinate by: 2
Enter coordinate cx: -6
Enter coordinate cy: 2
Enter coordinate dx: 1
Enter coordinate dy: -5

********************

Calculating Sides and x/y Components

dy1= 7
dx1= 7
Side a = 9.8995

dy2= -7
dx2= 7
Side b = 9.8995

dy3= -7
dx3= -7
Side c = 9.8995

dy4= -7
dx4= 7
Side d = 9.8995

********************

Calculating Diagonals

Diagonal a= 14 Diagonal b= 14
Difference= 0

***********************************

Plane Figure is a Rectangle!
Diagonal difference zero
Side a= 9.8995 Side b= 9.8995
The Area of the Rectangle is = 98

***********************************

Plane Figure is a Square!
Side = 9.8995
Center Coordinates of the Square are: (1,2)

 

RETURN