prefix - Output of a c++ program -
while trying improve c++ knowledge, found problem old programming contest. try enter contest year want prepared.
what output following program?
#include <iostream> using namespace std; int dat=1; int counter=0; class classb; class b { public: virtual void f(int d=5)=0; }; class { unsigned i; public: int pod; int index; a& operator++() { cout<<"a "<<pod--<<dat++<<endl; a* b=new a; return *b; } operator++(int) {cout<<"b "<<pod++<<dat--<<endl; a* b=new a; return *b;} a& operator--() {cout<<"c "<<dat++ <<++pod<<endl; a* b=new a; return *b;} operator--(int) {cout<<"d "<<dat--<<--pod<<endl; a* b=new a; return *b;} void operator+(a& b) {cout<<"addition index "<<index<<endl; pod++;} friend void b::f(int); a() : i(0), pod(dat) {pod++; index=++counter; cout<<"ca "<<"index "<<index<<endl; } ~a(){pod++; cout<<"da index "<<index<<endl;} }; const classb& returnclassa(const classb& p) {return p;} class classb: public a, public b { public: void f(int d=2){a c; c++; pod*=d--;} classb(){cout<<"cb index "<<index<<endl;} ~classb(){cout<<"db index "<<index<<endl;} }; classb returnclassb(classb s) {return s;} class classc : public classb { public: classc(){cout<<"cc index "<<index<<endl;} ~classc(){cout<<"dc index "<<index<<endl;} }; classb returnclassc(classb s){return s;} int main() { classc x; v,w; b *c = new classb; --++v--+++w; returnclassc(returnclassb(returnclassa(x))); return 0; } this supposed solved on paper, because beginner used compiler. also, variables counter , index added me can keep track of objects being created. original expression --++v--+++w--; changed --++v--+++w; because compiler giving me errors.
the part:
classc x; v,w; b *c = new classb; outputs :
- ca index 1
- cb index 1
- cc index 1
- ca index 2
- ca index 3
- ca index 4
- cb index 4
which understand.
i have problem understanding next expression, --++v--+++w; @ first tried understanding output of --++v--++; , add +w.
the output of --++v--++; is:
- d 11
- ca index 5
- b 10
- ca index 6
- a 0 -1
- ca index 7
- c 02
- ca index 8
- da index 6
- da index 5
this means order of operations --(++((v--)++)). why so? there rule operations evaluated first? don't understand why destructors of objects index 6 , 5 called?
if use original expression, --++v--+++w; , output is:
- d 11
- ca index 5
- b 10
- ca index 6
- a 0 -1
- ca index 7
- c 02
- ca index 8
- addition index 8
- da index 6
- da index 5
why +w operation evaluated last? because of operator precedence? also, found out if write cout << v.index return 2, meaning v still original object created before. objects indexes 5-8 go? how can access them?
the last part, returnclassc(returnclassb(returnclassa(x))); outputs:
- db index 1
- da index 1
- db index 1
- da index 1
- db index 1
- da index 1
i don't understand why destructors called?
a few things note when overloading operators in c++ there things know, example ++ caneither preincrement
++i or post increment
i++ in order tell compiler overloading you'll need have no parameters or int in signature
a operator++(int) is overloading post incremennt,
a& operator++() overloads pre increment
in c++ object's desctructor called in 2 cases
you call delete on pointer object allocated using new
a* = new a(); //constructor called delete(a);//destructor calledyou leave context object created on stack
if(something) { a(); ... } //here a's destructor called because no longer exists
notice in code how operators return copy, others return reference.
a operator++(int) this function returning copy. means when call particular function constructor being called once inside function new being called , again when return function copy of object pointed b being copied.
you can find infromation on operator precedence [here] (http://en.cppreference.com/w/cpp/language/operator_precedence)
i notice people tellign it's useless learn code this. agree coding in way pretty bad, can teach lot how constructors/destructors, heap/stack , operators work in c++. there better ways of learning, seems pretty 'mid-term examy' me.
Comments
Post a Comment