How to use constructor of class A as part of constructor in class B?

Is this possible? Didnt find any good answer on the net :( Cheers, Tobias

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 have a couple of choices depending on exactly what you're trying to accomplish.

1) B can inherit A.

1
2
3
4
class B : public A
{
...
};

B now has access to A's public and protected members.

2) A can be a member of B

1
2
3
4
5
6
7
class B
{   A      a;    // note that it's named

     B ()
     {  // Access a's public members
     }
};


while inheriting you can call constructor of A

else not.
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.
This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <iostream>
using namespace 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:
		unsigned int noM;
	public:
		CB(unsigned int noM) : CB(double _radius, bool _sL)
		{} //However this is not working.
		;		
		void setNOM (unsigned int);
		unsigned int 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?
Oops, I changed in line 26 CB(unsigned int noM) to Planet(unsigned int _noM) .... but that doesnt change a thing
You have the right idea. What's not working?

The only thing I see is at line 28, you don't initialize Planet's private noM.
Should read:
1
2
3
CB(unsigned int noM) : CB(double _radius, bool _sL)
{ this->noM = noM; // Initialize private var from parameter
} 




Now it works, but I still have a question , but there are a few things that still puzzle me .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include <iostream>
using namespace std;
class CB
{
	protected:
		double radius;
		bool sL;
	public:
		CB(double _radius, bool _sL)
		{
			sL = false;
		}; 
		CB()
		{
			sL = false;
		};
		void setRA (double);
		void setBO (bool);
		double getRA ();
		double getBO ();
};
class Planet : public CB
{
	private:
		unsigned int noM;
	public:
		Planet(unsigned int _noM) : CB()
		{}; /* this works! If I want to use the
		constructor with parameters CB(double 
		_radius, bool _sL), than it doesn't
		work. How do I do that correctly? */		
		void setNOM (unsigned int);
		unsigned int getNOM ();
};
int main()
{
return 0;
}


1
2
3
4
int main()
{
return 0;
}


btw, your program only do one thing: exit the main
Yes, this is true... but I was told by my teacher. that we are supposed to compile every 5 minutes or so in order to find errors faster.

Now I am done with most of the code, but I still encounter problems... A lot of them. This is the final code..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;
class CB
{
	protected:
		double radius;
		bool sL;
	public:
		CB(double _radius, bool _sL)
		{
			sL = false;
		}; 
		CB()
		{
			sL = false;
		};
		void setRA (double);
		void setBO (bool);
		double getRA ();
		bool getBO ();
};
class Planet : public CB
{
	private:
		unsigned int noM;
	public:
		Planet(unsigned int _noM) : CB()
		{}; /* this works! If I want to use the
		constructor with parameters CB(double 
		_radius, bool _sL), than it doesn't
		work. How do I do that correctly? */		
		void setNOM (unsigned int);
		unsigned int getNOM ();
};
void CB::setRA (double r)
{
	radius = r;
}
void CB::setBO (bool s)
{
	sL = s;
}
double CB::getRA ()
{
	return radius;
}
bool CB::getBO ()
{
	return sL;
}	
void Planet::setNOM (unsigned int t)
{
	noM = t;
}
unsigned int Planet::getNOM ()
{
	return noM;
}	
int main()
{
Planet mars(3400.0);
cout << mars.getRA() << endl;
return 0;


I dont get an error code.... but I do NOT get 3400 back from the program.
It doesn't output 3400 because you forgot to initialize noM in the constructor.
Thanks Peter. I changed that

1
2
3
4
		Planet(unsigned int _noM) : CB()
		{
			noM = _noM;
		};


Output:

1
2
3
4
tobias@Lonestar:~/c++$ g++ na1.cpp -Wall
tobias@Lonestar:~/c++$ ./a.out
1.52096e-314


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.
If you want to pass radius and Sl from Planet's constructor into CB's constructor, you need a Planet constructor with them.
1
2
3
4
5
6
class Planet
{
public:
    Planet(double _radius, bool _sL) : CB (_radius, _sL);
...
}



@AbstractionAnon.

Tried your code and no success....

This is how I changed it.
1
2
3
4
5
6
7
8
9
10
11
12
class Planet : public CB // I removed public CB as well, like you suggested, but that caused more errors. 
{
	private:
		unsigned int 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 (unsigned int);
		unsigned int getNOM ();
};


Oh and here are the error messages :

1
2
3
4
5
6
7
8
9
10
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:
1
2
3
4
5
Planet(unsigned int _noM, double _radius, bool _sL = false) : 
CB(_radius, _sL),
noM(_noM)
{
}


This should fix it for you.

Overall it should look like this (version without default parameters):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
class CB
{
	protected:
		double radius;
		bool sL;
	public:
		CB(double _radius, bool _sL) : radius(_radius), sL(_sL) {}
		CB() : radius(0), sL(false) {}
		
		void setRA (double);
		void setBO (bool);
		double getRA ();
		bool getBO ();
};
class Planet : public CB
{
	private:
		unsigned int noM;
	public:
		Planet(unsigned int _noM, double _radius, bool _sL) : CB(_radius, _sL), noM(_noM)	{}		
		void setNOM (unsigned int);
		unsigned int getNOM ();
};
void CB::setRA (double r)
{
	radius = r;
}
void CB::setBO (bool s)
{
	sL = s;
}
double CB::getRA ()
{
	return radius;
}
bool CB::getBO ()
{
	return sL;
}	
void Planet::setNOM (unsigned int t)
{
	noM = t;
}
unsigned int Planet::getNOM ()
{
	return noM;
}	
int main()
{
	Planet mars(3, 3400.0, false);
	cout << mars.getRA() << endl;
	return 0;
}
Last edited on
thank you for your help! It was appreciated.
Topic archived. No new replies allowed.