help me understand classes please

I understand what a class is but not how to use it. And I don't mean syntax.

Firstly, what is the purpose of public and private?

Secondly, what is the deal with constructor and deconstructer? Do you need it always and if only in special cases then what are those cases? Maybe I'm just stupid but it would seem that you construct a class when you declare it. What's the purpose?

Thirdly, what are the access parameters of private specifically? More to the point, is this next statement true and if so explain the second part. "Public holds data and private takes input." What does that mean "private takes input? Literally? and if so, then what....do you do with that input?
Idk, classes make sense but I always seem to fuck it up. And these are some things that I have never really understood. Help please.
Read tutorials from this site and others.
I have a book on the subject, I've read these tutorials. It is confusing, I feel it can be explained better by someone else. These are in light of the fact that I have studied for a while now.
public members can be used outside class through object call.But private members cannot be used outside class.They can't be called by function other than class ones.

constructors are used to initialise the class variable for a object(not for class) with values.destructors are used to delete the object.If you do not define them ,they are automatically created by compiler(default constructor).

"Public holds data and private takes input."


Its opposite of what in general we do.We generally declare variables in Private part which holds data and we define member function in public part which inputs data.
Thank you, it helps greatly but I need to mull it over briefly as I'm still a bit confused. Public and private are clear. Only one question:

What does "public takes input" mean exactly? Takes input from what? The private or the user?

Thanks again, its greatly appriciated. I hope you'll be patient with me.
public doesn't take input.

We generally define the function to take input in public: section.public do not take any input and private do not hold data.Its like sayings

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Hello
{
   private:    //no need to write private still.....
      int a,b,sum;
      char c;
      int summ(int x, int y)
      {
                 return x+y;
       }
   public:
       void getdata()
        {
                 cin>>a>>b;
                 sum=summ(a,b)
         }
};                      //a and c holds data while getdata get data from user 


I general programming its suggested to use variables and functions (that are not used outside) in private block while other function in public.
(Something will change when you will learn inheritence but leave that now)

EDIT: ofcourse input from user
Last edited on
~Don't use a "get" function to set data~

This is a long post, but that's what happens when all I have to do is enjoy my coffee :)

Private variables/functions (members) and constructors/destructors make your class much safer/easier to use.

Lets make a Rectangle class:
1
2
3
4
5
6
7
8
9
class Rectangle{
public:
  double width;
  double height;
  double area;

  Rectangle(void)
  { /* Nothing */ }
};


We have issues right away:
1
2
3
// main
Rectangle myRect;
cout << area;


Area (and width and height) is garbage so our cout is worthless. We can fix this in main
1
2
3
4
5
Rectangle myRect;
myRect.width = 0;
myRect.height = 0;
myRect.area = myRect.width * myRect.height;
cout << area;


But now we have to remember to do this every time we declare a Rectangle. A constructor will fix this. We can also overload the constructor, and make things a little quicker:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Rectangle{
public:
  double width;
  double height;
  double area;

  Rectangle(void)
  {
    width = 0;
    height = 0;
    area = width * height;
  }
  Rectangle(double w, double h)
  {
    width = w;
    height = h;
    area = width * height;
  }
};


Now this will make sense:
1
2
3
4
5
Rectangle myRect;
cout << myRect.area;

Rectangle anotherRect(4,5);
cout << anotherRect.area;


We still have a big problem with the fact that these are public variables:
1
2
3
Rectangle myRect(4,5);
myRect.area = 6;
cout << myRect.area;


So we make area private:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Rectangle{
private:
  double area;

public:
  double width;
  double height;

  Rectangle(void)
  { /* Same as before */ }
  Rectangle(double w, double h)
  { /* Same as before  */}

  // Now we need this:
  double getArea(void) { return area; }
};


We are still in trouble though (even more actually), because we can do this:
1
2
3
4
Rectangle myRect(4,5);
cout << myRect.getArea();
myRect.width = 10;
cout << myRect.getArea();


We don't want to be able to change the width or height without updating the area, so we make them private too:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Rectangle{
private:
  double area;
  double width;
  double height;

public:

  Rectangle(void)
  { /* Same as before */ }
  Rectangle(double w, double h)
  { /* Same as before  */}
  
  // Accessors
  double getArea(void) { return area; }
  double getWidth(void) { return width; }
  double getHeight(void) {return height; }

  // Mutators

  void setWidth(double w)
  {
    width = w;
    area = width * height;
  }
  void setHeight(double h)
  {
    height = h;
    area = width * height;
  }
};


Now there is no way that area is not the actual area of the rectangle. In fact, we don't need an "area" member at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Rectangle{
private:
  double width;
  double height;

public:

  Rectangle(void)
    : width(0), height(0)  // This is a better way than within the braces
  {}
  Rectangle(double w, double h)
    : width(w), height(h)
  {}
  
  // Accessors
  double getArea(void) { return width * height; }
  double getWidth(void) { return width; }
  double getHeight(void) {return height; }

  // Mutators

  void setWidth(double w)  { width = w; }
  void setHeight(double h)  { height = h; }
};


Now we can do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main(void)
  Rectangle myRect;
  for (int i = 0; i < 10; i++)
  {
    myRect.setWdith(i);
    for (int j = 0; j < 10; j++)
    {
      myRect.setHeight(j);
      cout << myRect.getWidth() << " * " << myRect.getHeight() << " = " myRect.getArea() << endl;
    }
  }
  return 0;
}

What's up with this whole "public gets data, private holds data" deal? This just sounds confusing. Public means it can be accessed by an object created outside of the class file. Private means only the class itself can access it, ie class functions can call private functions or data members.

A general rule of thumb is to have your data members set as private, and have public functions that can alter them. This isn't always the case, sometimes it makes sense to have data members be public, you just gotta play with it. Just go and actually make some classes and see what you can do.
thank you sir. No fancy run around, straight to the point. The statement is one I've heard over and over to help solidify in one sentence what public and private should be responsible for. As for practice, I've tried and failed over and over which leads me to believe that I don't understand what I'm doing or trying to do. The only book I have tries to relate OO to nachos and classes to microwave ovens (I think, its very confusing). The tuts here are over stated and long and might as well speak another language (to me anyway, you may feel different). So many different names and concepts keep confusing me and I'm only trying to understand three things. Private and public, how to use them properly in a program and relation to OO and the class itself. Which has been stated well here by the way. Second, constructors, deconstructers and what they should be used for and also when they are neccesary. I hear you need them, I hear you dont (default constructor?) And everything in between. Do you need them or don't you? My third has also been answered. Either way books and others contradict each other and it has confused me about everything on this topic. I thank you again for you straight foward response.
Do not look for comparisions.Just understand what the deal is.And you will be good with classes.Classes are important aspect of C++.
Topic archived. No new replies allowed.