lists with pointer // abstract classes
#include<iostream>
#include<list>
using namespace std;
class Shape
{
public:
Shape(int a,int b):x(a),y(b) {}
virtual void display()=0;
virtual void printArea()=0;
protected:
int x;
int y;
};
class Square:public Shape
{
public:
Square(int a):Shape(a,a) {}
void display()
{
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
}
void printArea()
{
cout<<"area: "<<x*x<<endl;
}
};
class Rectangle:public Shape
{
public:
Rectangle(int a,int b):Shape(a,b) {}
void display()
{
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
}
void printArea()
{
cout<<"area: "<<x*y<<endl;
}
};
class Circle:public Shape
{
public:
Circle(int a,int b):Shape(a,b) {}
void display()
{
cout<<"center: "<<x<<endl;
cout<<"radius: "<<y<<endl;
}
void printArea()
{
cout<<"area: "<<y*y*3<<endl;
}
};
#################################
#include"shape.h"
#include<iostream>
using namespace std;
int main()
{
list<Shape *> myList;
list<Shape *>::iterator myIterator;
myList.push_back(new Square(4));
myList.push_back(new Rectangle(5,3));
myList.push_back(new Circle(2,1));
for(myIterator=myList.begin(); myIterator!=myList.end(); myIterator++)
{
(*myIterator)->display();
(*myIterator)->printArea();
}
myList.pop_front();
for(myIterator=myList.begin(); myIterator!=myList.end(); myIterator++)
{
(*myIterator)->display();
(*myIterator)->printArea();
}
system("pause");
return 0;
}