Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
#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()
{
    //part 2
    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;
}
Posted
Updated 11-Oct-10 10:23am
v2

1 solution

C++
virtual void print() = 0;


declares a pure virtual method (and makes the class shape abstract).


On the other hand
C++
virtual void print();


declares (just) a virtual method: you must provide the method's implementation, otherwise the compiler will complain.


[added]

See, for instance "difference between a virtual function and a pure virtual function"[^] at stack overflow.

[/added]
:)

 
Share this answer
 
v3
Comments
joek1991 11-Oct-10 16:38pm    
I am sorry pallini but i didn't understand clearly what you mean . Can you please clarify . i am really sorry. thank you
Dalek Dave 11-Oct-10 18:35pm    
Good answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900