Last Updated:

Classes in C++

In this tutorial, you'll learn about classes in C++. A class is a generalization of the concept of structure, this data type combines data (fields) and functions (data processing methods). The concept of classes is at the heart of object-oriented programming (OOP). Knowledge of classes is necessary for compiling programs running under MS Windows.

A class is a composite data type whose elements are functions and variables (fields). The concept of a class is based on the fact that "various operations can be performed on objects." Object properties are described using class fields, and actions on objects are described using functions called class methods. A class has a name, consists of fields called class members, and functions—methods of the class.

After the class description, you need to describe the variable of type class. This description has the following format:

1
2
3
4
5
6
7
8
9
class name
{
private:
description of private members and methods of the protected class
:
description of protected members and methods of the public class
:
description of public members and methods of the class

}

Unlike structure fields, which are always available, classes can have members and methods of different levels of access:

  • open, also called public, (public). Their call is carried out using the operator . ("point");
  • private, or private, access to which is possible only by means of open methods;
  • protected methods are intermediate between private and public. We will consider them in the lesson about inheritance.

A class is actually a new data type. To create a variable of type class, use the operator:

name_class name;

Here, name_class is the name of the class, name is the name of the variable. In the following, we will call a variable of type class an object or instance of the class. Declaring a variable of type class (in our example, the variable name of type name_class) is called creating an object.

After the variable is described, you can access the members and methods of the class. This is done similarly to accessing the fields of a structure by using the . (period).

1
2
3
4
5
referencing the p1 field of an instance of the name
name class. p1;
accessing the f1 method of an instance of the class name
par1, par2,.. , parn is a list of formal parameters of the f1
name function. f1(par1, par2,..,parn
);

Note that class members are accessible from any method of the class and do not need to be passed as parameters to method functions.

As an example, consider the complex class for working with complex numbers. In the complex class, there will be members of the class:

  • double x is the real part of a complex number;
  • double y is the imaginary part of a complex number.

It will also contain methods:

  • double modul() is a function for calculating the modulus of a complex number;
  • double argument() is a function for calculating the argument of a complex number;
  • void show_complex() — the function displays a complex number on the screen.

The following is the text of the class and the main function that demonstrates how to work with the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "stdafx.h"
#include <iostream>
#include <math.h>
#define PI 3.14159
using namespace std;
define the class complex
class
{
public:
double x; the actual part of the complex number
double y; the imaginary part of the complex number
method of the complex class — modul function — to
calculate the module complex number
double modul()
{ return pow(x*x+y*y,0.5); }
method of the complex class — argument function for
calculating the argument of the complex number
double argument)
{ return atan2(y,x)*180/PI; }
method of the complex class — show_copmlex function for
outputting the complex number
of void show_complex()
{
if (>=0) output of a complex number with a positive imaginary part of
cout<<x<<y<<"+"<<y<<"i"<<endl;
else output of a complex number with a negative imaginary part of
cout<<x<<y<< "i"<<endl;
}
};
the main function
int main()
{
setlocale(LC_ALL,"Rus");;
define a chislo variable of type complex
complex chislo;
define the actual part of the complex number
chislo. x=2.5;
define the imaginary part of the complex number
chislo. y=1.432;
the output of a complex number chislo.show_complex() is
a reference to a method of the chislo class
show_complex();
output of the complex number module, chislo.modul() —
access to the method of the cout class
<< "Number modulus = "<<chislo. modul();
output of the complex number argument, chislo.argument() —
a call to the method of the cout class
<<endl<<"Number argument = "<<chislo. argument()<<endl;
system(«pause»);
return 0
;
}

The result of the program:

 

This begs the question, why do we need public and private class members and methods, and which members and methods should be declared public and which should be declared private? Using public members and methods allows you to access class members, but this is not always a good thing. If all members of a class are declared public, it is potentially possible to introduce an error into the functioning of the class's interrelated methods when they are accessed directly.

Imagine a class called "System of Linear Algebraic Equations" (SLAU), whose members are:

  • coefficient matrix A;
  • array b;
  • system solution - array x;
  • system dimension n.

Direct access to n, such as cin>>SLAU.n; or SLAU.n=-5; will disrupt the functioning of all methods of the class. Imagine what happens to the method of solving a system of linear algebraic equations with a negative number of equations.

Therefore, the general principle is the following: "The less open data about the class is used in the program, the better." Reducing the number of public members and methods will minimize the number of errors. It is desirable that all members of the class are closed, then it will be impossible to access the members of the class directly using the "." operator. The number of open methods should also be minimized.

If the accessor is not specified in the description of the class members, the members and methods are considered private. It is common to describe methods outside the class.

Let's try to slightly modify the example of the complex class discussed earlier. Let's add a vvod method for entering the actual and imaginary parts of a number; we'll make the class members and the show_complex method private and the rest of the methods public. The following is the text for working with the complex class after you make changes. Note the syntax of the class's methods when they are described outside the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include <iostream>
#include <math.h>
#define PI 3.14159
using namespace std;
class complex define the class complex
{
public methods
:
void vvod();
double modul();
double argument();
Private members and private methods
:
double x
double y;
void show_complex();
};
description of the public vvod method of the complex
void complex::vvod()
{
cout<< "type x \t"; cin>>x;
cout<< "enter y \t"; cin>>y;
Calling the private show_complex method from the public vvod
method show_complex();
}
description of the public modul method of the complex
double complex::modul()
{ return pow(x*x+y*y,0.5); }
description of the public argument method class complex
double complex::argument()
{ return atan2(y,x)*180/PI; }
description of the closed modul method of the complex
void complex class:: show_complex()
{
if (y>=0) cout<<x<<y<<«+»<<y<<«i»<<endl;
else cout<<x<<y<<"i"<<endl;
}
int main()
{
setlocale(LC_ALL,«Rus»);
complex chislo;
chislo. vvod();
cout<<"Complex number modulus= "<<chislo. modul();
cout<<endl<<"Complex number argument = "<<chislo. argument()<<endl;
system(«pause»);
return 0
;
}

The result of the program:

 

This example shows how to share public and private class members. The division into public and private in this example is somewhat artificial, it is made only to illustrate the mechanism for sharing private or public class elements. If you try to access the show_complex() method or the members of class x, y from the main function, the compiler will throw an error message (access to class members is denied).