The basics of programming in c++ for beginners

Pointers to objects

When we first met with pointers in C ++ (cm. Pointers in C++. Part 1 ) there can be a simplified representation, that the pointers can point only to individual variables embedded (scalar) C ++ type, and that this is just one more, an alternative form of access to such variables. In such an application pointers would welcome addition language, but with a very limited capacity.

A closer examination of C ++ pointers, we find, the pointer may be the address of accommodation (point at) any permissible object program: structure, class object, array, functions, or again, a pointer to an object, or a pointer to a pointer, and so on… This makes the pointer almost the most powerful tool in the programmer C ++ ... but also the most dangerous in terms of possible hidden errors.

Consider these options more. The easiest option is to use pointers to complex objects (class objects and structures). But such use of pointers opens up broad prospects in C ++, as we shall see.

So, we already know, that the class definition does not create any new database objects in the program - it is only a description of a template, new data type, according to which will create real objects data.

While creating (advertisement) New data objects, we can calculate the address of these objects and assign them to pointers to objects this class. Write a simple program, operations to be carried out with pointers to objects some class (file ex1.cc):

Here we see a relatively new topics in our design:

This constructor new class my, but with a parameter creation. When you call it calls the superclass constructor (number(numb)), giving it the same value. The following parentheses {} frame empty block code, which means, that nothing more needs to be done over and above the call the parent constructor.

Remembering, that the entire sequence of the constructors of all parent classes – call (in reverse order of succession) when calling engendered by the class constructor, but this only for designers without parameters. In the case of parameterized constructors rodieley you will have to call the clearly.

But we will digress from the subject of our discussion… And now it's time to compile and see the execution of the program we have received:

c ++, pointers to objects, Programming for beginners, c++

Nothing fundamentally new, and all this is much like the, we would work with pointers to variables of type double, let us say.

Recall, in addition, what operator new to dynamically create a new object:

and) It causes the dynamic memory control manager and allocates a new volume for placement of the object;

b) causes constructor the corresponding class (data type) for the initial markup (initialization) dedicated memory. Slightly modify your example (file ex2.cc):

Building and Running:

c ++, pointers to objects, Programming for beginners, c++

It is still there have been no radical changes ..., but pay attention to the, that the program (and it could be arbitrarily large program) declared and no not used explicitly objects - The program operates only pointers to objects.

And, finally, we get closer to the techniques in the use of pointers to objects, which makes it the most powerful indicators of the work tool with classes and objects in C ++. Namely: Virtualization member functions and class polymorphism.

concept polymorphism (variability of form, the diversity) – one of the main C ++ development on its predecessor, the C. It is, that the progenitor of a whole family inherited classes announces some of its functions, methods like virtual. A different classes inherited these features methods redefined differently.

It means, that different derived classes (related) function method with the same name will be performed somewhat different way, depending on the class to which it is used.

To demonstrate this technique on the example. Create (file ex3.cc) class, describing in general any figures on the flat plane (it can be elements of a 2D game scenario):

Here we have an example of, in C ++ called abstract class: class, which defines at least one virtual function with the definition of the form:

In the illustrated class features such as much 3. Naturally, that create an object abstract class impossible, there are functions, which the body is not defined. Trying to declare objects of this class will cause a syntax error. But from such a class, you can inherit, create derivative classes, who will inherit the general properties of a generic abstract (for example, coordinates x and Y the center of the figure and its size r).

define 3 derivatives from figure class: a circle, square and an equilateral triangle:

Now we are ready to create a program, for which they were built all these preparations: create any number different 2D geometrical objects, on which can perform uniform (virtual) actions, regardless of their different natures:

In this very simple example illustrates the, what in object model C ++ language called Polymorphisms. And this property is one of the most powerful tools of expression of C ++. And this technique is always realized through pointers to objects (figure*).

That will look like a compilation and execution of our example (ex3.cc) at the terminal of the Linux operating system when using the GCC compiler with the C ++ language (it will once again confirms, that programming in C ++ in the least dependent on the operating system):

c ++, pointers to objects, Programming for beginners, c++

Once again we turn to the code shown in Example, and once again fix the very important fact, that C ++ pointers always typified: Index can not be "a pointer to something". The C ++ language – it is the language with strong Name typing. The type of the pointer is: a pointer to the variable type indicates it, such as "pointer to double». Pointers to different types are incompatible with each other on assignment and comparison.

Technology virtual functions and polymorphism are so central to the whole philosophy of C ++, that require a separate detailed consideration. About this talk in one of the following lessons.

11 thoughts on “Pointers to objects

  1. On this topic – pointer indicates the beginning of the memory area (stores the address). And there is nothing more to write about it, tk. using the pointer you just take the value at that address, and leads him to the correct type (any).
    If and to disclose the topic – IMHO it is necessary to write about it, what problems solved pointers (without touching the PLO, tk. then this theme is almost not disclosed) and write based on pointers vs reference (topic has been disclosed in Myers, You can take it basis).

    But how are you then turned sharply you polymorphism. It's not bad, but nick, who read only your website is now little understood. Before, describing the classes you did not write about virtual functions, and even about the inheritance. And then suddenly fell to the reader all this and abstract classes.

    At the end of the article you describe polymorphism – I think it's worth a separate article. But before you write an article about “Class inheritance in C++” (This last theme in your plan on the main page)

    1. I agree. First about pointers, and then some fig about polymorphism. Somehow everything is too piled up.

  2. Tell, and who is admin? Lily and Igor? I always thought, that the name of the administrator writes Igor.

    1. class object (type) my – m1.
      A pointer to this object – pm1.
      m1.show() – method call (functions) show() class for my object m1.
      pm1->show() – call exactly the same method show() my class through pointer object m1.

      1. Actually, m1 – it is one object, and pm1 – a pointer to a completely different object of the same class (the Heap).

  3. This is lost 93 903 Pybley >>> https://forms.yandex.ru/cloud/6547f889068ff0ecfdb5873a/?hs=7547a39eb399d3a1f3aca107d969725b& # YU386 says:

    1p1rlh

  4. Tell me how to fix this error?
    Ошибка (actively) E0020 identifier “M_PI” not specified Project1

Leave a Reply

Your email address will not be published. Required fields are marked *