+ 1

Why to use class in C++? What is benefits of using Class and Object in C++?

28th May 2020, 10:43 AM
Aniket Gade
Aniket Gade - avatar
4 Antworten
+ 1
Classes are used to model the real world entity through programming. What if I want to create an Aniket Gade ? 😉
28th May 2020, 12:02 PM
Avinesh
Avinesh - avatar
+ 1
From my basic understanding it's creating things in life and setting them as one accessible object. I.e. your making a game, you can create a monster class and have derived ogre class or dwarf class or whatever you want with attacks set and walking habits and descriptions of looks, literally anything you want to add. Now you can edit, access and use these anywhere in the rest of your code. Did this help at all? 👍
28th May 2020, 12:25 PM
Olivia
Olivia - avatar
0
Suppose you want to build a program to store information about Facebook users such as email address, date of birth, etc. If you don’t use class, you have to create a variable for each user’s information, and it can take a lot of them. If there are 5000 user and 100 properties, you have to create 500000 variables. If you create a class, you can describe any user with just 100 variables + you get a nice hierarchical structure. Compare this: string bobby_email; string bobby_dob; string jane_email; string jane_dob; to: User bobby, jane; bobby.email = ... bobby.dob = ... jane.email = ... jane.dob = ... You can also set all information of a user in one class function call, instead of accessing each variable, like: bobby.set(“email”, “dob”, ..) instead of: bobby_email = .. bobby_dob = ..
28th May 2020, 3:06 PM
Bobby Fischer
Bobby Fischer - avatar