Hi im continuing to practise for c++ for when I go to uni next year and i have a problem with a practise example I have been given. I have set it up so the user can define the shapes and size etc, but I am unable to work out what I need to add/where in the code itself a save function to store what is in the vector, and then obviously a load to load it after the vector has been saved. Any help would be sweet.....
#include<iostream>
#include<vector>
#include<cmath>
//using std
using namespace std;
//shape class
class Shape{
public:
virtual double GetArea() = 0; //get area function
};
//2dshape class
class TwoDimensionalShape : public Shape {//inherits from shape class
public:
virtual double GetArea() = 0; //get area function
};
//2dshape square
class Square : public TwoDimensionalShape{//inherits from 2dshape class
public:
double length;
Square(double ilength){
length = ilength;
}
virtual ~Square(){} // destructor
virtual double GetArea() { return length * length;}//returns the Values for get area function
};
//2dshape circle
class Circle:public TwoDimensionalShape{//inherits from 2dshape class
public:
double r;
Circle(double radius){
r=radius;
}
virtual ~Circle(){}// destructor
virtual double GetArea() { return 3.14 * r*r;}//returns the Values for get area function
};
//2dshape Triangle
class Triangle : public TwoDimensionalShape{//inherits from 2dshape class
public:
double width,height;
Triangle(double iwidth, double iheight){
width = iwidth;
height = iheight;
}
virtual ~Triangle(){} // destructor
virtual double GetArea() { return (width * height)/2;}//returns the Values for get area function
};
//3dshape class
class ThreeDimensionalShape : public Shape{//inherits from shape class
public:
virtual double GetVolume() = 0;//adds extra function for all subclass that inherit from it
};
class Cube : public ThreeDimensionalShape{//inherits from 3dshape class
public:
Cube(double isideLength){
sideLength = isideLength;
}
virtual ~Cube(){}// destructor
virtual double GetArea(){return 6*sideLength *sideLength;}//returns the Values for get area function
virtual double GetVolume(){return sideLength*sideLength *sideLength;}//returns the Values for get volume function
private:
double sideLength;
};
class Sphere : public ThreeDimensionalShape{//inherits from 3dshape class
public:
Sphere(double iradius){
radius = iradius;
}
virtual ~Sphere(){}// destructor
virtual double GetArea(){return 4 * 3.14 * radius * radius;}//returns the Values for get volume function
virtual double GetVolume(){return (4/3)*3.14*radius*radius *radius;}//returns the Values for get volume function
private:
double radius;
};
int main(){
vector <Shape*> array0(5);//array size of five
//elements in array
array0[0]= new Square(6);
array0[1]= new Circle(8);
array0[2]= new Triangle(6,10);
array0[3]= new Cube(6);
array0[4]= new Sphere(8);
for(size_t num=0;num<array0.size();num++)
{
cout<<"\nArea: "<<array0[num]->GetArea()<<endl;//outputs the area to the screen
if( ThreeDimensionalShape* pointer3DShape = (dynamic_cast<ThreeDimensionalShape*>(array0[num])) )//this if checks whether the shape is 3d, then also print the volume
{
std::cout<<"\nVolume: "<<pointer3DShape->GetVolume()<<endl;
}
}
//deleting the shapes in the array
for(size_t i=0; i < array0.size(); i++)
{
delete array0[i];
}
//cout<<endl;
cin.ignore(100, '\n');
cin.get();
return 0;
}