What's the benefit of classes ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the benefit of classes ?

Classes benefits

18th Jul 2019, 7:14 PM
The unknown 321
The unknown 321 - avatar
11 Answers
+ 5
everything that belongs together is inside an Object and doesn't float around in all of your variables 🎈
18th Jul 2019, 7:34 PM
Anton Böhler
Anton Böhler - avatar
+ 4
A class is a blueprint for objects that control and protect their own data. It exactly defines if and how the stored data can be accessed. This makes an instance easily usable like for example a radio with buttons.
18th Jul 2019, 8:52 PM
HonFu
HonFu - avatar
+ 3
If you was going to build a type of motorbike first of all you would design your idea on a peice of paper(this would be the class in programming).. once you have done this you can start building your motorbike(this is the object of class motorbike in which you construct in main). If later you decide to create another type of motorbike which is similar to the original lets say a sports bike then you can create a subclass of the original motorbike this class inherits all the varables and methods of its parent motorbike class plus can have some of it's own methods and variables.
18th Jul 2019, 8:33 PM
D_Stark
D_Stark - avatar
+ 3
To classify objects.
19th Jul 2019, 2:43 AM
Sonic
Sonic - avatar
+ 2
a function doesn't store things...
18th Jul 2019, 7:58 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Can you give code in c++ ?
18th Jul 2019, 7:34 PM
The unknown 321
The unknown 321 - avatar
+ 1
But it works similarly like classes but a bit different.
18th Jul 2019, 8:00 PM
The unknown 321
The unknown 321 - avatar
+ 1
The unknown 321 Think of it this way: a function is an action that is done when called. It doesn't store things directly, but it can help you store data. A class is like a blueprint. It has the layout and the values of an object, and says something about how the object is supposed to look like. The way paramaters are set in classes and functions are pretty similar, but they are very different in terms of usage. You could use a class to create a car, and then use a function to drive it.
18th Jul 2019, 8:05 PM
Roolin
Roolin - avatar
0
A pretty big advantage of using classes is that they are reusable. Meaning that they only need to be written once. For example, you could make a class for a car, and then proceed to give specifications to that car (wheels, engine, cup holder, etc.). Then you could create different cars by using that class. Let's say you create a car class which holds information about its top speed. You could then use that car-class to create a new instance of a car which holds a specified value of top-speed. We could for example do this in Python: class Car: def __init__(self, topSpeed): topSpeed = self.topSpeed mercedes = Car(300) lada = Car(20) print(mercedes.topSpeed) print(lada.topSpeed)
18th Jul 2019, 7:25 PM
Roolin
Roolin - avatar
0
Anton Böhler but aren't function's the same ?
18th Jul 2019, 7:35 PM
The unknown 321
The unknown 321 - avatar
0
Roolin so classes are useful for having a lot of functions stored in it ?
19th Jul 2019, 6:17 AM
The unknown 321
The unknown 321 - avatar