
Originally Posted by
hayloiuy
Can someone kindly explain what is the problem
What does this line do?
It creates a temporary variable of type "sample". Temporary variables are destroyed as soon as the statement is executed, therefore nothing has happened.
You create a member variable.
Code:
#include <iostream>
#include <string>
using namespace std;
class Sam
{
struct sample{
int a;
string b;
};
sample mySampleA, mySampleB;
public:
void set_a();
void set_b();
int ret_a();
string ret_b();
};
void Sam::set_a()
{
int a1;
cout<<"Insert int a";
cin>>a1;
mySampleA.a=a1;
}
int Sam::ret_a()
{
return mySampleA.a;
}
Regards,
Paul McKenzie
Bookmarks