[ drki89 @ 05.02.2010. 13:11 ] @
Zna li neko gde mogu da se nadju zadaci ovakvog tipa:

Code:


Task 1. What is the output for the following program?

#include "iostream.h"

class Student;

class Person {
public:
   Person(){cout << "constructor Person" << endl;}
   ~Person(){cout << "destructor Person" << endl;}
};
const Student& returnPerson(const Student& p){return p;}

class Student : public Person {
public:
   Student(){cout << "constructor Student" << endl;}
   ~Student(){cout << "destructor Student" << endl;}
};
Student returnStudent(Student s){ return s;}

class PhDStudent : public Student {
public:
   PhDStudent(){cout << "constructor PhDStudent" << endl;}
   ~PhDStudent(){cout << "destructor PhDStudent" << endl;}
};
Student returnPhDStudent(Student s){ return s; }

int main(int argc, char* argv[])
{
   PhDStudent laza;
   returnPhDStudent(returnStudent(returnPerson(laza)));
}


Code:

Task 2. What is the output for the following program?

#include <iostream>
#include <complex>
using namespace std;

class Base{
public:
   virtual void f( int );
   virtual void f( double );
   virtual void g( int i = 10 );
};
void Base::f( int ){ cout << "Base::f(int)" << endl;}
void Base::f( double ){ cout << "Base::f(double)" << endl;}
void Base::g( int i ){ cout << i << endl;}

class Derived: public Base{
public:
   void f( complex<double> );
   void g( int i = 20 );
};
void Derived::f( complex<double> ){ cout << "Derived::f(complex)" << endl;}
void Derived::g( int i ){ cout << "Derived::g() " << i << endl;}

void main(){
   Base b; Derived d; Base* pb = new Derived;
   b.f(1.0);
   d.f(1.0);
   pb->f(1.0);
   b.g();
   d.g();
   pb->g();
   delete pb;
}


Code:

Task 3. What is the output for the following program?

#include <iostream.h>

class Base{
   int x;
   virtual int f( int );
public:
   Base( int a ) : x(a) {}
   double f( double );
};
int Base::f( int a ){ return a+x;}
double Base::f( double a ){ return a*x + f(5);}

class Derived : public Base{
   int x;
   int f( int );
public:
   Derived ( int a, int b ) : Base(a), x(b) {};
   double f( double );
};
int Derived::f( int a ){ return a-x;}
double Derived::f( double a ){ return a/x+f(5); f(1);}

void main()
{
   Base b(2);
   Derived d(2,4);
   Base* pb = new Derived(2,4);
   cout << b.f( 1.2 ) << endl;
   cout << d.f( 1.2 ) << endl;
   cout << pb->f( 1.2 ) << endl;
}


Code:

Task 4. What is the output for the following program?

#include <iostream.h>

class Base{
public:
   int x;
   Base(){x = 5;}
   virtual int a();
   int b();
   void operator() (Base& x);
};
int Base::a(){cout << "Base A\n";this->x;return b();}
int Base::b(){cout << "Base B\n";return this->x;}
void Base::operator() (Base& x){
this->x = x.a() * x.b();
cout << this->x << endl;
}

class Derived: public Base{
public:
   int a();
   int b();
};
int Derived::a(){ cout << "Derived A\n";return b()*100;}
int Derived::b(){ cout << "Derived B\n";return this->x * 10;}

int main(){
   Base obj;
   Base *d = new Derived();
   obj.a(); d->a(); obj.b();
   d->b(); obj(*d); (*d)(obj);
   return 0;
}


Code:

Task 5. What is the output for the following program?

#include "iostream.h"

static int b;

class X {
   int *pi;
public:
   X(){b=0;};
   X(int i) : pi(new int(i)) {b++;}
   X(const X &x) : pi(new int(*x.pi)){b*=2;}
   X& operator= (const X&);
   ~X() {delete pi;}
};
X& X::operator= (const X& x) {
   if (this != &x) {
      delete pi;
      pi = new int(*x.pi);
   };
   return *this;
}

X funF(X x){X Xnew(5); x=Xnew; return x;}
   void g() {
   X xa=3, xb=1;
   X xc = xa;
   xa = funF(xb);
   xc = xa;
}

int main(int argc, char* argv[]){
   X d();
   g();
   cout << b << endl;
   return 0;
}



[Ovu poruku je menjao drki89 dana 05.02.2010. u 14:48 GMT+1]

[Ovu poruku je menjao drki89 dana 05.02.2010. u 14:49 GMT+1]