+ 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).

14th Feb 2022, 10:26 PM
Tamre Rowe
Tamre Rowe - avatar
9 odpowiedzi
+ 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.
15th Feb 2022, 1:26 AM
Schindlabua
Schindlabua - avatar
+ 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
14th Feb 2022, 10:40 PM
Alberto Barrueta
Alberto Barrueta - avatar
+ 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
15th Feb 2022, 1:03 AM
Raul Ramirez
Raul Ramirez - avatar
+ 2
Awesome! This helps a ton! Thanks everyone!
15th Feb 2022, 1:28 AM
Tamre Rowe
Tamre Rowe - avatar
+ 1
But how does that differ from just norma variables and functions?
15th Feb 2022, 12:05 AM
Tamre Rowe
Tamre Rowe - avatar
+ 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
15th Feb 2022, 1:12 AM
Raul Ramirez
Raul Ramirez - avatar
+ 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?
15th Feb 2022, 1:20 AM
Tamre Rowe
Tamre Rowe - avatar
+ 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.
15th Feb 2022, 2:46 AM
Not interesting But into resting
- 2
Hi
16th Feb 2022, 8:23 AM
Belinda Reynolds