Question

With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()'...

With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()' functions in the inheriting class to work with multiple different polygons? I need them to work for polygon 'triangle' all the way to the polygon 'octagon', but am not sure how to implement the functions to work with the different shapes without making more inheriting classes. Here is what I have so far:

class Shape {

private:

string color;

string type;

public:

Shape(string c){

setColor(c);

}

void setColor(string s){

color = s;

}

virtual string getColor(){

return color;

}

  

void setType(int t){

if (t < 3)

cout << "No Shape / Error" << endl;

if (t == 3)

type = "triangle";

if (t == 4)

type = "square";

if (t == 5)

type = "pentagon";

if (t == 6)

type = "hexagon";

if (t == 7)

type = "heptagon";

if (t == 8)

type = "octogon";

}

string getType(){

   return type;

}

virtual double calculatePerimeter(double n)=0;

virtual double calculateArea()=0;

};

// Inheriting Class *******************************

class Polygon : public Shape {

private:

int sides;

double sideLength;

public:

      Polygon(string s, int n) : Shape(s){

setColor(s);

setType(n);

}

//Not sure how to implement the two bottom functions below to work with multiple polygons from triangle all the way through octagon

double calculatePerimeter(double n){return 0;};

double calculateArea(){return 0;}

};

int main(){

vector polygonList; // list of types of polygons

polygonList.push_back( new Polygon ("red", 3)); // 3 sides so it will be a triangle

polygonList.push_back( new Polygon ("blue", 4)); // 4 sides so it will be a square

// The bottom two lines should give perimeter and area to triangle and square (should work all the way up to octagon/not implemented need guidance)

cout << "The area of shape 1 is > " << polygonList.at(0)->calculateArea() << " its perimeter is > " << polygonList.at(0)->calculatePerimeter() << endl;

cout << "The area of shape 2 is > " << polygonList.at(1)->calculateArea() << " its perimeter is > " << polygonList.at(1)->calculatePerimeter() << endl;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Area of a regular polygon is (sideLength*sideLength*sides)/(4*tan(180/sides)) and its perimeter is sideLength*sides. We change the constructor in Polygon to include a double variable to initialize sideLength and we change the parameter list of calculatePerimeter() since the int is already in the class and not needed.

SOURCE CODE IN C++:

#include <iostream>

#include <cmath>

#include <vector>

using namespace std;

class Shape {

private:

string color;

string type;

public:

Shape(string c){

setColor(c);

}

void setColor(string s){

color = s;

}

string getColor(){

return color;

}


void setType(int t){

if (t < 3)

cout << "No Shape / Error" << endl;

if (t == 3)

type = "triangle";

if (t == 4)

type = "square";

if (t == 5)

type = "pentagon";

if (t == 6)

type = "hexagon";

if (t == 7)

type = "heptagon";

if (t == 8)

type = "octogon";

}

string getType(){

return type;

}

virtual double calculatePerimeter()=0;

virtual double calculateArea()=0;

};

// Inheriting Class *******************************

class Polygon : public Shape

{

private:

int sides;

double sideLength;

public:

Polygon(string s, int n,double l) : Shape(s)

{

setType(n);

sides=n;

sideLength=l;

}

//Not sure how to implement the two bottom functions below to work with multiple polygons from triangle all the way through octagon

double calculatePerimeter()

{

return sides*sideLength;

}

double calculateArea()

{

return (sideLength*sideLength*sides)/(4*tan(180/sides));

}

};

int main()

{

vector<Polygon*> polygonList; // list of types of polygons

polygonList.push_back(new Polygon("red", 3, 3)); // 3 sides so it will be a triangle

polygonList.push_back(new Polygon("blue", 4, 4)); // 4 sides so it will be a square


// The bottom two lines should give perimeter and area to triangle and square (should work all the way up to octagon/not implemented need guidance)

cout << "The area of shape 1 is > " << polygonList.at(0)->calculateArea() << " its perimeter is > " << polygonList.at(0)->calculatePerimeter() << endl;

cout << "The area of shape 2 is > " << polygonList.at(1)->calculateArea() << " its perimeter is > " << polygonList.at(1)->calculatePerimeter() << endl;

}

OUTPUT:

The area of shape 1 is > 21.0911 its perimeter is > 9 The area of shape 2 is > 9.87791 its perimeter is > 16

Add a comment
Know the answer?
Add Answer to:
With the given C++ code, what is the best way to utilize the 'calculatePerimeter()' and 'calculateArea()'...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields...

    Design a class named Triangle that extends GeometricObject class. The class contains: Three double data fields named side1, side2, and side3 with default values 1.0 to denote three sides of the triangle. A no-arg constructor that creates a default triangle with color = "blue", filled = true. A constructor that creates a triangle with the specified side1, side2, side3 and color = "blue", filled = true. The accessor functions for all three data fields, named getSide1(), getSide2(), getSide3(). A function...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...

    // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello virtual function (hint this will be done in Person class // 2) Change main to create two Person pointer variables // 3) Set each pointer to one of the two objects already instantiated below // 4) Use the pointer variable to invoke the two sayHello() methods #include <iostream > #include <string > using namespace std ; class Person { protected : string name; int...

  • Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the...

    Write a class named Octagon (Octagon.java) that extends the following abstract GeometricObject class and implements the Comparable and Cloneable interfaces. //GeometricObject.java: The abstract GeometricObject class public abstract class GeometricObject { private String color = "white"; private boolean filled; private java.util.Date dateCreated; /** Construct a default geometric object */ protected GeometricObject() { dateCreated = new java.util.Date(); } /** Construct a geometric object with color and filled value */ protected GeometricObject(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color;...

  • C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your...

    C++ Please complete the implementation of the following source code (Question3.cpp). You need to add your code in the source code where the comment “// your code” locates. After you finish the implementation, please also provide the output of your program. #include <iostream> using namespace std; class Shape { protected: // your code public: void setWidth (int w) { // your code } void setHeight (int h) { // your code } }; class Rectangle: public Shape { public: int...

  • This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to...

    This is a c++ program. Use the description from Parking Ticket Simulator (listed below) as a basis, where you need to create a Car class and Police Officer class, to create a new simulation. Write a simulation program (refer to the Bank Teller example listed below) that simulates cars entering a parking lot, paying for parking, and leaving the parking lot. The officer will randomly appear to survey the cars in the lot to ensure that no cars are parked...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT