It is a class Constructor with a Initialization List. Basically it works the same way as a normal constructor that looks like this
1 2 3 4 5 6 7 8 9 10
|
class One
{
public:
One(int param)
{
number = param;
}
int number;
};
|
Except with a few differences but that might be a little advanced for now so I won't mention them. But I will say that you should use Constructor's with Initialization Lists instead of the constructor in my example.
To use a Initialization List is quite simple actually. For each member you have in the class you can set a default value for that member. We do this by following the parameters of the constructor with a colon ":" and then start listing our members and the values you want assigned to them inside braces "( )".
So for example like this
One(int param) : number(param), numberTwo(54), numberThree(param + 54) {}
As you can see we can use other variables, literals, expressions, and even functions to initialize the members.
EDIT:
but i want to know about
in class B's parametrised constructor how to take class A's object as a parameter |
So basically you need to use class a object from class A in your constructor only and don't need it after that?
If that is the case just pass in a reference or pointer the the object. For example here would be a constructor that can use a Object from class A.
Class B's constructor.
1 2 3 4 5 6 7
|
// If you aren't altering anything in class A and just need it's info use a
// const reference.
B(const A& object)
{
// Do whatever you need to do with class A's object.
// You just can't alter it in any way.
}
|
If you do need to alter class A's object just remove the const.