class A
{
private:
// some variables
public:
A()
{
day = 1;
month = 1;
};
// some methods
};
class B
{
private:
// variables
public:
B()
{
A() /* IS THIS POSSIBLE? Can I use
constructor A() with its values in B? */
// other B specific variables
}
// some methods
};
You don't need to do anything in case of default constructor - the compiler will automatically call it for you. If you want to call a non-default constructor it is done like this:
1 2 3 4 5 6 7 8 9 10 11
class A {
public:
A(int i) {}
};
class B: public A {
public:
B(int x) : A(x), _x(x) {}
private:
int _x;
};
So basically, you initialize the base classes in the same way as you initialize member variables.
#include <iostream>
usingnamespace std;
class CB
{
protected:
double radius;
bool sL;
public:
CB(double _radius, bool _sL)
{
sL = false;
}; /*I am supposed to set sL(selfLuminating to false
) = false) and the constructor CB(double _radius,
bool _sL) should be part of the constructor in class
Planet */
void setRA (double);
void setBO (bool);
double getRA ();
double getBO ();
};
class Planet : public CB
{
private:
unsignedint noM;
public:
CB(unsignedint noM) : CB(double _radius, bool _sL)
{} //However this is not working.
;
void setNOM (unsignedint);
unsignedint getNOM ();
};
int main()
{
return 0;
}
Sorry about my stupid questions, but I am new to all this.
I tried to implement the ideas of KRAkatau, but I couldnt get it to work. Any ideas?
At the end of the day, I assume, if I could use the other constructor (the one that accepts parameters), than it would work just fine, BUT I still havent figured out how to use the constructor with parameters as part of the constructor in class Planet.
class Planet : public CB // I removed public CB as well, like you suggested, but that caused more errors.
{
private:
unsignedint noM;
public:
Planet(double _radius, bool _sL) : CB(_radius, _sL)
{
sL = false;
}; // and I would like to assign a value to "noM" as well, not sure how this is done.
void setNOM (unsignedint);
unsignedint getNOM ();
};
tobias@Lonestar:~/c++$ g++ na2.cpp -Wall
na2.cpp: In function ‘int main()’:
na2.cpp:61:14: error: no matching function for call to ‘Planet::Planet(int)’
na2.cpp:61:14: note: candidates are:
na2.cpp:28:3: note: Planet::Planet(double, bool)
na2.cpp:28:3: note: candidate expects 2 arguments, 1 provided
na2.cpp:23:7: note: Planet::Planet(const Planet&)
na2.cpp:23:7: note: no known conversion for argument 1 from ‘int’ to ‘const Planet&’
I guess you noticed that you don't need to pass the types of variables to the constructor of parent class, so at least the initialization is done right now.
As for the error message - it says exactly what it means. Now you have a constructor for planet with two parameters: radius and sL (whatever it is). But in your main function you only pass one parameter. I assume it looks like this now:
Planet mars(3400);
But you need to pass the second boolean parameter:
Planet mars(3400, false);
or add a default value for that parameter in the constructor:
1 2 3 4
Planet(double _radius, bool _sL = false) : CB(_radius, _sL)
{
sL = false; // By the way, this assignment is unnecessary - the base-class constructor already does that
}; // this ";" is not necessary and your compiler might warn you about this
Finally, if you want to assign noM, then just pass it to a constructor as well: