Sams Teach Yourself C++ in 21 Days

(singke) #1
Understanding Object-Oriented Programming 173

6


Exercises ........................................................................................................



  1. Write the code that declares a class called Employeewith these data members:
    itsAge,itsYearsOfService, and itsSalary.

  2. Rewrite the Employeeclass declaration to make the data members private, and pro-
    vide public accessor methods to get and set each of the data members.

  3. Write a program with the Employeeclass that makes two employees; sets their
    itsAge,itsYearsOfService, and itsSalary; and prints their values. You’ll need
    to add the code for the accessor methods as well.

  4. Continuing from Exercise 3, write the code for a method of Employeethat reports
    how many thousands of dollars the employee earns, rounded to the nearest 1,000.

  5. Change the Employeeclass so that you can initialize itsAge,itsYearsOfService,
    and itsSalarywhen you create the employee.

  6. BUG BUSTERS:What is wrong with the following declaration?
    class Square
    {
    public:
    int Side;
    }
    7.BUG BUSTERS:Why isn’t the following class declaration very useful?
    class Cat
    {
    int GetAge() const;
    private:
    int itsAge;
    };
    8.BUG BUSTERS:What three bugs in this code should the compiler find?
    class TV
    {
    public:
    void SetStation(int Station);
    int GetStation() const;
    private:
    int itsStation;
    };


int main()
{
TV myTV;
myTV.itsStation = 9;
TV.SetStation(10);
TV myOtherTv(2);
}
Free download pdf