classes i need help!!

This is a discussion on classes i need help!! within the C++ Programming forums, part of the General Programming Boards category; i am learning c++ but i have a problem with classes; Code: 1: // Demonstrates declaration of a class and ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    classes i need help!!

    i am learning c++ but i have a problem with classes;
    Code:
    1:   // Demonstrates declaration of a class and
    2:   // definition of class methods,3:
    4:   #include <iostream.h>      // for cout5:
    6:   class Cat                   // begin declaration of the class7:   {
    8:     public:                   // begin public section
    9:       int GetAge();           // accessor function
    10:      void SetAge (int age);  // accessor function
    11:      void Meow();            // general function
    12:    private:                  // begin private section
    13:      int itsAge;             // member variable
    14:  };
    15:
    16:  // GetAge, Public accessor function
    17:  // returns value of itsAge member
    18:  int Cat::GetAge()
    19:  {
    20:     return itsAge;
    21:  }
    22:
    23:  // definition of SetAge, public
    24:  // accessor function
    25:  // returns sets itsAge member
    26:  void Cat::SetAge(int age)
    27:  {
    28:     // set member variable its age to
    29:     // value passed in by parameter age
    30:     itsAge = age;
    31:  }
    32:
    33:  // definition of Meow method
    34:  // returns: void
    35:  // parameters: None
    36:  // action: Prints "meow" to screen
    37:  void Cat::Meow()
    38:  {
    39:     cout << "Meow.\n";
    40:  }
    41:
    42:  // create a cat, set its age, have it
    43:  // meow, tell us its age, then meow again.44:  int main()45:  {
    46:     Cat Frisky;
    47:     Frisky.SetAge(5);
    48:     Frisky.Meow();
    49:     cout << "Frisky is a cat who is " ;
    50:     cout << Frisky.GetAge() << " years old.\n";
    51:     Frisky.Meow();
    52;      return 0;53: }
    why do we use itsage in private and why dont we use it in public??
    Last edited by condorx; 06-28-2002 at 06:14 AM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You could put age in public if you wanted. The reason to make it private is to prevent other parts of code from messing around with it. If it was public, any code could change it. Making it private means only the class it's in can manipulate it, and your get/set functions control how that manipulation is done.
    Truth is a malleable commodity - Dick Cheney

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21