Last Updated:

The concept of class in C++

The purpose of introducing the concept of classes in C++ is to provide the programmer with a means of creating new types that are as convenient to use as the built-in ones. In addition, derived classes and templates are ways to organize classes that have something in common with each other.

A type is a specific representation of a concept. For example, the built-in float type, together with the operations +, –, * etc., represents a specific embodiment of the mathematical concept of a real number.

A class is a user-defined type. A class definition defines a representation of objects of that class and a set of operations that can be applied to those objects. A class provides an abstraction of data, it hides the details of an object's view, and it provides access to the data it contains only through the functions and operations described as part of that class.

The main point of introducing new types is to separate meaningless implementation details (for example, the location in memory of the components of objects of a given type) from properties that have a determining value for the correct use of the entity (for example, a full set of data access functions). This separation is best expressed in terms of restricting access to data from the outside and using special procedures for this purpose within a well-defined interface.

A carefully selected set of user-defined types makes the program more concise and expressive. In addition, these types make it possible to conduct a variety of code analysis. In particular, they allow the compiler to detect cases of unacceptable use of objects that would not otherwise be detected until the testing stage.

The class definition is as follows:
class <classname> { <list of class members> };

A class declaration is a declaration of some type. For further work, you must declare the appropriate variables or objects of the class.

class X { ... };Type X declaration
X x;Declare a variable – an object of class (type) Xх

Objects of a class can be assigned, passed as parameters to a function, and returned as its result. Other natural operations, such as equality checking, can also be defined by the user.

In the list of class members, you can declare variables, functions, classes, enumerations, and friendly functions and classes. A class member cannot be declared in the list of class members twice. This also applies to functions (although there may be functions with the same name, but a different set of formal parameters). In addition, you cannot declare a variable and a function with the same name in a class. The list of members of a class defines the complete set of members of that class. You can't add members to a class somewhere else.

class X 
int i; 
int i;Error - Re-advertisement
}; 
int X::k;Error - Attempted to declare a class member outside of the class declaration
class Y 
int f(); 
int f();Error - Redeclaring function
int f(int x);No errors
}; 
class Z 
int f(); 
int f;Error – there is a function with the same name
}; 
 

A class member cannot have an initializer. A class member cannot be declared with the autoextern, and register memory class specifications. Class objects are initialized using constructors. A class object cannot contain an object of the same class, but it can contain a pointer or reference to an object of the same class.

To access the members of a class (after declaring some variable of that class or pointing to an object of that class), use the following syntax:
<change> . <the name of a class member> <point> -> <class member name>

3. Accessing Class Members

 

Access control is applied consistently to class member functions and class member data.

A class member can be:

  • private, which means that its name can only be used within class member functions and friends of the class in which the class member is declared;
  • protected means that its name can only be used within class member functions, friends of that class, and classes derived from it;
  • public means that its name can be used inside any function (as well as outside functions in initializers).

Access modifiers can be used multiple times in the same class declaration.

The access control mechanisms in C++ provide protection against accidental, not intentional, access. However, this refers to the problems of the programming discipline, not to the problems of the language.

Class members without an access specifier are private by default. Members of structures and associations are public by default.

4. Class Member Functions

 

A function declared in a class without a friend specifier is called a member function of the class. Its call has the appropriate syntax.

Describes the class member function to relate to the scope of the class. This means that a member function of a class can directly use the names of the members of its class.

class X { privateint n; publicvoid f(); };Class X Declaration
void X::f() { n++; }Defining a Function f from Class X
X a, b;Declaring Class X Variables
a.f();The call to the f function applies to the variable . Thus, // changes the n member of the variable . The variable remains unchanged. аab

4.1. Constant Functions-Members of the Class

 

In the function declaration, you can add a const modifier after the parameter list. This will mean that the function does not change the state of the object to which it is applied. The const suffix is part of a function type and must be written when a function is defined outside the class.

class X { privateint n; publicint f() const; }; int X::f() const 
return n++; }Error – Attempting to change the value of a class member in a constant function

A constant function member of a class can be called on both a constant and a non-constant object, while a non-constant function can only be called on an object that is not a constant.

4.2. This Index

 

In a non-static class member function, the this keyword denotes a pointer to the object on which the function is called, that is, inside a member function of a class, a member of the same class named x can be denoted as x, and as this -> x. A pointer to the object on which a function is called is an implicit parameter of that function.

class X { privateint n; public: 
void f(int n) { this -> n = n; } };a member of the n class is set to the value of the n parameter

The this pointer in a member function of class X is of type X * const. However, this is not an ordinary variable, it is impossible to get its address or assign it anything. In a constant member function of class X, this is of type const X * const to prevent modification of the object itself.

In most cases, the use of this is implicit. Specifically, each access to a non-static member of a class implicitly uses this to access a member of the corresponding object.

Member functions of a class can also use the this pointer to return a reference to a class object so that they can use the function call as a parameter of another function.

class X { ... public: X& f(); }; X& X::f() { ... return *this; }

4.3. Embedded Class Member Functions

 

A member function of a class can be described inside a class declaration. In this case, it is considered an inline function.

Example. In both cases, the GetSize function is embeddable. 

class Vectorprivate: int size; ... public: int GetSize() { return size; } ... } class Vector { private: int size; ... public: int GetSize(); ... } inline int Vector::GetSize() { return size; }