still confused with constructors and private data members

This is a discussion on still confused with constructors and private data members within the C++ Programming forums, part of the General Programming Boards category; Can you explain it to me, I've done the exercises but all I did was pattern it from the book ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    still confused with constructors and private data members

    Can you explain it to me, I've done the exercises but all I did was pattern it from the book and I only understood few of the concepts.And also "arguments"

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Concerning arguments, do you know how to write simple functions? Constructors (and member functions) use arguments in the same way.

    As for the rest, you might show some code and ask more specifically what you don't understand.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3

    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, 03: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, 06:18 AM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM
  5. Value of data members
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 06-14-2002, 10:20 AM

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