Manuel Pap
Manuel Pap

Reputation: 1389

Classes, Privates through Publics

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

Answers (1)

mkirci
mkirci

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

Related Questions