
Class
Class is a logical entity contains the information of their members like member method, member variable, static method, static variable, access modifier, constructor, destructor & constant.
the class keyword is used to define a class.
- Class is a user-defined data type.
- Class members are by default private.
- Whichever is written inside a class, called members of the class
Syntax
Example
#include <iostream> using namespace std; class Student { public: string studentName; void walk() { cout << "walking..." << endl; } };
class Student { public: string studentName; void walk() { cout << "walking..." << endl; } }
Explanation
In the above example, Student is the name of the class, and public is an access modifier (access specifier), studentName is a variable (properties of student), walk is a method.
- studentName and walk both are members of Student class.
Object
An object is a real-world entity, which is declared at compile time and initialize at runtime.
- An object is a collection member variable.
- An object is stored in the heap area.
- We create any number of objects of a class.
Syntax of creating an object
ClassName object1;
Example
#include <iostream> using namespace std; class Student { public: string studentName; void walk() { cout << "walking..." << endl; } }; int main(int argc, char const *argv[]) { Student s1; // s1 is an object of Student class // give name to student s1.studentName = "Online Vidyalay"; // calling method s1.walk(); return 0; } // output // walking...