+ 2
Why use classes?
Why should I use classes? They donât seem that different from variables and functions and donât seem to do much other than complicate things. Sorry if this question seems dumb but I just canât understand why you would use classes (Iâm certain there is a reason, I just donât know it).
9 Respostas
+ 6
Yes you can do everything without classes, many programming languages don't have them. At the end of the day they just another way of structuring code.
Once your programs get more complicated you need a way to group code into smaller, more manageable chunks, and classes give you a way to do so. A class can be very complicated internally but none of that complexity will "leak" into the rest of the program if you don't want it to.
Example: You write a bot for Facebook, and of course it will need to connect to Facebook and login and do all sorts of complicated stuff. To post a message you need to send web requests everywhere. But if you program it right, from the outside, it'll just look like
facebook f;
f.post_message("Hello!");
So first you can focus on programming the facebook class without worrying about anything else, and once you are done, you can forget about the complicated stuff you had to do and just use the class in your other code.
+ 5
The clases are like create other type of data, the methods are the way in they can operate whit the objects, and the atributes variables that can contain information in the object
+ 3
Class are like objects in real life
They have properties
They have functions
Ex: class Vehicle{
//properties
- hp ,torque, weight
- acceleration
- accelarationSpeed
- decelarationSpeed
// functions
- getAcceleration();
// constructor
Vehicle(hp,torque,weight)
// printInformation();
}
Now in main we can create a bunch of vehicle simply like this
Main (){
// fyi vector is a class
vector<Vehicle> v;
v.push_back( new Vehicle (225,200,5000));
v.push_back( new Vehicle(300,400,2000));
// pretend i added 100 more
// 100 lines to create 100 cars
//Then i can do something like
int slowest = 0;
for(i < v.size()){
If v[i].getAccel slower slowest = i;
}
v[i].printInfo();
}
Vs doing something like
calcAcceleration(hp,torque,weight);
Int main(){
vector<int> horses;
vecror<int> torques;
vector<string> weight;
vector<float> accelations
// one car
horses.push_back(225)
torques.push_back(200)
weights.push_back(2000)
// so for 100 cars thats 300 lines
To be continued
+ 2
Awesome! This helps a ton! Thanks everyone!
+ 1
But how does that differ from just norma variables and functions?
+ 1
slowest = 0;
for(i < horses.size(){
a = calcAcceleration(horses[i],torques[i], weight[i])
If a slower than slowest slowest = i
Cout << horses[slowest] << etc
Now what if we want to add more properties and more functions with classes that be easy but without then things will get messy
Take away classes keep things organized
And there are plenty of other benefits you will soon learn
+ 1
Ah ok so you would use it when making a large amount of the object to make lots of custom things without adding a ton of variables?
+ 1
*Classes make the code a lot more shorter and easier to maintain.
*you will have to work with variables with types other than the standard ones provided (int, char,... etc) and there is no way around it.
*It's always better to have less to correct if things go wrong.
- 2
Hi