Site icon i2tutorials

CPP – Class

Class

 

C++ incorporates all of these extensions into a different user-defined type called a class.

A class is a user-defined type of data that links data and associated functions.

Generally, a class specification has two parts:

Class declaration

Class function definitions

The class declaration sets out the type and scope of its members. Class function definitions outline how class functions are implemented.

 

The General form of a class declaration

class class_name

{
private:
Variable declarations;
Function declarations;
public:
Variable declarations;
Functions declarations;
};

 

The keyword class specifies that what follows is an abstract data of type class_name. The body of a class is enclosed in braces and finished with a semi-colon. The class body contains the declaration of variables and functions. These functions and variables are collectively referred to as class members. They are usually grouped into two sections, namely, private and public to indicate which of the members are private and which of them are public. They are usually grouped into two sections, namely, private and public to indicate which of the members are private and which of them are public. Not that these key words are followed with a colon. If the two labels are missing, then the default is for all members to be private.

 

Class members that have been declared private may be viewed from the class only and public members may be viewed from outside the class. Variables declared inside a class are called data members and functions are called member functions. Linking data and functions together in a single class type variable is called encapsulating.

 

Example for class specification:

class student

{
int rno;
char name[10];
public:
void get();
void put();
};

 

The above specification specifies that name of the class is a student. It includes rno and name as private data member and get() and put() as public member functions.

Exit mobile version