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...

  • // This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int...

    // This program uses the programmer-defined Rectangle class. #include "Rectangle.cpp" #include <iostream> using namespace std; int main() {    Rectangle rectangle1;    Rectangle rectangle2;    rectangle1.setLength(10.0);    rectangle1.setWidth(5.0);    rectangle2.setLength(7.0);    rectangle2.setWidth(3.0);    cout << "Perimeter of rectangle1 is " << rectangle1.calculatePerimeter() << endl;    cout << "Area of rectangle1 is " << rectangle1.calculateArea() << endl;    cout << "Perimeter of rectangle2 is " << rectangle2.calculatePerimeter() << endl;    cout << "Area of rectangle2 is " << rectangle2.calculateArea() << endl;...

  • 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...

  • previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print...

    previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() {    //create Animal object    Animal animal("Dog","Mammal",4);    cout<<"Animal object details"<<endl;    cout<<"Name: "<<animal.getName()<<endl;    cout<<"Type: "<<animal.getType()<<endl;    cout<<"# of Legs: "<<animal.getLegs()<<endl;          cout<<endl<<endl;    //create Cat object    Cat cat("byssinian cat","carnivorous mammal",4,"short...

  • Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task...

    Objective Extend the class GeometicShapes to include a Triangle class.. Background Reading ZyBooks Chapter 10 Task Create the following fields and methods for a Triangle class that extends the provided GeometricShapes class public class GeometricShapes { private String color = "red"; private boolean filled; private java.util.Date dateCreated;    public GeometricShapes() { dateCreated = new java.util.Date(); }    public GeometricShapes(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; }    public void setColor (String color)...

  • 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...

  • 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;...

  • 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...

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