user3479130
user3479130

Reputation: 11

Classes with private member .. What is wrong with this code?

I am new into classes, and I have been trying to create this simple class code but every time I get an error. It works fine when I don't use the access specifier private, but I want to practice how to use private. Could you please tell me what's wrong?

#include <iostream>
#include <string>

using namespace std;

class Student
{
private:
    string name;
    int ID;
public:
    void setName(string);
    string getName();

    void setID(int);
    int getID();
};

void Student::setName(string n)
{
    name=n;
}

string Student::getName()
{
    cout<<name;
    return name;
}

void Student::setID(int i)
{
    ID=i;
}

int Student::getID()
{
    cout<<ID;
    return ID;
}

int main ()
{
    Student S;

    cout<<"Enter Student name: ";
    cin>>S.name;
    cout<<"Enter students ID: ";
    cin>>S.ID;

    cout<<"The student's name is "<< S.name<<endl;
    cout<<"The student's ID is "<< S.ID<<endl;

    return 0;
}

Upvotes: 1

Views: 478

Answers (3)

rdavb
rdavb

Reputation: 64

What's wrong : You are accessing private members without using a class member function (outside of the class scope).

Private members are useful when you want to protect a value from uncontrolled access. Like when the modification of a value must undergo a certain verification (which would be implemented in class functions).

In your code, you made sure name and ID are private, which means both can only be accessed using class functions (like the constructor or the getter and setter).

If you wanted, you could create a class named classroom (which would contain many students, stored in a vector).

In that class, you could make sure than when a student is added, it's ID is automatically generated and does not equal any other ID. In that case, it would be important to put the student vector private since it would require some sort of validation.

    class Student
{
private: // anything that wants to access members below
        //  this must be defined as a class member, or the equivalent
    string name; 
    int ID;
public:
    void setName(string); // can access private members
    string getName(); // can access private members.... should be const

    void setID(int); // can access private members
    int getID(); // can access private members, should be const
};

Upvotes: 0

Rerito
Rerito

Reputation: 6086

In your main function you are trying to access the name and IDmember of your class. Which are private... Since you are outside the scope of the class Student, the compiler shouts at you.

You should do this (as you've implemented setters and getters):

int ID(0);
std::string name;
std::cin >> name;
S.setName(name);
std::cin >> ID;
S.setID(ID);

Upvotes: 2

Fares M.
Fares M.

Reputation: 1538

You have to access your private fields using setters/getters to set or retrieve their values, you can't use them with the class dot notation because they're private and you can access to them only using public methods

Upvotes: 0

Related Questions