Reputation: 1389
i am trying to understand how can i access private classes through public classes because some experts said to me that i have to use only private classes. But i can't understand why this doesn't work.I really don't know how can i access private through public its really confusing .
#include <iostream>
#include <string>
using namespace std;
class ManolisClass{
public :
void setName(string x){
name = x;
}
string getName(){
return name;
}
private :
string name;
};
int main()
{
ManolisClass bo;
getline(cin, bo.setName() );
cout << bo.getName();
return 0;
}
Upvotes: 0
Views: 127
Reputation: 159
Your access methods are correct, but as you can see from the signature of the function setName, you have to provide a string to set the name of the class. getLine
method takes a string as argument. You could create an intermediate variable and use that variable to set the name of the class.
Here is how one can do it.
string temp;
getline(cin, temp);
bo.setName(temp);
Upvotes: 1