Click here to Skip to main content
15,920,030 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
C#
#include<iostream>
class CShape
{

private:
    char cType[20];
    float fArea;

public:

    CShape(){}
    CShape(char * cType_name , float fA) : fArea(fA) { std::strcpy(cType,cType_name); }
    virtual void print()
    {

        std::cout << "Type: " << cType << std::endl;
        std::cout << "Area: " << fArea << std::endl;

    }
    virtual float area() const = 0;

};

class CCircle : public CShape
{

private:
    float fRadius;

public:

    CCircle(){}
    CCircle (float fR=0 ) : CShape("CCircle",3.14*fR*fR) { fRadius=fR;}
    virtual void print ()
    {

        CShape::print();
        std::cout << "Radius: " << fRadius << std::endl;

    }

    float virtual area() { return 3.14*fRadius*fRadius; }


};


int main()
{

    CCircle cir(1);

}


The above is my code , the system prompt that CCircle is a abstract class!
I don't know why! who can help me!
Posted

When overriding virtual methods you have to make sure that the method signatures exactly match (please see Brydon's remark). Change from
Quote:
float virtual area() { return 3.14*fRadius*fRadius; }
to
C++
virtual float area() const { return 3.14*fRadius*fRadius; }
 
Share this answer
 
v4
Comments
H.Brydon 14-May-13 23:22pm    
Let me chime in here and say that this is the only correct solution between #1, #2 and #3.

Also that "virtual float" is the same as "float virtual" when deciding whether the APIs "exactly match".
CPallini 15-May-13 3:23am    
Yes, good point, my virtual 5 :-).
I knew that, however, for some strange reasons, my hands refused to type 'float virtual'.
Take the virtual off the methods in CCircle. Try override instead.

Any class that contains a virtual function is abstract by definition.
 
Share this answer
 
Comments
CPallini 14-May-13 9:01am    
Your second sentence is slightly incorrect: an abstract class contains at least one pure virtual function.
H.Brydon 14-May-13 23:20pm    
Griff! Say what?
The compiler will not allow the declaration of object d because CCircle is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function CCircle ::area().

Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function.

what you can do is you can create a pointer to that CCricle class
i.e,
CCricle *l_ptrcricle;
l_ptrcricle= new CCricle;// will you give a error
 
Share this 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