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 :

  1. ca index 1
  2. cb index 1
  3. cc index 1
  4. ca index 2
  5. ca index 3
  6. ca index 4
  7. 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:

  1. d 11
  2. ca index 5
  3. b 10
  4. ca index 6
  5. a 0 -1
  6. ca index 7
  7. c 02
  8. ca index 8
  9. da index 6
  10. 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:

  1. d 11
  2. ca index 5
  3. b 10
  4. ca index 6
  5. a 0 -1
  6. ca index 7
  7. c 02
  8. ca index 8
  9. addition index 8
  10. da index 6
  11. 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:

  1. db index 1
  2. da index 1
  3. db index 1
  4. da index 1
  5. db index 1
  6. 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

  1. you call delete on pointer object allocated using new

    a* = new a(); //constructor called delete(a);//destructor called 
  2. you 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

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -