Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Inheritance/Constructor problem
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dwindle




PostPosted: Mon Mar 27, 2006 10:50 am   Post subject: Inheritance/Constructor problem

code:
class Base
{
      protected:
            int x, y;
      public:
            Base() : x(0), y(0)
                  { }
            Base(int nx, int ny) : x(nx), y(ny)
                  { }
};

class Derived : public Base
{
      protected:
            int z;
      public:
            Derived() : x(0), y(0), z(0)
                  { }
};



I get an error at the Derived constructor line when I try to compile. Apparently the x and y fields don't exist but I know it does since it's inherited from the base class. Also if I put it as normal assignment statements inside the brackets it'll work but I really shouldn't have to do that should I? This is the simplified version of an actual program I'm working on and I wanted to split up the initializing variables part of the contructor from some stuff I actually wanted it to do. I'm using Dev-Cpp and I'm wondering if this code works ok for other people.

Also how do I indent certain lines when I post? I tried just adding spaces before but that doesnt work.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Mar 27, 2006 12:47 pm   Post subject: (No subject)

Use code tags.

code:
[code]foo   bar[/code]


code:
foo   bar


As for your problem... why are you not simply calling the Base class' constructor from the Derived class?
Dwindle




PostPosted: Mon Mar 27, 2006 8:15 pm   Post subject: (No subject)

thanks, both problems solved.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: