#include <iostream>
usingnamespace std;
class Employee
{
private:
string ename;
double esalary;
public:
Employee(string nm = "", double sal = 0.0)
{
ename = nm;
esalary = sal;
}
string getName()
{ return ename;}
double getSalary()
{ return esalary;}
};
Employee read_employee();
int main()
{
read_employee();
cout << r.getName << endl; // this does not work, obviously
cout << r.getSalary << endl; // this does not work, obviously
return 0;
}
Employee read_employee()
{
string name;
cout << "Please enter the name: ";
getline(cin, name);
double salary;
cout << "Please enter the salary: ";
cin >> salary;
Employee r(name, salary);
return r;
}
I want to be able to call the read_employee function from main, and then print out the values of the member variables FROM MAIN. I'll need the object to access the member variables, so how can I use the object returned from read_employee() in main?
To use an object returned from a function, you have to not ignore it. ;)
1 2 3 4 5 6 7 8 9
int main()
{
Employee r = read_employee();
cout << r.getName() << endl; // this does not work, obviously
cout << r.getSalary() << endl; // this does not work, obviously
return 0;
}
koothkeeper wrote:
If you make read_employee() return a reference, you can:
@cire: Wow, I feel really stupid with this one, lol. I was missing the open-close parenthesis, but the this whole time I thought the assignment statement was not working. Ugh...