0% found this document useful (0 votes)
92 views

Programmer Defined Shapes

The document contains class definitions for shapes including Circle, Square, and Triangle. It defines their member variables, default constructors, and overloaded constructors that accept user input. It also defines functions to calculate the area for each shape. The client code demonstrates using the constructors and calling the area functions to output the area for each shape based on user input.

Uploaded by

api-690889230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Programmer Defined Shapes

The document contains class definitions for shapes including Circle, Square, and Triangle. It defines their member variables, default constructors, and overloaded constructors that accept user input. It also defines functions to calculate the area for each shape. The client code demonstrates using the constructors and calling the area functions to output the area for each shape based on user input.

Uploaded by

api-690889230
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

//SPECIFICATION FILE

//contains class definition for various shapes

#ifndef SHAPES_H
#define SHAPES_H

class Circle
{
private:
float radius = 0; //initialization of member
const float PI = 3.14159;
public:
Circle(); //default constructor
Circle(float);
float area();
};

class Square
{
private:
float side = 0;//initialization of member
public:
Square();//default constructor
Square(float);
float areaSq();
};

class Triangle
{
private:
float base = 0;//initialization of member
float height = 0;//initialization of member
public:
Triangle();//default constructor
Triangle(float,float);
float areaTri();
};

#endif //!SHAPE_H
//IMPLEMENTATION FILE
//holds function definitions for class Circle

#include "Shapes.h"

//********************************
// Circle Constructors
//If there is no value found, it uses a default initialization
Circle::Circle()
{
radius = 0;//initialization of member
}

//receives a value from user and uses it to initialize object


Circle::Circle(float r)
{
radius = r;
}

float Circle::area()
{
return (PI * (radius * radius));
}

//*****************************************************
//Square Constructors
//If there is no value found, it uses a default initialization
Square::Square()
{
side = 0;//initialization of member
}

//receives a value from user and uses it to initialize object


Square::Square(float s)
{
side = s;
}

float Square::areaSq()
{
return (side * side);
}
//******************************
//Triangle Constructors
//If there is no value found, it uses a default initialization
Triangle::Triangle()
{
base = 0;//initialization of member
height = 0;
}

//receives a value from user and uses it to initialize object


Triangle::Triangle(float b, float h)
{
base = b;
height = h;
}

float Triangle::areaTri()
{
return ((height * base) / 2);
}
//CLIENT CODE
/*Isaac Bernal
Fundamentals of Programming 2
Professor Lara
Demonstrates the use of private member initializations, the use of constructors and destructors.
*/

#include <iostream>
#include "Shapes.h"
using namespace std;

int main() {
//Circle cir(5);
//Square sq(3);
//Triangle tri(6,5);
float radius;
float side;
float base;
float height;

cout << "Enter a radius: ";


cin >> radius;

Circle cir(radius); //uses overloaded constructor


cout << "Area: " << cir.area();

cin.clear();

cout << "\n\nEnter a side of a Square: ";


cin >> side;

Square sq(side);//uses overloaded constructor


cout << "Area of Square: " << sq.areaSq();

cin.clear();

cout << "\n\nEnter a Base of Triangle: ";


cin >> base;

cout << "Enter a Height of Triangle: ";


cin >> height;

Triangle tri(height,base);//uses overloaded constructor


cout << "Area of Triangle: " << tri.areaTri();
return 0;
}

You might also like