Defining of Class in C++

Class in C++ is almost similar to structures. The only difference being that classes have functions too, as their members, apart from different types of variables. There is also one another difference between classes and structures. Such difference is that the members of a class are private. by default, while, the members of a structure are public, by default. The meanings of private members and public members will get clearer to you as move ahead with the tutorial.

Defining or Declaration of Class:
A class is a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary, from external use. While defining a class, we are creating a new abstract data type that can be treated like any other built-in data type.

Generally, a class specification in C++ has two parts:

  1. Class declaration
  2. Class function definitions

The class declaration describes the type and scope of its members. The class function definition describe how the class functions are implemented.

The general form of a class declaration is:

class class_name
     {
     private:
          variable declaration;
          function declaration;
     public:
          variable declaration;
          function declaration;
     };

Now, let us understand the above definition in detail:

  1. The keyword class specifies that what follows is an abstract data of type class_name
  2. The body of the class is enclosed within braces and is terminated by a semicolon
  3. The class body contains the declaration of variables and functions, collectively called as members
  4. They are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public. The keywords private and public are known as visibility labels. Note that these keywords are followed by a colon.
    The members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from outside the class also. The data hiding (using private declaration) is the key feature of object-oriented programming. The use of the keyword private is optional. By default, all the members of a class are private
  5. The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions can have access to private data members and private member functions. However, public members can be accessed from outside the class
  6. The binding of data and functions together into a single class-type variable is referred to as encapsulation

Example of a class declaration:

class item
     {
          //variable declaration
          //private by default
          int number;
          float cost;
     public:
          //function declaration using prototype
          void getdata(int a, int b);
          void putdata(void);
     };

Here,
item is a class name,
number and cost are data members which are private by default, and
getdata and putdata are the member functions and both are public.

You may also like...

Leave a Reply

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