Classes or data structures?

Hey Guys,

In which situation do i have to use classes? And data structures? I'm a beginner, and i really don't see the difference between them. I mean the output is the same.

It's like:

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
#include "stdafx.h"
#include <iostream>

using namespace std;

struct first
{
	int one;
	int two;
} number1;

class second
{
public:
	int three;
	int four;
};

int main()
{
	number1.one=1;
	number1.two=2;
	second number2;
	number2.three=3;
	number2.four=4;
	cout << number1.one << "\n";
	cout << number1.two << "\n";
	cout << number2.three << "\n";
	cout <<number2.four << "\n";
	return 0;
}


And an other problem:

It doesn't work with strings. If i set a string value for example for two (in the struct) or for four (in the class), i can't write the value. I know how i set it to a string (for example: string two;), but it doesn't work.

Thanks.
Last edited on
closed account (1yR4jE8b)
Well, in a Structure, all members are public by default whereas in a Class they are private(which is why you needed to put the public label there).

Otherwise, they are pretty much the same.
The only difference between a class and a struct is the default member visibility and inheritance type (private for classes and public for structs).

And an other problem:

Did you #include <string> ?
darkestfright: Thank you!

R0mai: I'm such an idiot, i didn't include :) Thank you!
Topic archived. No new replies allowed.