Exercise 1: polygon.h is defined as follows: class polygon{ private: int sides;
ID: 3710296 • Letter: E
Question
Exercise 1:
polygon.h is defined as follows:
class polygon{
private:
int sides;
public:
polygon(int sides); //Constructor - default sides to 3
polygon(const polygon*); //Copy constructor
void setSides(int sides); //setter for private attribute
int getSides() const; //Getter for private attribute
//Other functions
double calcArea(); //calculates the area of a polygon
double calcPerimeter(); //calculates the perimeter of a polygon
void printDimensions(); //displays the dimensions of a polygon along with its area and perimeter
}
To make class polygon a pure virtual base class, choose the function or functions that must be overridden by all inherited classes. Adjust polygon.h to become a pure virtual base class. Define polygon.cpp to implement the functions defined in polygon.h.
Explanation / Answer
Polygon.h
// Make functions calcArea(), calcPerimeter() and printDimensions() virtual functions to make class polygon a pure virtual class, these functions can be defined in the child class
class polygon{
private:
int sides;
public:
polygon(int sides); //Constructor - default sides to 3
polygon(const polygon*); //Copy constructor
void setSides(int sid); //setter for private attribute
int getSides() const; //Getter for private attribute
//Other functions
virtual double calcArea(); //calculates the area of a polygon
virtual double calcPerimeter(); //calculates the perimeter of a polygon
virtual void printDimensions(); //displays the dimensions of a polygon along with its area and perimeter
}
Polygon.cpp
polygon :: polygon(int sid){
cout<<"Default constructor"<<endl;
sides = sid;
}
polygon :: polygon(const polygon*){
cout<<"Copy constructor"<<endl;
sides = new int;
sides = polygon->sides;
}
void polygon :: setSides(int sid){
sides = sides;
}
int polygon :: getSides() const {
return sides;
}
**NOTE: Virtual function implementation depends on the child class and thus implementation is not provided here.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.