Im still a bit confused by abstract classes. I get the concept but am just a bit confused by the implementation. In the below note the addition of virtual and an = 0 to the void display procedure.
virtualvoid display()= 0;
Apparently this makes not only the function a pure virtual function but also the class an abstract class. Now the function becoming a pure virtual function makes sense. It means that the base definition of the function (if there even is one) cannot be called only the derived versions can.
Where my confusion arises from the fact that the above declaration changes the whole class to an abstract class. Does this mean that every base class member function cannot be called? If so does this not cause problems with the constructor? My understanding is that derived functions make calls to the base class constructor in their own construction?
Any clarity would be deeply appreciated.
Below is an example i have been playing around with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Item
{
private:
protected:
string sellername;
int price;
staticconst string mediumar [ ];
public:
Item(string sellername1, int price1);
virtualvoid display()= 0;
friendclass Node;
friendclass Tree;
};
What an abstract class implies is that, for your example, you cannot create any objects of type Item. You can create derived classes, but none of Item itself.
OK fine but don't you have to create an a base class object to create a derived class object. I.e. the constructor of a derived class will call the constructor of a base class?
don't you have to create an a base class object to create a derived class object
The way you word it, it sounds like two objects are created, which isn't true.
When you create a derived class object, you are creating a single object that includes the contents of the base class and the contents of the derived class. This is legal, because this single object will have an implementation for every virtual method declared. Every method declared in the class definition can be called.
Creating an object of the base class is illegal, because the object would not have an implementation for every virtual method declared. Some methods declared in the definition would not be callable, as there would be no implementation of them.
OK i think this is starting to make more sense. I think the possibility of this is messing me up.
1 2 3 4 5 6 7 8 9 10 11 12 13
class Item
{
private:
string sellername;
int price;
staticconst string mediumar [ ];
public:
Item(string sellername1, int price1)= 0;
virtualvoid display()= 0;
friendclass Node;
friendclass Tree;
};
There is a pure virtual constructor for a base class with private members. OH NO! Am i correct in thinking this messes things up and makes the private data members redundant?