Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 11-18-2003, 12:15 PM   #1
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
GSXdan is offline   Reply With Quote
Old 11-18-2003, 01:53 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
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.
doctorgonzo is offline   Reply With Quote
Old 11-18-2003, 02:08 PM   #3
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
Yea they only need to be instantiated in Hall, Eatery, and NonProp, so i just used the code:
Code:
virtual void action(Person *);
i havent compiled yet, but my prof said that would be fine.

thanks
GSXdan is offline   Reply With Quote
Old 11-18-2003, 02:11 PM   #4
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
That's not a pure virtual function. You need to add an =0 to the end.
doctorgonzo is offline   Reply With Quote
Old 11-18-2003, 02:36 PM   #5
Member (10 bit)
 
GSXdan's Avatar
 
Join Date: Jun 2002
Location: P-burg, Ohio, USA
Posts: 555
Send a message via AIM to GSXdan
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
GSXdan is offline   Reply With Quote
Old 11-18-2003, 02:45 PM   #6
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
That looks like it should work just fine.
doctorgonzo is offline   Reply With Quote
Old 11-30-2003, 10:22 PM   #7
Member (7 bit)
 
Join Date: Nov 2003
Location: LA
Posts: 101
Send a message via AIM to HNPFL
I could learn a thing or 2 from u guys about C++
HNPFL is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:42 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2