is the below code is valid
class test
{
int a;
public:
test()
{
a=o;
cout<<a;
}
test(int a)
{
a=a;
cout<<a;
}
test(test &t) //copy constructor
{
a=t.a;
}
};
void main()
{
test t;
test t(5);
test t1=t; //is this a valid code
} can a parameterized constructor can be assigned to and newly created object
how to do it
Please use the 'Insert Code' button, and put your fomatted code betweeen the "[ code ]" and "[ / code ]" tags to make it easier for everyone to read:-)
You need to make a couple of changes on thsi one.
The basic constructor
1 2 3 4 5
test(int a)
{
a=a;
cout<<a;
}
needs to have a different parameter name.
Currently it does NOT set the private variable a as the parameter a is considered more local in scope.
Change to
1 2 3 4 5
test(int b)
{
a=b;
cout<<a;
}
and it works fine.
Note that this sort of error, where a parameter name overrides an atribute name, can be very difficult to find in a large project.
Secondly you need to change the varaible name for the test of the parameterised constructor as you alreadsy have a vaiable t.
try
1 2 3 4 5 6
void main()
{
test t;
test t2(5);
test t1=t2;
}
I have added an overload of the operator << to allow you to output the value of a in an instance of test so you can see it all working.
>> The basic constructor 1 [...] needs to have a different parameter name.
Although a good point, it's not entirely true. There are two ways to fix this without changing the parameter name:
1. With an initialize list:
1 2
test(int a) : a(a) // Initialize the member variable a with the value of the parameter a
{}
2. Explicitly setting the member variable using 'this':
1 2 3 4
test(int a)
{
this->a = a; // Assign the value of the parameter a to the member variable a
}
BTW: I don't think 'b' was a good suggestion for the parameter containing the initial value of 'a'. The name should reflect the name of the member variable:
1 2 3
test(int _a) // It's common to use an underscore
test(int initial_a)
test(int a_value)
Yes, you can do extra work to get arround it, but it's simpler just to use a different parameter name!
'b' vs '_a', etc.
Agreed - but then this is logically extended to avoid 'a' as an attribute name in the first place...
The point was to have something simple to demonstrate the change required:-)