why do we need to have classes ?

i am learning c++

i wonder why do we need to have classes ?

what the things that classes can do , but function() cannot?

thanks
It's called Object Oriented Programming (OOP) and it is just one of the multiple paradigms supported by the C++ language. Consider reading up on OOP for more information about its benefits.
Its not about that if they can do something or not - its about being able to write more efficent and reusable code to develop powerful apps what would be too complicated to write without classes.

In theory you wouldnt need any other type of data than normal int and not having even functions, but this makes things to complicated, and you dont reuse the code you write.
Last edited on
Classes may not be needed as long as there are reused couple of functions.
As a number of functions in program grows the function of classes comes in thoughts.
Classes cannot do anything without functions except for the holding a memory. As for in C++ functions inside function and or/procedure are not allowed.
From the editorial point of view the name reusing is impossible without them. That means a lot of functional names can be united under one then passed to another one...

hope it helps.
there are many answers to this question but polymorphism is possibly the most useful feature of classes if not of c++ in general, read about polymorphism.
Best answered by one of the items in Scott Meyers Part I, it's on of the best ways you'd be able to know about classes, not to mention you're bound to navigate through other useful items also.
There are two types of data types: built-in and class data type.

The latter one is known as "class". The built-in is basically those we use: int, signed, unsigned, char, bool, and etc...

Yes, there is enum, struc for the similar purpose, but not enough to provide a good way to manage your codes.
Last edited on
Here's the best thing I can say that really helped me when I was learning C++ a long time ago: A class is like a blueprint for something. If you have a class "Car", then it can make tons of Cars. All the cars will have the same types of information (steering wheel, seat, windshield, a color), but different variations on those things.

That barely begins to touch it, but that's what was very helpful to me. I think it's important to note for a beginner that it may seem like you don't need classes for the little things you want to do, but even fairly simple programs could be vastly improved with them. Serious projects almost can't happen without them.
Last edited on
I agree with declan, and if you are a beginner, it is a good idea to just quickly look at struct in C++.

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
#include <iostream>

struct Employee
{
    int nID;
    int nAge;
    float fWage;
};


void PrintInformation(Employee sEmployee)
{
    using namespace std;
    cout << "ID:   " << sEmployee.nID << endl;
    cout << "Age:  " << sEmployee.nAge << endl;
    cout << "Wage: " << sEmployee.fWage << endl << endl;
}

int main()
{
    Employee sJoe; // create an Employee struct for Joe
    sJoe.nID = 14;
    sJoe.nAge = 32;
    sJoe.fWage = 24.15;

    Employee sFrank; // create an Employee struct for Frank
    sFrank.nID = 15;
    sFrank.nAge = 28;
    sFrank.fWage = 18.27;

    // Print Joe's information
    PrintInformation(sJoe);

    // Print Frank's information
    PrintInformation(sFrank);

    return 0;
}




ID:   14
Age:  32
Wage: 24.15

ID:   15
Age:  28
Wage: 18.27



Note that every time we process the information via the function, we process a similar thing, the content may be different, however.

Not the greatest way to explain a class, but this is how I first understand classes.
To really make the point simpler:

Classes treat things as objects. The reason people use OO over functions or older styles in a larger program is for portability. It is a lot easier, quicker, and more securer to pull out the information from a class, than a function.

For a small program you might not really need it.
It really depends on the programmer when to use it.

Last but not the least, the programming itself also shapes a bit about how one uses OO techniques. In python almost everything that I do deals is OO (explicitly).
Last edited on
Classes are used to create user defined types. The language only supplies a few to get you started (int, long, float and so on).
Topic archived. No new replies allowed.