|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (10 bit)
|
pure virtual functions....
Say i have a base class, Square, which goes to child class Property, which has 3 child classes Hall, Eatery, and NonProp. if i declare a pure virtual function, action, in Square, do i need to override it in Property even if i have no use for it there? I would just implement it in Property, but my prof said to do it in the Square class, since we are going to expand on this later.
and if i do need to override it in Property, can i just make it a empty function? thanks ^dan |
|
|
|
|
|
#2 |
|
Professional gadfly
|
If a class has a pure virtual function, it is an abstract class. It can't be instantiated. If your provide a pure virtual function in Square, and don't override it in Property, then Property too will become an abstract class that can't be instantiated. That's not a problem if you never need to actually create a Square or Property object.
|
|
|
|
|
|
#3 |
|
Member (10 bit)
|
Yea they only need to be instantiated in Hall, Eatery, and NonProp, so i just used the code:
Code:
virtual void action(Person *); thanks |
|
|
|
|
|
#4 |
|
Professional gadfly
|
That's not a pure virtual function. You need to add an =0 to the end.
|
|
|
|
|
|
#5 |
|
Member (10 bit)
|
Yea, the pure virtual function is in the base class square. sorry for the confusion. here is what the h file looks like:
Code:
#ifndef SQUARE_H
#define SQUARE_H
// base class Square
class Square {
public:
Square(string= NULL, int=0);
virtual void action(Person *) = 0;
virtual ~Square() { };
protected:
string Sname;
int Sloc;
};
// class Property, inherits from Square
class Property : public Square {
public:
Property(string , int , int);
protected:
int CostToBuy;
bool IsOwned;
Person * WhoOwns;
};
// class Hall, inherits from Property
class Hall : public Property {
public:
Hall(string, int, int, int, int, int, int);
virtual void action(Person *);
protected:
int CostPerRoom;
int MaxRooms;
int InitRentCost;
int RentCostPerRoom;
int ActNumRooms;
};
// class Eatery, inherits from Property
class Eatery : public Property {
public:
Eatery(string, int, int, int) ;
virtual void action(Person *);
protected:
int CostToEat;
};
// class NonProp, inherits from Square
class NonProp : public Square {
public:
NonProp(char, string, int);
virtual void action(Person *);
protected:
char npType;
};
#endif
thanks |
|
|
|
|
|
#6 |
|
Professional gadfly
|
That looks like it should work just fine.
|
|
|
|
|
|
#7 |
|
Member (7 bit)
|
I could learn a thing or 2 from u guys about C++
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|