difference b/w object and class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

difference b/w object and class?

27th Aug 2016, 5:10 PM
teju
3 Answers
+ 4
A class is a block of code where one would store variables and functions. For example, when creating a game, you may have a class for enemy, which contains variables such as health and attack power, or functions such as attack(). Classes also have three different access specifiers you can use - public, private and protected. Use the SoloLearn modules on this to learn the difference between the three, otherwise this would be a very long answer. An object is simply a way how one would access members of that particular class. You can have more than one object for a single class, and each object will holds its own variables. For example, if you had int 'a' in the class, object1 could have 'a' with a value of 5, where as object2 could have 'a' with a value of 10. Example: #include <iostream> using namespace std; class enemy { public: void setAp(int ap) { attackPower = ap; } void attack() { cout << "Enemy does " << attackPower << " damage!"; } private: int attackPower; }; int main() { enemy obj; obj.setAp(5); obj.attack(); }
27th Aug 2016, 7:16 PM
Cohen Creber
Cohen Creber - avatar
+ 1
A class defines how to create an object. An object is an instance of a class, that you can use concretely. Think of the class as a blueprint and an object of the class as something built from it.
27th Aug 2016, 8:37 PM
Zen
Zen - avatar
0
the class is a recipe the object is the food.
24th Sep 2016, 9:22 AM
kis geza