Assing value to object

Hi,
could anyone provide me solution(modfication) in below program for compiling code

#include<iosream>

using name std;

class test
{
public:
int data;

}

int main()
{
test A;
A=10; //if i want to write this line what will be the modification

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iosream>

using name std;

class test
{
public:
int data; // This should probably be private

}; // you forgot your semi-colon here

int main()
{
test A;
A=10; // 'A' is the object, what you want to do is assign one of the objects members a value
A.data = 10; // Use the dot operator

}


You should probably also use a setter function rather than manually assigning it the value.
You can overload a = operator to make the assignment work
1
2
3
4
5
6
7
// in class test

test &operator = ( int i )
{
   data = i;
   return *this;
}
could you explain little bit. it works fine for me but it will be great if you provide littlbe explain
about the code
test &operator = ( int i )
{
data = i;
return *this;
}
little bit clear for my previous question

test &operator = ( int i ) --> got this line
{
data = i; --> got this line
return *this; ---> could not understand proper way
}

for implementing A=10
You shall have a look at Operator Overloading.
You shall search about that subject , for learning it with details.
return *this; this is the pointer pointing to the current object, *this is the reference to the current object

You can find info on this and operator overloading in this tutorial: http://www.cplusplus.com/doc/tutorial/classes2/
hmm that's true anway thanks alll for quick help
Topic archived. No new replies allowed.