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

Inheritance 2.5. Turn on objects

Lecture



There are two options for including an object of type X in class A;

1) Declare in class A a member of type X;

class A {
X x;
// ...
};

2) Declare in class A a member of type X * or X &.

class A {
X * p;
X & r;
};

It is preferable to include the object itself as in the first case. This is more efficient and less error prone, since the relationship between the contained and containing objects is described by the rules of construction and destruction.

For example,

// A person
class person {
char * name:
public:
person (char *);
// ...
};
//School
class school {
person head; // director
public:
school (char * name): head (name) {}
// ...
};

The second variant with the pointer can be used when during the lifetime of the “containing” object it is necessary to change the pointer to the “contained” object.

For example,

class school {
person * head; // director
public:
school (char * name): head (new person (name)) {}
~ school {delete head;}
person * change (char * newname) {
person * temp = head;
head = new person (newname);
return temp;}
// ...
};

The second option can be used when you need to set an “contained” object as an argument.

For example,

class school {
person * head; // director
public:
school (person * q): head (q) {}
// ...
};

Having objects that include other objects, we create a hierarchy of objects. It is an alternative and addition to the class hierarchy. But what about when the number of included objects is not known in advance and (or) may change during the lifetime of the “containing” object. For example, if the school object contains students, their number may vary.

There are two ways to solve this problem. The first is that a linked list of included objects is organized, and a “containing” object has a member-pointer to the beginning of this list.

For example,

class person {
char * name;
person * next;
...
};
class school {
person * head; // pointer to school principal
person * begin; // pointer to the beginning of the list of students
public:
shool (char * name): head (new person (name)), begin (NULL) {}
~ shool ();
void add (person * ob);
// ...
};

In this case, when creating the school object, an empty list of the included objects is created. To include an object, the add () method is called, to which a pointer to the included object is passed as a parameter. The destructor deletes all included objects one by one. The person object contains the next field, which allows you to link objects to the list. A complete program demonstrating this method of inclusion is given in Example 2.4.2.

The second way is to use a special container object.

The container class is designed to store objects and presents convenient, simple and convenient ways to access them.

class school {
person * head;
container pupil;
...
};

Here pupil is a container containing pupils. Everything you need to add, delete, view, etc. included objects must be contained in the methods of the container class. An example would be C ++ Standard Template Library (STL) Containers.

Consider the relationship between inheritance and inclusion.

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



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)