Loans | Asia Travel Forum | Buy Anything On eBay | Bad Credit Mortgages | Shares
pure virtual functions.... [Archive] - PCMech Forums

PDA

View Full Version : pure virtual functions....


GSXdan
11-18-2003, 01:15 PM
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

doctorgonzo
11-18-2003, 02:53 PM
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.

GSXdan
11-18-2003, 03:08 PM
Yea they only need to be instantiated in Hall, Eatery, and NonProp, so i just used the code:

virtual void action(Person *);


i havent compiled yet, but my prof said that would be fine.

thanks

doctorgonzo
11-18-2003, 03:11 PM
That's not a pure virtual function. You need to add an =0 to the end.

GSXdan
11-18-2003, 03:36 PM
Yea, the pure virtual function is in the base class square. sorry for the confusion. here is what the h file looks like:


#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

doctorgonzo
11-18-2003, 03:45 PM
That looks like it should work just fine.

HNPFL
11-30-2003, 11:22 PM
I could learn a thing or 2 from u guys about C++