Private/Public Classes, explanation please in context

This is a discussion on Private/Public Classes, explanation please in context within the C++ Programming forums, part of the General Programming Boards category; I'm making a car sale program, and we are asked to have many different components in the calculation of the ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75

    Private/Public Classes, explanation please in context

    I'm making a car sale program, and we are asked to have many different components in the calculation of the final price. the public stuff make sense, but can someone explain to me when I should use the private variables and how I should use them and why I should use them. it doesn't really make sense, and we really wern't given an explanation. Thank you very much. Here's a snippet of my code to help you. Could someone give me an example of the cost function that is in the private class, and how I can get it from the user? Thanks again!!!

    Code:
    class Cars
    {
     public:
          string get_color();
          string get_make();
          int get_year();
          int number_of_speeds();
          double cost_car();
          void set(int the_year, string the_color, int the_speeds);
     private:
          int speeds;
          string color;
          string make;
          int cost;
    };

  2. #2
    Registered User
    Join Date
    Apr 2009
    Location
    01001101 01100001 01110010 01111001 01101100 01100001 01101110 01101110
    Posts
    3
    Private member variables are used for data that should only be accessed by member functions and not from outside code e.g. int main() code or other classes(unless said class is inherited). Basically to allow class calculations to be pure in a sense.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ...creepy
    Posts
    75
    can i have an example of what you mean in context. it would help me understand what you said better by looking at something too. Thanks!

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Basically, a class has two parts: public and private. Private members aren't meant to be accessed by anything else.

    For example, if your "speed" variable was public, you could change its value using car1.speed = something, assuming car1 is of class type "Car".

    Instead, you can just add a public function like "setSpeed" that changes the value of the private member "speed". If you try to access the private member from somewhere else, compiler will complain about attempt to acces private member of a class.

    Your main function doesn't need to know how the class works, or the name of the class variables. It just needs to know there's certain functions you can use to change those values (if that's what you want it to do).

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Quote Originally Posted by liquidxit2 View Post
    Private member variables are used for data that should only be accessed by member functions and not from outside code e.g. int main() code or other classes(unless said class is inherited). Basically to allow class calculations to be pure in a sense.
    This is wrong. inherited classes cannot access the private member variables of their base class.

    variables are made private to encapsulate the class. For instance, let's say you have the following class:
    Code:
    class Person
    {
    public:
        int age;
        string name;
    };
    The problem is that a user could do something like:
    Code:
    Person p;
    p.age = -1;  // Uh oh, this isn't good!
    We can fix the above code by doing something like:
    Code:
    class Person
    {
    public:
        void SetAge(int new_age)
        {
            if(new_age < 0)
            {
                cout << "ERROR: Invalid age being set.  Using zero instead\n";
                age = 0;
            }
            else
                age = new_age;
        }
    
        int GetAge() { return age; }
    private:
        int age;
        string name;
    };
    The above code doesn't give the user direct access to the member variables. This means that you can do things like validate input.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Basically, a class has two parts: public and private. Private members aren't meant to be accessed by anything else.
    Classes actually have 3 access modifiers: public, protected, and private.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Quote Originally Posted by bithub View Post
    Classes actually have 3 access modifiers: public, protected, and private.
    Didn't even remember when I was typing, my bad.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    147
    Good replies.

    What the students usually don't get at first is why.

    Encapsulation is the right point, and there are posts around here, I'm sure, that discuss this.

    There are a number of concepts students cover that may appear confusing, even unimportant, and some have concluded are obstacles instead of benefits. This is one of those subjects that appears, at first, to have little functional purpose. You must have patience for the meaning to come to you.

    It doesn't help that introductory examples trivialize these concepts, and many times fail to illustrate, or worse, tend to suggest that the concept is meaningless.

    A customer record is a good example of this.

    When a class represents a collection of simple data, like the contents of a record from a database, it may be that all of that data should be public. In C it may have been a struct. It's often hard to visualize, or even justify, protection of a name, address and phone number.

    Let me offer an example that might serve to illustrate how protection is well used.

    Code:
    template <typename V> class DirtyValue
    {
     private:
    
      bool  IsDirty;
      V  Data;
    
     public:
    
     DirtyValue() : IsDirty( false ) {}
    }

    I'm not fleshing this out. To a new student, templates may be unknown, and perhaps that's a twist I should avoid, but here we go anyway.

    Data is of type V, which can be any valid type. This class can be generated for any type of data, as in

    DirtyValue<double> x;
    DirtyValue<Vector3d> v;


    The idea here is you have a value for which you need to track changes. As I said, I've not fleshed out this class, but suffice it to say that a complete version of it allows statements like this:

    x = 5.0;
    v = Vector3d( 1.0, 2.0, 1.5 );

    double a = x;

    In this case, x looks and acts like a double, v looks and acts like a Vector3d. However, when I use it, I have several contexts of use. Read or write contexts.

    When I assigned x = 5.0, an operator function that performs the "=" here ALSO sets IsDirty to true.

    Now, if I need to know if the value of x was changed since the last time I used it, I can do this:

    Code:
    if ( !x.IsCurrent() ) 
      {
        // use x
        x.SetCurrent();
      }
    Again, using functions I've not fleshed out in the class above.

    SetCurrent will set "IsDirty" to false.

    In my own 'real' version of this, I have support for multiple "interests" - each "interest" uses it's own ID, and instead of a bool, IsDirty is a bit field (an unsigned integer).

    This means I can track "currency" by several consumers.

    I also know that ANY write action on a DirtyValue will automatically set IsDirty for me - every time.

    I also know that no one can screw that fact up for me!

    That's why it's private.

    I also know that no one can alter the Data without also managing IsDirty accordingly.


    That's what it's about.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    Quote Originally Posted by JVene View Post
    long post
    I think you've managed to complicate a very simple concept...

  10. #10
    Registered User
    Join Date
    Apr 2009
    Location
    01001101 01100001 01110010 01111001 01101100 01100001 01101110 01101110
    Posts
    3
    Quote Originally Posted by bithub View Post
    I think you've managed to complicate a very simple concept...
    Agreed. Oh and I stand corrected on the inherited class comment.

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, 11:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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