+ 4
Class and functions
Why to use classes if you have normal functions?
5 Réponses
+ 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☺️☺️.
+ 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?
+ 2
What is the difference between methods and classes
+ 2
Thanks Friend👍👍
+ 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)