hello everyone ,
Q: in a base class for example :
class shape
{
public:
virtual void print()=0;
};
why if i omit the 0 , my code doesn't work ?
below is my code.
Thank you
#include<iostream>
#include<cmath>
using namespace std;
double PI=3.1416;
class shape
{
public:
virtual void print()=0;
};
class point:virtual public shape
{
protected:
double x ,y ;
public:
point(){x=0;y=0;}
void setpoint(double a , double b) {x=a; y=b;}
point(double a , double b){x=a; y=b;}
double distance(const point p)
{
return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
void print()
{
point p;
cout<<"x = " << x << ","<<"y= " <<y<<endl;
cout<<"the distance from the origin is : "<<distance(p)<<endl;
}
};
class circle : public point
{
protected:
double r;
public:
circle(double a , double b ,double c ): point (a,b)
{
r=c;
}
double perimeter()
{
return (2*PI*r);
}
double area()
{
return (PI*PI*r);
}
void print()
{
point :: print();
cout<<"the coordinates of the center are : (" << x<< ","<<y <<")"<<endl;
cout<<"the radius is : " <<r <<endl;
cout<<"the area is :"<<area()<<endl;
cout<<"the perimeter is :"<<perimeter()<<endl;
}
};
class cylinder : public circle
{
protected:
double h;
public:
cylinder (double a , double b , double c , double d): circle (a,b,c)
{
h=d;
}
double surface()
{
return (2*PI*r*(r+h));
}
double volume()
{
return (PI*r*r*h);
}
void print()
{
circle::print();
cout<<"the height is :"<<h <<endl;
cout<<"the surface is : "<<surface()<<endl;
cout<<"the volume is : "<<volume()<<endl;
}
};
int main()
{
cout<<"part 2 :"<<endl;
const int size =100;
shape *s[size];
int choice=0;
int i=0;
double x,y,r,h;
while (choice !=4)
{
cout<<endl<<endl<<"Choose between:\npoint(1)-circle (2)-cylinder (3)- quit (4)\n ";
cin>>choice;
switch (choice)
{
case 1:
cout<<"you chose a point! :"<<endl;
cout<<"enter the point coordinates x and y : "<<endl;
cout<<"x=";cin>>x;
cout<<"y=";cin>>y;
s[i]= new point(x,y);
i++;
break;
case 2:
cout<<"you chose a circle! :"<<endl;
cout<<"enter the center coordinates x and y :"<<endl;
cout<<"x=";cin>>x;
cout<<"y=";cin>>y;
cout<<"enter the radius :";cin>>r;
s[i]=new circle(x,y,r);
i++;
break;
case 3:
cout<<"you chose a cylinder! :"<<endl;
cout<<"enter the center coordinates x and y :"<<endl;
cout<<"x=";cin>>x;
cout<<"y=";cin>>y;
cout<<"enter the radius :";cin>>r;
cout<<"enter the height :";cin>>h;
s[i]=new cylinder (x,y,r,h);
i++;
break;
case 4:
for (int j=0 ; j<i ;j++)
{
cout<<"you entered the following data in choice #"<<j<<endl;
s[j]->print();
cout<<endl<<endl;
}
break;
default:
cout<<"incorrect input ! ";
break;
}
}
for (int j=0;j<i;j++)
{
delete s[j];
}
return
0;
}