creating one class object in constructor of another class

Sep 20, 2013 at 3:30pm
hello,
suppose i have two classes A and B so how to access object of class A
in constructor of B's class as a parameter
thanx in advance
Sep 20, 2013 at 3:37pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class A
{

};

class B
{
	A m_Aparam;

public:
	B(A aParam) : m_Aparam(aParam)
	{}
};


int main()
{
	A objA;
	B objB(objA);

	return 0;
}
Last edited on Sep 20, 2013 at 3:38pm
Sep 20, 2013 at 3:57pm
closed account (3qX21hU5)
@OP

It really depends on what you need to use it for. Do you need to manipulate the object of whatever object you pass in? Or do you want another instance of that class inside the class?

Do you only need it in the constructor or do you need it throughout the life of the class?

In mutexe's example above you will be creating a copy of whatever object you pass in so you won't be directly altering whatever object you pass in.

Basically what I am trying to say and just rambling on about is we need more information from you before we can give you a definite answer.
Sep 20, 2013 at 4:03pm
thnx for reply but can you please explain

"B(A aParam) : m_Aparam(aParam)"

what this line actually means
thnx
Sep 20, 2013 at 4:14pm
thank you for rply again
but i want to know about
in class B's parametrised constructor how to take class A's object as a parameter
Sep 20, 2013 at 4:16pm
closed account (3qX21hU5)
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.
Last edited on Sep 20, 2013 at 4:33pm
Sep 20, 2013 at 8:25pm
Intialization lists will initialize a variable instead of just assign it:

int a = 0; initializes a. MyInt a(0); also initializes a assuming there is a constructor MyInt::MyInt(int);.

If one class uses another, then the contained value (a in this case) will call a default consturctor unless we explicitly call another in the initialization list
1
2
3
4
5
6
class MyClass
{
    MyInt a;
public:
    MyClass() : a(0) {} // calls MyInt::MyInt(int) as a constructor instead of the default constructor
}
Topic archived. No new replies allowed.