Basic extend class problem

Hello people! I have a problem with extending a class. Basically, as a start off point, I try to extend a class with nothing additional to it. No new methods, no nothing. So I thought that after the extending part, I could create new objects, through my extended class, that would look and behave exactly like the parent class. Is this correct? But, when I try to create the objects, using the same constructor method as the parent, I get a compile error, saying “no matching function …”

If someone could provide me with a very simple (but working) example of an extended class, showing all the include files (which I tend to mess up) and any pointers etc if that is needed. If I only have a very basic working example to work out from, it would be of great help. I know there is a small code snippet in the wiki guide, but it is not enough for me to get it started …

Thanks in advance! :slight_smile:

Knut Einar

If the constructor of your base class takes parameters, then you will need a constructor for your child class too. You can either take the same parameters and pass directly to the base class, or no parameters and pass defaults to the base class constructor, or take even more paramters and keep some for yourself.

  
  
class BaseClass() {  
public:  
   BaseClass(float f) {  
      myVar = f;  
   }  
protected:  
   float myVar  
};  
  
  
class ChildClass1() {  
public:  
   ChildClass1(float f):BaseClass(f) {  
   }  
};  
  
  
class ChildClass2() {  
public:  
   ChildClass2():BaseClass(0.5) {  
   }  
};  
  
  
class ChildClass3() {  
public:  
   ChildClass3(float f, float f2):BaseClass(f) {  
      myVar2 = f2;  
   }  
  
// or you will see some C++ coders do:  
   ChildClass3(float f, float f2):BaseClass(f), myVar2(f2) {  
   }  
  
  
protected:  
   float myVar2;  
};  
  
  
  
  
  

Thanks for your answer, memo :slight_smile:

By the way; is it possible to have constructors overloaded? And, if possible, is it something one should do? I see from your examples that it is possible to ‘overload’ the child constructor in terms of the base class, but can this also be done with the base class constructor?

I need a good C++ book I think. Anyone has any good tips for that? My only book is a book that I used for a class at the university, but I don’t think it is very good.

Regards,
Knut Einar

Yes you can overload constructors and it is very common

  
  
class ofPoint {  
public:  
   ofPoint();  
   ofPoint(float x, float y, float z);  
   ofPoint(float value);  
   ofPoint(ofPoint &p);  
etc.  
}  
  

You can get some resources on C++ from the wiki
http://wiki.openframeworks.cc/index.php-…-t-Up-Guide