Need classes help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Undermine
    New Member
    • Oct 2008
    • 6

    Need classes help.

    This is my header file.

    [PHP]#include <string>
    using std::string;

    class Employee
    {
    public:
    Employee(string ,string,int) ;
    void setFirstName( string );
    string getFirstName();
    void setLastName( string );
    string getLastName();
    void setMonthlySalar y(int);
    int getMonthlySalar y();
    private:
    string Fname;
    string Lname;
    int Salary;

    };[/PHP]

    Below is my body file:

    [PHP]#include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    #include "employ.h"


    Employee::Emplo yee(string str, string obc, int range )
    {

    setFirstName(st r);
    setLastName(obc );
    setMonthlySalar y(range);
    }

    void Employee::setFi rstName(string str)
    {
    Fname= str;

    cout <<" Employee's first name is:" << str << endl;
    }

    // string Employee::getFi rstName()
    // {
    // return Fname;
    // }




    void Employee::setLa stName(string obc)
    {
    Lname= obc;

    cout<<" Employee's last name is:" << obc <<endl;
    }

    //string Employee::getLa stName()
    //{
    // return Lname;
    // }

    // int Employee::getMo nthlySalary()
    // {
    // return Salary;
    // }

    void Employee::setMo nthlySalary(int )
    {
    // range= Salary;
    // if ( Salary < 0)
    // Salary = 0;
    // else
    // {
    // Salary=Salary;

    cout<<"::::"<< Lname << ":" << Fname << " monthly salary is " << Salary << endl;
    // }
    } [/PHP]


    and finally my main file
    [PHP]
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;


    #include "employ.h"

    int main()
    {

    Employee employ1(" Mike","Madox",7 00);
    Employee employ2("Steven ","Stevenson",8 00);


    char holdscr;

    std::cout << "\n\n\t*** Enter a character and press return to exit the program! ***\n\n\t"<< std::endl;

    std::cin >> holdscr;

    return 0;
    }[/PHP]



    First of, I have a couple questions. Why when i un comment the comment lines in my body does it have absolutely no effect on the outcome of the file. When i execute this file i get the correct, last name, first name, but for the int it gives me some crazy #'s and does not carry over what i typed in my main.

    How can i fix this?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Your main doesn't use that getter at all so uncommenting it has no effect.
    You are getting garbage for the int value because you have commented out the parts that set it's value in the method that is called from the constructor.

    Comment

    Working...