You get a bonus - 1 coin for daily activity. Now you have 1 coin

1.1. data type - class

Lecture



The purpose of introducing the concept of classes in C ++ is to provide the programmer with the means to create new types that are as easy to use as the built-in types. The type is a concrete representation of some concept. For example, the built-in type C ++ float along with the operations +, -, *, etc. is the embodiment of the mathematical concept of a real number. A class is a user-defined type. We create a new type to define a concept that is not expressed directly by built-in types. For example, we could enter the type of TrunkLine (long-distance line) in the program related to telephony, the type Depositir (depositor) in the program of bank management or the type Pretator (predator) in the program of environmental modeling.

A class is a fundamental concept of C ++ and underlies many properties of C ++. The class provides a mechanism for creating objects. The class reflects the most important concepts of object-oriented programming: encapsulation, inheritance, polymorphism.

In terms of syntax, a class in C ++ is a structured type formed on the basis of already existing types.

In this sense, the class is an extension of the concept of structure. In the simplest case, a class can be defined using the following construction:

class_type class_name {class_list_list};

Where

class_type is one of the service words class, struct, union ;

class_name is an identifier;

class_ member_list - definitions and descriptions of typed data and class-related functions.

Functions are class methods that define operations on an object.

Data is the fields of an object that make up its structure. The field values ​​determine the state of the object.

We will call class members as class components, distinguishing component data and component functions.

Example 1.1.1

struct date // date

{int month, day, year; // fields: month, day, year

void set (int, int, int); // method - set the date

void get (int *, int *, int *); // method - get date

void next (); // method - set the next date

void pri nt (); // method - display the date

};

Example 1.1.2

Example 1.1.2

struct complex // complex number

{double re, im;

double real () {return (re);}

double imag () {return (im);}

void set (double x, double y) {re = x; im = y;}

void print () {cout << ”re =” << re; cout << “im =” << im;}

};

To describe an object of a class (class instance), the construction is used

class_name object_name

date today, my_birthday;

date * point = & today; // pointer to date object

date clim [30]; // array of objects

date & name = my_birthday; // object reference

The objects defined include data that corresponds to the data members of the class. Member functions of a class allow processing data of specific objects of a class. You can access the data of an object and call functions for an object in two ways. First, with the help of “qualified” names:

object_name.class_name:: data_name

object_name.class_name:: function_name

Class name may be omitted

object_name.data_name

object_name.function_name

For example:

class “complex number”

complex x1, x2;

x1.re = 1.24;

x1.im = 2.3;

x2.set (5.1,1.7);

x1.print ();

The second access method uses a pointer to an object.

pointer_to_object–> component_name

complex * point = & x1; // or point = new complex;

point -> re = 1.24;

point -> im = 2.3;

point -> print ();

Example 1.1.3

// Class “goods”

intpercent = 12; // markup

struct goods

{char name [40];

float price;

void Input ()

{cout << “name:”;

cin >> name;

cout << “price:”;

cin >> price;}

void print ()

{cout << “\ n” << name;

cout << “, price:”;

cout << long (price * (1.0 + percent * 0.01));}

};

void main (void)

{goods wares [5];

int k = 5;

for (int i = 0; i

cout << “\ nList of goods at mark-up” << percent << “%”;

for (i = 0; i

percent = 10;

cout << “\ nList of goods at mark-up” << percent << "%";

goods * pGoods = wares;

for (i = 0; i print ();

}

created: 2015-12-20
updated: 2021-03-13
132395



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

C ++ (C plus plus)

Terms: C ++ (C plus plus)