You are on page 1of 4

Elektrijada 2004 Igalo

Object oriented programming


Task1: Task2:

Task3: Task4:

Task5: Task6:

Task7:

Task1 Task2 Task3 Task4 Task5 Task6 Task7 Sum

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

#i nc l ude " i os t r e a m. h"


c l a s s St u de nt ;
c l a s s Pe r s on {
publ i c :
Pe r s on( ) { c out << " c ons t r uc t or Pe r s on" << e ndl ; }
~Pe r s on( ) { c out << " de s t r uc t or Pe r s on" << e ndl ; }
};
c ons t St u de nt & r e t ur nPe r s on( c ons t St ud e nt & p) { r e t ur n 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)));
}

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

#i nc l ude <i os t r e a m>


#i nc l ude <c ompl e x>
us i ng na me s pa c e s t d;
c l a s s Ba s e {
publ i c :
vi r t ua l voi d f ( i nt ) ;
vi r t ua l voi d f ( doubl e ) ;
vi r t ua l voi d g( i nt i = 10 );
};
voi d Ba s e : : f ( i nt ) { c out << " Ba s e : : f ( i nt ) " << e ndl ; }
voi d Ba s e : : f ( doubl e ) { c out << " Ba s e : : f ( doubl e ) " << e ndl ; }
voi d Ba s e : : g( i nt i ) { c out << i << e ndl ; }

c l a s s De r i ve d: publ i c Ba s e {
publ i c :
voi d f ( c ompl e x<doubl e > ) ;
voi d g( i nt i = 20 ) ;
};
voi d De r i ve d: : f ( c ompl e x<doubl e > ) { c o ut << " De r i ve d: : f ( c ompl e x) " << e ndl ; }
voi d De r i ve d: : g( i nt i ) { c out << " De r i ve d: : g( ) " << i << e ndl ; }

voi d ma i n ( ) {
Ba s e b; De r i ve d d; Ba s e * pb = ne w De r i ve d;
b. f ( 1. 0 ) ;
d. f ( 1. 0 ) ;
pb- >f ( 1 . 0) ;
b. g( ) ;
d. g( ) ;
pb- >g( ) ;
de l e t e pb;
}

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

#i nc l ude <i os t r e a m. h>


c l a s s Ba s e {
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;

2
Reenja int f( int );
public:
/*--------------------task 1-------------------------*/ Derived ( int a, int b ) : Base(a), x(b) {};
constructor Person double f( double );
constructor Student };
constructor PhDStudent int Derived::f( int a ){ return a-x;}
destructor Student double Derived::f( double a ){ return a/x+f(5); f(1);}
destructor Person
destructor Student void main()
destructor Person {
destructor Student Base b(2);
destructor Person Derived d(2,4);
destructor Student Base* pb = new Derived(2,4);
destructor Person cout << b.f( 1.2 ) << endl;
destructor PhDStudent cout << d.f( 1.2 ) << endl;
destructor Student cout << pb->f( 1.2 ) << endl;
destructor Person }

/*--------------------task 2-------------------------*/ Task 4. What is the output for the following program?
Base::f(double)
Derived::f(complex) #include <iostream.h>
Base::f(double)
10 class Base{
Derived::g() 20 public:
Derived::g() 10 int x;
Base(){x = 5;}
/*--------------------task 3-------------------------*/ virtual int a();
9.4 int b();
1.3 void operator() (Base& x);
3.4 };
int Base::a(){cout << "Base A\n";this->x;return b();}
/*--------------------task 4-------------------------*/ int Base::b(){cout << "Base B\n";return this->x;}
Base A void Base::operator() (Base& x){
Base B this->x = x.a() * x.b();
Derived A cout << this->x << endl;
Derived B }
Base B
Base B class Derived: public Base{
Derived A public:
Derived B int a();
Base B int b();
25000 };
Base A int Derived::a(){ cout << "Derived A\n";return b()*100;}
Base B int Derived::b(){ cout << "Derived B\n";return this->x * 10;}
Base B
625000000 int main(){
Base obj;
/*--------------------task 5-------------------------*/ Base *d = new Derived();
18 obj.a(); d->a(); obj.b();
d->b(); obj(*d); (*d)(obj);
/*--------------------task 6-------------------------*/ return 0;
64 }
160 Task 5. What is the output for the following program?
96
32 #include "iostream.h"
208 static int b;
176 class X {
144 int *pi;
112 public:
80 X(){b=0;};
48 X(int i) : pi(new int(i)) {b++;}
16 X(const X &x) : pi(new int(*x.pi)){b*=2;}
11 X& operator= (const X&);
~X() {delete pi;}
/*--------------------task 7-------------------------*/ };
252 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;

