Defining Class and Creating Objects

When we define any class, we are not defining any data, we just define a structure or a blueprint, as to what the object of that class type will contain and what operations can be performed on that object.

Below is the syntax of class definition,

class ClassName
{
    Access specifier: 
    Data members;
    Member Functions()
    {
        // member function defintion
    }
};

Here is an example, we have made a simple class named Student with appropriate members,

class Student
{
    public:
    int rollno;
    string name;
};

So its clear from the syntax and example, class definition starts with the keyword "class" followed by the class name. Then inside the curly braces comes the class body, that is data members and member functions, whose access is bounded by access specifier. A class definition ends with a semicolon, or with a list of object declarations.



For example:

class Student
{
    public:
    int rollno;
    string name;
}A,B;

Here A and B are the objects of class Student, declared with the class definition. We can also declare objects separately, like we declare variable of primitive data types. In this case the data type is the class name, and variable is the object.

int main()
{
    // creating object of class Student
    Student A;
    Student B;
}

Both A and B will have their own copies of data members i.e. rollno and name and we can store different values for them in these objects.