"asdf" <li*********@gmail.comwrote in message
news:11********************@79g2000cws.googlegroup s.com...
May I change the value of data members of an object by using the
constructor?
No. A constructor is only invoked at creation time.
It is used to initialize an object.
struct A
{
int x;
default y;
'default' is a reserved word in C++. You can't use
it as a type or a name.
}
int main()
{
A aobj;
}
aobj is an object of the class A, and I want to change the values of x
and y of the aobj, how can I do? Thanks.
struct A
{
int x;
int y;
A(int x_parm, int y_parm) : x(x_parm), y(y_parm) /* constructor */
{
}
int main()
{
A obj(1, 2); /* creates a type 'A' object whose member 'x'
is initialized to 1, and whose member 'y'
is initialized to 2 */
obj.x = 42; /* changes member 'x' */
obj.y = 99; /* changes member 'y' */
return 0;
}
-Mike