Hi! First C++ Program. Trying to get it to work.

Hi guys. This is my first c++ homework assignment. I am familiar with c, but am having a hard time getting this to run:

#include <iostream>
#include <string>
use namespace std;




class Employee
{
public:
string GetFirst();
void SetFirst(string firstName);

string GetLast();
void SetLast(string lastName);

int GetSalary();
void SetSalary(int salary);
private:

string firstName;

string lastName;

int salary;

};

void main()
{
Employee Me;

Me.SetFirst( "David" );

Me.SetLast ( "Stark" );

Me.SetSalary ( 85000 );


cout << "\nMy name is: " << Me.firstname;

cout << "\nMy last name is: "<< Me.lastname;

cout << "\nMy salary is: "<< Me.salary;
}






It comes out with all sorts of errors. I just want it to print.

David
Stark
85000
...

Thanks a lot
I think you meant using and not use near the top there.
Also, you declare Get functions for your class, but then you call non-existant Set functions? And then instead of using the Get functions, you try to access the data members you explicitly declared private? Also, the Get functions are not defined, you just declared them.
Last edited on
Hmmm. Need to hit the book more. Example of how to define a get function? I was using an iffy example to write this. I'll look at other ones.
You define a function the same way as always. But in this case you may want to consider whether the get functions should return a const reference (so it cannot be modified outside of the class) or a normal reference (so you can modify it outside of the class). If you want to be able to modify it outside of the class, then you should simply make the variables public. Otherwise, getter and setter functions are needed.
Topic archived. No new replies allowed.