Class and functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Class and functions

Why to use classes if you have normal functions?

24th Aug 2018, 1:30 PM
I Am Arthur
I Am Arthur - avatar
5 Answers
+ 5
Function : Function are used to reduce code in your program. For example : Suppose your program is based on adding numbers. Instead of doing addition every time , you can define function once and call it as many times as you want. Class : Class can be useful to store real world things. For example : If you want to store details of book such as page no. , author , book name , etc. You can with usage of classes. (C++ language) class bookDetails { string author = "abc"; string bookTitle = "objects"; int pageNumber = 13; public : int authorName() { cout << author; } }; Method : Method is just function inside classes. Hope this helps☺️☺️.
25th Aug 2018, 5:19 AM
Meet Mehta
Meet Mehta - avatar
+ 3
Good question! This is because sometimes you want to associate certain functions to certain objects, so that the object will use it's own properties to determine the output. -------------------- For example, if you're making a game involving cars, you could say something like: for car in cars: # For each car in the 'cars' array, car.move() # Move the car car.show() # Then show the car (This is in Python since you didn't specify a language, but it would look similar in your language too) --------------- This might makes use of the individual 'x' and 'y' properties of each car to change its location, and then use the new location to display it. All this is only possible because there are individual 'Car' objects which have their own methods and properties. Could you try and do this using regular functions?
24th Aug 2018, 1:53 PM
Just A Rather Ridiculously Long Username
+ 2
What is the difference between methods and classes
25th Aug 2018, 4:02 AM
Kavish Bamboli
Kavish Bamboli - avatar
+ 2
Thanks Friend👍👍
26th Aug 2018, 6:36 AM
Kavish Bamboli
Kavish Bamboli - avatar
+ 1
Just A Rather Ridiculously Long Username Yeah, since that theres a function called move and show for car in cars: move(car) show(car)
24th Aug 2018, 10:43 PM
I Am Arthur
I Am Arthur - avatar