I have a the following classes:
parent classed called "CPolygon"
and its child class called "CTriangle" and "CRectangle"
In the main function how do I create an ARRAY of class 'CPolygon' dynamically and when the user says he wants to create a new Triangle, the CPoly[i] will become a new Triangle object and so goes for creating a Rectangle object.
I cannot use vector for this.
I read the documentation file and other forums.
#include <iostream>
usingnamespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtualint area (void) =0;
void printarea (void)
{ cout << this->area() << endl; }
};
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area (void)
{ return (width * height / 2); }
};
int main () {
//create an array of CPolygon objects called OPoly[] that will be dynamically
//increased as the user enters new entries
int choice;
while(1){
cout<< "1: Trianlge\n2: Rectangle"<<endl;
cin<<choice;
if(choice==1)
{
//make this object of CPolygon array, an object of CTriangle
//do some work here related to CTraingle
}
else
{
//make this object of CPolygon array, an object of CRectangle
//do some work here related to CRectangle
}
//increase array OPoly here
}
}
Thank you so much tath!!
But still, one problem remains unsolved for me... How to increase the array size dynamically without "cin>>size"... like everytime the it comes to the end of the while loop in my previous code, can't I increase the size of the array by 1 so that another CPolygon can be made... without using vectors
I saw a method of the object array being copied to another array of objects with one object more, then the previous one getting deleted.
Is this also correct?