Question about classes and public/private

Hello, so I picked up c++ a few days ago after doing java for 2 years in IB and I have a quick question. So, I have a general understanding of how classes work in c++ (at least from a syntax point of view), however, I'm a bit confused.
Is the main()function in a class? For example one of the small tutorial things I ended doing was:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int calculator()
{
	int a = 4;
	int b = 21;
	int sum = a + b; //uses arithmatic functions to do operations on variables stored data type integer

	cout <<"Sum: " + sum<< endl; //prints "Sum: " and the value of the variable sum

	return 0;
}

int main()
{
	int apples = 7; //variable apples type integer value of 7
	cout << apples << endl;

	calculator(); //calls the function 'calculator'

	return 0;
}


is my main() function in a class? Also, because calculator() function is not specified as public or private, is it assumed to be public? This is a wee bit confusing coming from Java where everything is specified hehe.

Thanks for any help you can throw my way, sorry for the trivial questions.
Last edited on
main and calculator are general purpose functions. They are not member functions of a class. You code has no any class defined except of course std::cout.
Last edited on
This is probably a fairly reasonable approximation how a class would relate to your example

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

using namespace std;

class   calculator {
    public:
        int Add (int A = 4, int B = 21);
    };

int calculator::Add ( int A, int B ) {
    return A + B;
    }
    
int main ( ) {
    calculator Calc;
    
    cout << "Sum: " << Calc.Add () << endl;
    cout << Calc.Add (5) << endl;
    cout << Calc.Add (11, 32) << endl;
    return 0;
    }


Line 7: Is the functions declaration with default parameters
Lines 18 - 19 are how they could be used.

I just put those in, to give you an example of how flexible a class can be designed.
C++ is different from Java in that the C++ entry point is always the function
int main()
or
int main(int argc, char* argv[]

The main() function is not within a class. C++ does not require everything to be in classes like Java does.
closed account (zb0S216C)
colourfulbananas wrote:
"Is the main() function in a class?"

No. If you're comfortable with classes, then you should know full well main() is not a member of a class.

But why is main() not in a class? Because C++ inherited main() from C. Because C had no classes, main() had to be a standalone function. But what about structures? In C, structures could only have pointers to functions, not actual functions.

colourfulbananas wrote:
"Also, because calculator() function is not specified as public or private, is it assumed to be public?"

Access specifiers are non-existent outside a class. Since main() is not a member of a class, it's neither public, private, nor protected.

If the transition from Java to C++ is too difficult, try the fruity C#. I believe the syntax is much like Java.

Wazzak
Last edited on
Thanks for the help everyone :) I'm slowly getting the hang of it. Quick last question though, so I've gotten to pointers and I'm slowly grasping that, however in Java I was taught that there were non primitive and primitive data types and that determined if the variable was passed by reference or value. But with a pointer can you pass any type of variable by reference? (I should probably stop comparing things to how they work in Java soon...)

Thank you very much for the help. :)
Having pointer you have access to the variable it points to. So you may change the variable if only the pointer is not defined such a way that it points to a const variable.

For example

1
2
3
4
5
int x = 10;
int *p = &x;
*p = 20;
const int *cp = &x;
*cp = 30;   // error: can not change a const variable 
closed account (zb0S216C)
colourfulbananas wrote:
"But with a pointer can you pass any type of variable by reference?"

With a void pointer, yes, you can. However, if a pointer is of type float, it can only point to a float and nothing else unless it's converted. Of course, since void allows you to point to anything, the compiler will not allow you to write to the object it points to unless you tell the compiler what it's pointing to. For example:

1
2
3
4
5
void SomeFunction(void *Data)
{
    *Data = 10;  // A
    *(static_cast<int*>(Data)) = 10; // B
}

A) This is an error. Since Data can be pointing to anything, the compiler will not know what it's pointing to. Thus, the compiler will be clueless as to how the 10 should be stored in Data. This brings use to B.

B) Here, we're using static_cast to convert Data to a pointer to an int. This informs the compiler how it should store the 10 in Data. Note that casting creates another pointer to the same location as the pointer you gave it. In this case, it returns a pointer to an int which points to the address pointed-to by Data. Casting, however, does not affect the pointer given to it.

Wazzak
Here's a good article describing pointers in detail.

http://cplusplus.com/doc/tutorial/pointers/

Pointers and how to handle non-primitive data types is frequently one of the biggest obstacles to jumping between Java and C++.
in Java I was taught that there were non primitive and primitive data types and that determined if the variable was passed by reference or value

Java can't pass anything by reference: in Java you're passing objects of primitive type and pointers to objects of non-primitive type, both by value. (Java calls its pointers "object references"). As a mental exercise, consider the C++ function std::swap() and how it could be done in Java.

C++ allows actual pass-by-reference, and you shouldn't even bring up pointers for this.

Other surprises to be prepared for:
The keyword new is not how (most) objects are created.
Non-member functions are preferred over member functions in class design.

Last edited on
Non-member functions are preferred over member functions in class design.


Could you explain what you mean by this?
@doug4

Meyers "When you think encapsulation, you should think non-member functions.": http://www.drdobbs.com/cpp/184401197

Sutter: "Where possible, prefer writing functions as nonmember nonfriends." http://www.gotw.ca/gotw/084.htm

And of course Sutter/Alexandrescu's 101 Coding Standards item 44

Granted, it should be obvious, given how C++ does name lookup, but since about ten years ago, it became common notion.
Cubbi,

Thanks for the links. My formal training in C++ is over 10 years old, so I never learned this. It makes sense.

I'll have to see where to apply this. I had OO and member functions beaten into me, and while I see the benefit of non-member functions, it's may be hard to break away from what I was taught.

Thanks for the lesson.
Topic archived. No new replies allowed.