question about class

I am having a bit of troubles fully understanding classes. To my understanding this is what the general structure of a class looks like.

1
2
3
4
5
6
class example
{
   \\things that are only available to the class
   public:
      \\things that are available everywhere
}


Is that structure correct? Also, what I truly don't fully understand can you put anything you want in there like this next example,

1
2
3
4
5
6
7
8
9
10
11
class addition
{
    int a; \\first number
    int b; \\second number
    public:
      cout << "Enter first number:";
      cin >> a;
      cout << "Enter Second number:";
      cin >> b;
      cout << "Your number is " << a + b;
}


I know you could do that another way but that is the only thing I could come up with on the top of my head. Of course earlier in the program I would enter using namespace std;. Would this be a legitimate class and the correct setup?
To my understanding this is what the general structure of a class looks like.

Yes, but there are 3 standard keywords for access restrictions in a class: private, protected and public, but any member of the class can be accessed by a friend class or function.

Look at this:
1
2
3
4
5
6
7
8
9
class{
//anything here is private. Inaccessible by derived classes and open code but assecible by friends
public:
//accessible by open code, derived classes and friends
protected:
//inaccessible by open code. accessible by friends and derived classes
private:
//everything here is private
};


For the second one, you can only keep variables and functons in a class. So you must do al those cout stuff in a member function.

Aceix.
Last edited on
Actually in a member function. Aceix was doing two things at once :)

Class isn't a function. You can, however, make class have functions:
[code]
class Addition
{
public:
void Add() {
int a,b;
cout << "Enter first number:";
cin >> a;
cout << "Enter Second number:";
cin >> b;
cout << "Your number is " << a + b;
}
};

Look up on classes, class member variables and class member functions.

Cheers!
Ok, I did some more reading about classes and now I am starting to understand it a bit more. I didn't know however that you could (in this example) define Add directly in the class. Would this be a good start to a basic area/perimeter of a square program and would it is this better than my first example (I am not using my existing code so there might be a couple of errors)

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

class square
{
    int width;
    int length;
    public:
        void area (int, int);
        void perimeter (int, int);
};

void square::area (int width, int length)
{
   cout << "Enter the width of the square:";
   cin >> width;
   cout << "Enter the length of the square:";
   cin >> length;
   cout << "The area of the square is " << length * width << endl;
}

void square::perimeter(int width, int length)
{
   cout << "Enter the length of the square:";
   cin >> length;
   cout << "The perimeter of the square is " << length * 4 << endl;
}

int main ()
{
   string choice;
   cout << "Welcome to an example of a class!\n";
   cout << "1.Area\n";
   cout << "2.Perimeter\n";
   cout << "Please enter your choice:";
   cin >> choice;
   if (choice == "area")
   {
      square::area();
   }
   if (choice == "perimeter")
   {
      square::perimeter();
   }
   return 0;
}


Also I would like criticism over my code style, I hear some people say they hate white space but is it just programmers preference or do I need to cut back on the amount of white space? Is it better to use "\n" or "endl"? I usually use "\n" when I am ending with a sentence, if I end it with an equation to display the answer I use "endl" as shown above. Are there any habits I could pick up that would make me a better programmer (the code you see above is how all my other code looks like)?
Whitespace is ok, nothing bad. I would still include variable names in prototypes of functions, these are sometimes useful when you are reading function. You can understand function better(of course not always, but still :)

First off - you have already width and length of your square. So why use some other variables? Simply do this:

1
2
3
4
5
6
7
8
void square::area ()
{
   cout << "Enter the width of the square:";
   cin >> width;
   cout << "Enter the length of the square:";
   cin >> length;
   cout << "The area of the square is " << length * width << endl;
}


Also, I would change this function if I were you:
 
   cout << "The area of the square is " << length * width << endl;


Because it makes no sense to ask for width and height every time you want to calculate area of same square. You could simply make another function:
1
2
3
4
5
void square::ChangeSize(int w, int h)
{
 width = w;
 height = h;
}


Ideally, you wouldn't even use cout, because you may want to use area to some calculations. So you could do something like this:
1
2
3
4
int square::area()
{
  return width*length;
}


Also, for correctness's sake - rectangle has same width and height. You are creating rectangle now :)

As for habits - I like Allman, so I won't comply :) However, instead of using using namespace std I would recommend putting std:: before every std's object/function. It's safer(no name collisions), and considered better habit.

Cheers!
I see where you are going with the void square::ChangeSize(int w, int h). The purpose of the program was to take what the user wants for the length and width to be and calculate the area/perimeter, that is why I had all the user inputs. As for the cout's that were in the voids, I usually create the general program then if I add other stuff to it I change the code accordingly. I need to start doing what you recommended with that part. I have another question:

1
2
3
4
int square::area()
{
   return width*length;
}


Would you still need the void square::area ()? In main would you then use the int square::area() like this in a cout to display the answer:

1
2
3
4
5
int main ()
{
   \\some other code
   std::cout << "The area is " << square::area() << std::endl;
}


Lastly, what would I need to change in cout << "The area of the square is " << length * width << endl;? I honestly see nothing wrong with that.
Every member function needs object that will call it.
1
2
3
square mySquare;
mySquare.ChangeSize(5, 5);
std::cout << "The area is " << mySquare.area() << std::endl;

It means that function will use data from mySquare.

There is also a way to call function like you showed, but it means calling static member functions - if you want you can read some tutorials, they will probably explain it better than I could.

Another thing is, that if you want to use class, you generally aim for creating some object which will hold some state. So, if you create square, it will have it's state(its width and height). You can change these, but square will always have its width and height.
If you only want to make some functions according to some general idea(like "I want to make functions that will do operations on rectangle, but each time user will provide rectangle's width and length"), it's better to use namespace:
1
2
3
4
5
6
7
//rectangle.hpp
#pragma once
namespace Rectangle
{
  int Area(int w, int h);
  int Perimeter(int w, int h);
}


1
2
3
4
5
6
7
8
9
10
11
//rectangle.cpp
#include "rectangle.hpp"
  int Rectangle::Area(int w, int h)
  {
     return w*h;
  }

  int Rectangle::Perimeter(int w, int h)
  {
     return w*2 + h*2;
  }
@MatthewRock

Thank you! You have solved my problems and questions! Now I just have one more, are classes used all over c++ like in windowed programming?
are classes used all over c++ like in windowed programming?


If you mean, while using WINAPI, then no. The windows API was written in C, which means it's mostly structs, macros and enumerated types.

There are several APIs that attempt to wrap the most essential aspects of the WINAPI in their own classes. Even Microsoft has their own (not open-source) library called CLR.
@xismn

Ok, that will do it! Thank you all!
Topic archived. No new replies allowed.