hi friends, I am new to c++, just learning.
i want the program to access the private member of the class, and have used set and get, and validate when user input. but the program below is not working.
i cant compile it.
can anyone help me, thanks friends.
#include<iostream>
#include<string>
usingnamespace std;
class students
{
private :
string name;
string ID;
int age;
public :
string setName(string temp)
{
name = temp;
}
string getName()
{
return name;
}
void setID(int temp)
{
if(temp < 1)
{
cout << " You have enter an invalid ID number ";
exit(1);
}
else
ID = temp;
}
string getID()
{
return ID;
}
void setAge(int temp)
{
if (temp < 1)
{
cout<< " invalid age type"<<"program terminatingt";
exit(1);
}
elseif (temp > 150)
{
cout <<" you are not human"<< "program terminating";
exit(1);
}
else
age = temp;
}
int getAge()
{
return age;
}
};
int main()
{
students st;
cout <<" Enter your name : ";
cin >>st.setName();
cout<< "Enter your ID :";
cin >>st.setID();
cout<< " Enter your age :";
cin >>st.setAge();
}
When returning something (get method) you have a return type. When you're setting a value (set method) you don't return anything back.
So just like blackcoder said you needed to have your "setName" method to return void (void means 'returns nothing') instead of returning a string like the "getName" method does.