Thread: still confused with constructors and private data members

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Construction is a form of initialization, they help create objects of a class. It's actually a special function within the class.

    Private data members make up the inner workings of a class. For example (cliché, i know), let's say you wanted to make a class that models a car. A car must have an engine in order to work. However, regular drivers shouldn't have to know how the engine works in order to operate the car. All you have to do is put the key in the ignition and start the car. This design would like so:
    Code:
    class Engine;
    
    class Car
    {
       private:
          Engine engine; // This is a private engine...
                         // Only the car itself knows about the greasy inner workings of the engine.
       public:
          void ignition_start() // This function is public... this is how people start the car.
          {
              engine.turnOn(); // They DON'T have to pop the hood and start the engine manually!
          }
    };
    The public members of a class make up what is sometimes called the public interface of that class, because those members are what the outside world uses in order to operate that class.

    In essence, private members can only be seen and used from within the class itself.
    Last edited by rudyman; 07-26-2008 at 09:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about constructors
    By veronicak5678 in forum C++ Programming
    Replies: 19
    Last Post: 03-05-2008, 04:04 PM
  2. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  5. Value of data members
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2002, 10:20 AM