Thread: OK. new easy question for you guys.

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    25

    OK. new easy question for you guys.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    class Employee//Class called employee
    {
    int Age;
    int YearsOfService;
    int Salary;
    public:
    Employee(): Age(18) ,YearsOfService(1) ,Salary(10000){} // Trying to make default values
    Employee(int initialAge, int initialYearsOfService, int initialSalary); // constructor
    ~Employee(); // destructor
    void SetAge(int age) {ItsAge = age; }
    void SetYearsOfService(int YearsOfService) {ItsYearsOfService = YearsOfService; }
    void SetSalary(int Salary) {ItsSalary = Salary; }
    
    int GetAge() const { return ItsAge; }
    int GetYearsOfService() const { return ItsYearsOfService ; }
    int GetSalary() const { return ItsSalary; }
    
    private:
    int ItsAge;
    int ItsYearsOfService;
    int ItsSalary;
    };
    
    // Function definitions.
    Employee::Employee (int initialAge,int initialYearsOfService,int initialSalary)
    { 
    ItsAge = initialAge;
    ItsYearsOfService = initialYearsOfService;
    ItsSalary = initialSalary;
    }
    
    Employee::~Employee() // takes no action
    {
    
    }
    
    
    int main()
    {
    Employee JohnFKennedy(36,4,2000);
    Employee Tom;
    
    std::cout<<"\nJohn F. Kennedy's Age is: "<<JohnFKennedy.GetAge();
    std::cout<<"\nJohn F. Kennedy's sallary is: $"<<JohnFKennedy.GetSalary();
    std::cout<<"\nJohn F. Kennedy's Years of service is: "<<JohnFKennedy.GetYearsOfService();
    std::cout<<"\nTom's Age is: "<<Tom.GetAge();
    std::cout<<"\nTom's sallary is: $"<<Tom.GetSalary();
    std::cout<<"\nToms's Years of service is: "<< Tom.GetYearsOfService();
    std::cout<<"\n";
    system("pause");
    return 0;
    }
    OK. The problem. when i create a new object, i want the values to be default. But i get this random number instead. what am i doing wrong?
    Last edited by arnis; 07-17-2003 at 06:47 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    std::cout<<"\nTom's Age is: "<<JohnFKennedy.GetAge();
    std::cout<<"\nTom's sallary is: $"<<JohnFKennedy.GetSalary();
    std::cout<<"\nToms's Years of service is: "<< Tom.GetYearsOfService();
    Ah the joys of copy and paste! Look at what variable you're using there. The first two (age and sallary) are not Tom's.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    25
    haha. My mistake. Ive corrected that. But i still get this random numbers.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    class Employee//Class called employee
    {
    public:
    	// constructor
     	// WRONG: Employee(): Age(18) ,YearsOfService(1) ,Salary(10000){} - INSTEAD:
    	Employee(int initialAge = 18, int initialYearsOfService = 1, int initialSalary = 10000);
    ...
    };
    
    Employee::Employee(int initialAge,int initialYearsOfService,int initialSalary)
    	: ItsAge(initialAge), ItsYearsOfService(initialYearsOfService), ItsSalary(initialSalary)
    { }
    I think that is what you want - an initialization list to initialize your values, and default values for when they are not specified. If you wanted two separate constructors you could also use this -- notice the use of the actual member variables (i.e. ItsAge instead of Age) in the initialization list:
    Code:
    Employee::Employee() : ItsAge(18), ItsYearsOfService(1), ItsSalary(10000)
    { }

  5. #5
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Code:
    Employee(int initialAge=18, int initialYearsOfService=1, int initialSalary=10000):Age(initialAge) ,
                                     YearsOfService(initialYearsOfService) ,
                                     Salary(initialSalary){}
    I think this is what you want to do or you can do it this way as well:
    Code:
    Employee(int initialAge=18, int initialYearsOfService=1, int initialSalary=10000)
    {
        Age = initialAge;
        YearsofService = initialYearsOfService;
        Salary = initialSalary;
    }
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  6. #6
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    Looks like jlou beat me to post lol... guess I took to long typing out reply
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    25
    thanks. Im going to bed now. will test it tommorow

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Dohojar
    I think this is what you want
    Originally posted by jlou
    I think that is what you want
    Scary!

  9. #9
    Registered User Dohojar's Avatar
    Join Date
    Feb 2002
    Posts
    115
    *shudders* that is scary. Your reply wasn't even posted yet when I started mine. Too funny
    Dohojar Moajbuj
    Time is the greatest teacher, too bad it kills all its students

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 01-02-2006, 04:46 PM
  2. This is hopefully an extremely easy question...
    By rachaelvictoria in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 01:36 AM
  3. 4 easy question
    By Zeratulsdomain in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2005, 10:43 PM
  4. easy newbie question
    By ioaz in forum C++ Programming
    Replies: 7
    Last Post: 09-24-2003, 03:58 PM
  5. I have a 2 easy question about some homework?
    By correlcj in forum C Programming
    Replies: 3
    Last Post: 07-18-2002, 07:28 PM