Class One : public Two
{
public:
One()
{
Three three;
cout << "something";
}
private:
Four _four;
};
I have to display a sentence: "One Two Three Four Five Six Seven" and class One have to remain as it is. Every class can't display more than one word in her constructor and destructor.
So... i've figured out that my base class is Class Four. I've also made constr. & destr. in every class and tried to write something in their bodies but thats what ive got on 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've read a few articles about inheritance but still have no idea how to display words in classes constr. & destructors but dont do it twice or more even if i create objects of these classes like it is done in class One.
P.S Sorry for gramma and other mistakes ;)
What we can deduce, based solely on the known definition of One (and my remembrance of C++ construction rules, which may not be 100% accurate :-), not taking into account any additional possible inheritance relationships between the classes:
Two() (being the base class constructor) comes before the output of all other constructors,Four() (belonging to a member of the derived class) comes next,Three() (a local object within the One() constructor),One() itself,~Three() (being a local variable, it goes out of scope when returning from One()).And here we have our shiny One object constructed. Since there is not much we can do with it, let's destroy it! Note that all objects are destroyed exactly in the opposite of their order of construction; this is a fundamental C++ rule. Thus
~Four() (since members are destroyed before their owner),~One(), as the derived portion of the class is destroyed before its base part),~Two() as the base part is destroyed last.This gives you exactly seven possible numbers to output, thus you need no additional base classes or members anywhere. Just put the right numbers into the output of the right constructors & destructors :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With