Metric-Cart

Blog about becoming a programmer

code
Programming languages

C and C++ languages

C
This programming language is widely used to create system software and is very popular among software developers. This language is characterized by the fact that it combines the features of high-level language, allowing the programmer to access all the machine resources, in contrast to Pascal and BASIC.

Structure of work in C
A structure in this language is a data type designed to combine data of different types into a single whole. The order in which the values are stored in memory is set when defining the type, and is preserved for the lifetime of the objects, which allows indirect access.

The structure type in C allows recursion, that is, the presence in its composition of pointers referring to objects of this very structure. Thus, structures in C combine the functionality not only of tuples and records, but also of algebraic types. To make it easy to understand, a structure is said to be a class with all fields public by default.

ะก++
C++ was developed by a Swedish programmer named Bjarne Stroustrup in the early 1980s. C++ has a few additional commands and operators, but the main difference is the approach to programming. The main reason for the popularity of C++ today is its support for object-oriented programming (OOP). OOP is a different way of writing programs that helps programmers write programs faster and with fewer errors. OOP also makes it possible to increase the speed of maintenance. This language is a “descendant” of the C language, but it has a number of significant differences.

Structure in C++
A structure is a collection of variables with a single name that provides a common way to store information together. Declaring a structure results in a pattern used to create structure objects. The variables that form a structure are called structure members.

Structures are useful when you need to combine several variables with different types under one name. This makes the program more compact and more flexible to make changes. Structures are also indispensable when you want to group together some data, such as a record from a database or a contact from an address book.

Differences in C and C++ structures
Classes and objects appeared in C++. Technically, a C++ class is a structure type in C and an object is a variable of that type. The only difference is that in C++ there are also access modifiers and fields can be not only data but functions;
What in C++ is inheritance, in C is just a structure in a structure;
In C++ there are two new operations: new and delete. When new is called, the constructor is automatically called and when delete is called, the destructor is called. The innovation can be described by the formula: new = malloc + constructor, delete = free + destructor;
In C++ there are functions which are called automatically after structure variable creation (constructors) and before its destruction (destructors). In all other respects, these are normal functions that are subject to a number of restrictions. Some of these restrictions are not justified and get in the way;
The C++ developers introduced the word var. And to make it look original, they renamed “var” to “&” and called it “reference”. This caused a lot of confusion, because C already had the concepts of “pointer” and “address” (denoted by the same symbol &), and the concept of “reference” also sounds like something pointing-addressing;
The desire of C programmers to control parameter types in definitions gave rise to inline functions in C++. Such a function is an usual define with parameters, but you don’t have to bother with “\” symbols and types are checked. The desire to legitimize in define parameters the name of a type gave rise to template. The main advantage of template is that #define with the same parameters will produce two identical pieces of code. And the compiler’s template is likely to be co-optimized: the same pieces of code will be concatenated into one;
Different standard file extensions. file.c means the file is written in C and file.cpp means the file is written in C++;
In C++ the void keyword is optional. Many C++ programmers include void as a means of improving program readability and indicating that the function has no parameters;
In C++, all functions must have prototypes. In C, this requirement is optional;
In C, a character constant is automatically incremented to an integer. This is not the case in C++;
In C, declaring a global variable more than once is not an error, although it does not serve as a sign of good programming. In C++, it is an error;
In C, an identifier has at least 31 significant characters. In C++, all characters are treated as meaningful;
In C, it is possible to call the main() function from a program, although this is atypical. In C++, this is not allowed;
In C, you cannot take the address of a register variable. In C++ you can do this.