ARTICLE AD BOX
For one of my course activities I was tasked with using class templates on a Rectangle class then have a pointer array iterate through three Rectangle objects that will show the object's dimensions, area, and perimeter.
#include <iostream> #include <vector> #include <memory> using namespace std; class Base { public: virtual ~Base () {} virtual double get() const = 0; virtual void set() const = 0; virtual void area() const = 0; virtual void perimeter() const = 0; virtual void PrintRectangleDimensions() const = 0; }; template <typename T> class Rectangle : public Base { private: T length, width; public: Rectangle(T val1, T val2) : length(val1), width(val2) { this -> length = length; this -> width = width; } double get(string dimension) { if (dimension == "length") { return length; } if (dimension == "width") { return width; } }; void set(double value1 = 0, double value2 = 0) { if (value1 != 0) { length = value1; } if (value2 != 0) { width = value2; } }; void area() { cout << "AREA: " << length * width << endl; }; void perimeter() { cout << "PERIMETER: " << (2.0*length) + (2.0*width) << endl; }; void PrintRectangleDimensions() { cout << "Length = " << length << endl; cout << "Width = " << width << endl; }; }; int main (){ std::vector<std::unique_ptr<Base>> Rectangles[3]; Rectangles[0].set(); }I went about this using a Base class with virtual functions of all the functions in the original Rectangle class, but upon trying to set the values of one Rectangle it keeps telling me the vector I made does not have the functions I set in both the base class and the Rectangle class. What could be causing this?
124k31 gold badges281 silver badges491 bronze badges
New contributor
DaSoupman is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
15
Explore related questions
See similar questions with these tags.
