24. What Is Class?

By | September 23, 2021

When you define a class, you define a blueprint for a data type. This doesn’t actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows −

Create a Class

To create a class, use the class keyword:

Example

Create a class called “MyClass“:class MyClass {       // The class
  public:             // Access specifier
    int myNum;        // Attribute (int variable)
    string myString;  // Attribute (string variable)
};

Example explained

  • The class keyword is used to create a class called MyClass.
  • The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class. You will learn more about access specifiers later.
  • Inside the class, there is an integer variable myNum and a string variable myString. When variables are declared within a class, they are called attributes.
  • At last, end the class definition with a semicolon ;.

Leave a Reply

Your email address will not be published. Required fields are marked *