C ++ inheritance exercise

      Class One : public Two
        {
        public:
           One()
           {
              Three three;
              cout << "something";
           }
        private:
           Four _four;
       };

I need to show the sentence: "One Two Three Four Five Six Sevens" and class One should remain as they are. Each class cannot display more than one word in its constructor and destructor.

So ... I realized that my base class is the fourth class. I also did constr. and destruction. in each class and tried to write something in their bodies, but this is what ive got on the output:

Class Four Constructor:
Class Three Constructor:
Class Two Constructor:
Class Four Constructor:
Class Four Constructor:
Class Three Constructor:
Class One Constructor:
Class Three Destructor:
Class Four Destructor:

DESTRUCTION:
Class One Destructor:
Class Four Destructor:
Class Two Destructor:
Class Three Destructor:
Class Four Destructor:

my main function:

int main()
{
 One one; //<----  it also have to remain
 cout << endl;
 cout << "DESTRUCTION:\n";
}

I have read several articles on inheritance, but still don't know how to show words in constr classes. and destructors, but do not do this twice or even more, even if I create objects of these classes, as is done in the first class.

PS Sorry for the grammar and other mistakes;)

+3
source share
5

, One ( ++, 100% :-),

  • Two() ( ) ,
  • Four() ( ),
  • Three() ( One()),
  • One(),
  • ~Three() ( , One()).

One. , ! , ; ++. ,

  • ~Four() ( ),
  • ( ~One(), ),
  • ~Two() .

, . : -)

+4

, ...

, , , , . , -, , , ,

:

  • , ? ?
  • , ? ?

. , , . - . -. ?

, , .

Btw, .

!

+1

:] , :)

class Two
{
public:
    Two()
    {
        cout << "Class Two Constructor: ONE" << endl;
    }
    ~Two()
    {
        cout << "Class Two Destructor:" << endl;
    }
};

class Three
{
public:
    Three()
    {
        cout << "Class Three Constructor: THREE" << endl;
    }
    ~Three()
    {
        cout << "Class Three Destructor: FIVE" << endl;
    }
};

class Four
{
public:
    Four()
    {
        cout << "Class Four Constructor: TWO" << endl;
    }
    ~Four()
    {
        cout << "Class Four Destructor: SEVEN" << endl;
    }
};


class One: public Two
{
public:
    One()
    {
        Three Three;
        cout << "Class One Constructor: FOUR" << endl;
    }
    ~One()
    {
        cout << "Class One Destructor: SIX" << endl;
    }
private:
    Four _Four;
};

:

Class Two Constructor: ONE
Class Four Constructor: TWO
Class Three Constructor: THREE
Class One Constructor: FOUR
Class Three Destructor: FIVE

DESTRUCTION:
Class One Destructor: SIX
Class Four Destructor: SEVEN
Class Two Destructor:
+1

, , . , ( , Java, ++, ). , One Two, Three, One, : Three, Two, One.

0
source

Also understand this -

From standard documents, Delete,

In the first alternative (delete an object), if the static type of the operand is different from its dynamic type, the static type must be the base class of the dynamic operand type, and the static type must have a virtual destructor or undefined behavior.

0
source

Source: https://habr.com/ru/post/1783872/


All Articles