I am learnign c++ & I am confused as why or when I would use a struct, class or enumeration

In laymans terms how would you describe what a Struct is, what a Class is & what a enumeration is? When I say what are they I mean what are they used for?

Would it be correct to say that a struct is a group of variables of differents type?

And a Class is a group of functions of a different type?

What is the usefulness of these...things? For example, I would say the usefulness of a bool function would be when I am testing whether an outcome is true or false & a double function is useful when I need to return a value that may sometimes be a whole number & sometimes a fractional.

So in what real life scenarios are structs, Classes & enumerations used?

In C++ you could say that a struct is also a sort of class, but when someone uses the struct keyword, he's often just referring to a structure as (POD = Plain Old Datatype), POD means that it's used as they did before in C: a structure is used to group (related) variables (they don't necessarily have to be of a different type)


struct book
  char author[20];
  char title[30];
  double price;

A class is used to encapsulate data, it allows you to implement Object Oriented design into your program, a class can have three access specifiers: public, private and protected: In a class, all data is private by default, this means that you cannot access it directly and that you'll have to implement a method (= another name used for 'function' when we're talking about classes) if you want to make your data accessible.
public on the other hand means that you allow your data to be accessible, and protected means that your data is only accessible for methods of the class or for inherited classes from that class.
Remark: In C++ a structure is also a class (but in a structure is all data public by default)


class fish
// everything here is private
  void swim() { cout << "Swimming..." << endl; }
  void eat() {cout << "Eating..." << endl; }

An enumeration is what the name says: an enumeration (of integer constants)


enum animal {DOG, CAT, FISH, HORSE, BIRD};

animal a = DOG;

if( a == DOG )
    cout << "The animal is a dog." << endl;

Thanks :)

What would be some real world scenarios for using each of them? I gather in Classes you may use it in storing/dealing with passwords? Why would we need to make something private within a program? Is it so the same variable can be used for different objects.


class animals
  void swim() { cout << "Swimming..." << endl; }
  void eat() {cout << "Eating..." << endl; }
  void attributes(int, string, string);

  int age;
  string owner;
  string breed;

int main()

    animals dog;
    animals cat;



C++ structs are not just the same as C structs
I hope you understand access modifiers like public , private etc..
C++ structs are exactly the same as C++ classes, in all but one respect. If I declare members in a C++ struct, like :

struct Foo
int bar;
char* baz;
- then the members of the struct, in this case bar and baz, will be given public visibility by default, which means that the following function:

Foo makeFoo()
Foo temp;
temp.bar = 1;
temp.baz = "test";
return temp;
- would compile just fine. However, if I instead defined a class, like so:

class Foo
int bar;
char* baz;
- then now my function won't compile, because the members of the class are given private visibility by default; I'll get errors like

`int Foo::bar' is private

`char*Foo::baz' is private
as usual.

instead of typing :
#define SPRING 0
#define SUMMER 1
#define FALL 2
#define WINTER 3

you can just use Enum as :

now you can use the the same way you use all the costants with preprocessor directive #define as shown above ..

Classes are often used in very large projects, it's another approach of programming.
Classes are also commonly used to implement new datatypes.

Hey crash1989, could you please post using code tags?

Hey Gretty, your program containing that class won't compile for these reasons:

  • void attributes(int, string, string); -> where is the code for that function?
  • dog.attributes(2,jim,kkjkjk); ,
    you probably meant: dog.attributes(2,[B]"[/B]jim[B]"[/B],[B]"[/B]kkjkjk[B]"[/B]); -> notice the quotes.

    If you write it without quotes, then the compiler assumes that jim and kkjkjk are variables/instances of the string class.
    But I'm pretty much sure that you meant to wrap it between quotes :)

>Why would we need to make something private within a program?

Well, that's just the principal of data encapsulation: It's better that you let the object manage his own data members (in this context: variables), object oriented programming follows the concept of: "divide and conquer", you don't have to remember what all variables in a class stand for, you don't have to remember what all those help-functions do, you only have to remember the interface of your object.
It's better that all those complex things are out of your eyesight, but declaring a data member inside the private section of your object also ensures that your program (or a function) for example cannot accidentally change a variable's value. (It helps you to introduce less bugs in your program)

The interface of an object should define an easy way to let your program communicate with the object.

One important thing you should keep in mind while designing a class is: when there's no need to be able to access the data members of an object directly, you should make them private, in that case you're sure that the data can only be changed by the object's methods itself, and not from anywhere else in your program.

An example:

using namespace std;

class bird
    int count;

    void seen() { count++; }
    int get_count() { return count; }
        // constructor
        count = 0;

int main()
    bird starling;
    cout << "< Begin observation >" << endl;
    cout << "Number of starlings observed: " << starling.get_count() << endl;
    cout << "Number of starlings observed: " << starling.get_count() << endl;
    cout << "< End of observation >" << endl;
    return 0;

Note:: The constructor of an object will be called at the time when the object is being created ("constructed"), a constructor is used to initialize variables in the object, to prepare the object for being used within your program.

for the effort put in this thread

I'm wondering whether the OP is still interested in new posts in his thread.

Not everyone spends every single free minute on a C++ forum like us, so (s)he might still come back. I know: It's hard to comprehend, but I've even heard rumors that there are people who actually don't like programming at all :S Can you imagine? :icon_wink:

Yeh, I've seen people coming back after a year :P
This article has been dead for over six months. Start a new discussion instead.