6 3
xa = funF(xb); mX = x0; mY = y0; mH = hlen;
xc = xa; DrawA A(true, 1);
} A.mainDraw(i);
if ( i == n ){
int main(int argc, char* argv[]){ cout << equalCoo << endl;
X d(); return 1;
g(); }
cout << b << endl; }
return 0; cout << equalCoo << endl;
} return 0;
}
Task 6. What is the output for the following program?

#i nc l ude " i os t r e a m. h" Task 7. What is the output for the following program?
i nt n = 3 , h0 = 256, equalCoo = 0;
static int mX,mY,mH; #include "iostream.h"
#include <vector>
class DrawA; class DrawB; class DrawC; class DrawD; using namespace std;
class Draw{
public: class Node {
Draw(bool ord, int sign); Node *left, *right;
~Draw(){}; public:
void draw(int i, Draw& first, Draw& second); Node(){left = NULL; right = NULL;}
virtual void mainDraw(int level){}; ~Node(){}
protected: int process();
void equal(){if(mX == mY) {equalCoo++;cout << mX << endl;}} Node** getLeft(){return &left;}
bool mOrd; Node** getRight(){return &right;}
int mSign; };
int mLevel;
};
Draw::Draw(bool ord, int sign) : mOrd(ord), mSign(sign) {}; int Node::process(){
void Draw::draw(int i, Draw &first, Draw &second){ int retValue=0;
if (i > 0){ if(left==NULL && right==NULL) return 1;
if(mOrd){ if(left!=NULL) retValue += left->process();
first.mainDraw(i-1); mX = mX - mSign * mH;equal(); if(right!=NULL) retValue += right->process();
draw(i-1, first, second); mY = mY - mSign * mH;equal(); return retValue;
draw(i-1, first, second); mX = mX + mSign * mH;equal(); }
second.mainDraw(i-1);
}else{ class Level {
first.mainDraw(i-1); mY = mY - mSign * mH;equal(); int mLevel;
draw(i-1, first, second); mX = mX - mSign * mH;equal(); public:
draw(i-1, first, second); mY = mY + mSign * mH;equal(); Level(int i);
second.mainDraw(i-1);
} ~Level(){for (int i = 0; i < mLevel; i++) delete mNodes[i];}
} vector<Node*> mNodes;
} friend void link(Level& l1, Level& l2);
class DrawA : public Draw{ };
public:
DrawA(bool ord, int sign) : Draw(ord, sign){}
void mainDraw(int level); Level::Level(int i):mLevel(i){
}; for(int j=0; j<mLevel;j++){
class DrawB : public Draw{ mNodes.push_back(new Node());
public: }
DrawB(bool ord, int sign) : Draw(ord, sign){} }
void mainDraw(int level);
};
class DrawC : public Draw{ void link(Level& l1, Level& l2){
public: for(int j=0;j<l2.mLevel;j++){
DrawC(bool ord, int sign) : Draw(ord, sign){} *(l1.mNodes[j]->getRight()) = l2.mNodes[j];
void mainDraw(int level); *(l1.mNodes[j+1]->getLeft()) = l2.mNodes[j];
}; }
class DrawD : public Draw{ }
public:
DrawD(bool ord, int sign) : Draw(ord, sign){} int main(int argc, char* argv[])
void mainDraw(int level); {
}; Level* levels[12]; int i;
void DrawA::mainDraw(int level){DrawD d(false,1); DrawB b(false, -1); draw(level,d,b);} for( i=0; i<12; i++)
void DrawB::mainDraw(int level){DrawC c(true, -1);DrawA a(true, 1); draw(level,c,a);} levels[i] = new Level(i);
void DrawC::mainDraw(int level){DrawB b(false,-1); DrawD d(false, 1); draw(level,b,d);} for( i=0;i<11;i++){
void DrawD::mainDraw(int level){DrawA a(true,1); DrawC c(true,-1); draw(level,a,c);} link(*(levels[i+1]),*(levels[i]));
}
int main(int argc, char* argv[]){ cout << levels[11]->mNodes[5]->process() << endl;
int step = 0, hlen = h0, x0 = hlen / 2, y0 = x0, i = 0;
for( i=0; i<12; i++)
while(true){ delete levels[i];
i++; return 0;
hlen = hlen / 2; }
x0 = x0 + (hlen / 2);
y0 = y0 + (hlen / 2);

4 5

You might also like