why classes and objects ?? (c++)

i am believe that i am noob in programing spacial in C++

1st i learn how i make functions (it was amazing)

but now after i learn how to deal with class and object i am so confused... when i should use it .... what make it so efficiency more than functions ... why i should use it instead of function ??

in more examples i see i try to do it by functions not classes ??

i don't know its good or bad ??

i need ur help to find example i can't make it by functions and should use classes ?? example appear the efficiency of classes ??
Well believe it or not there are few hard rules, but some things are better handled one way than another.

A function is great for simple task. The thing to remember a function can only return one value. Now it can be long and contain multiple values such as day, month year such as 04232017.

A class is a little more advanced, and can contain data and functions. The first thing to ask is the data all related to the same thing, such as name, age and birth date.

So if you know someones name and birth day, you could have a function that tells you their age without asking for that data. You do that because the age will change, the birth day will not.

class person
{
public:
string name;
int age;
int DOB;
};
There is nothing you can't do with functions and C-structs (not class-like, just data). Actually there is nothing you can't do with just loose variables and functions. Come to think of it, you can do anything in a monolithic main function also, so why write functions? The answer to this absurdity is the same to your question... you use functions to make main more readable, to reuse the code, to organize the code. You use structs to pull the data together and manage it better, make it more readable... builds right on up to classes where you manage data and functions all together. You do it because it makes your code better, easier to understand, easier to manage, offers a layer of safety against bugs, and more ... for all the same reasons you would use a function.

No one can show you an example that can't be done without. At the end of the day, anything you can do in C++ is converted to assembly language which has none of these constructs. Therefore (without getting long winded) you can do anything without these constructs. Just because you can do something, does not mean you should :)

Last edited on
Topic archived. No new replies allowed.