Professional Documents
Culture Documents
מונחה עצמים- פתרון מועד א - המכללה למנהל 2010
מונחה עצמים- פתרון מועד א - המכללה למנהל 2010
h>
class A
{
protected:
float f;
public:
A(float j):f(j) {}
};
class B: protected A
{
char *str;
public:
B(int val=0, char *str1=NULL){
str = new char[strlen(str1)+1];
strcpy(str,str1);
f = 2;
1
12
}
};
class C: public B
{
int i;
public:
C(int val, char *str1){
str = new char[strlen(str1)+1];
strcpy(str,str1);
i=val;}
};
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int val=0):x(val)
A(const A& a):x(a.x)
void h()
virtual void i()
virtual void g()
virtual ~A()
{cout<<"(1)"<<endl;}
{cout<<"(2)"<<endl;}
{cout<<"(3)"<<endl;}
{h(); cout<<"(4)"<<endl;}
{i(); h(); cout<<"(5)"<<endl;}
{cout<<"(6)"<<endl;}
};
class B : public A
{
public:
B(const B& b ):A(b) {cout<<"(7)"<<endl;}
B()
{cout<<"(8)"<<endl;}
virtual void f()
{cout<<"(9)"<<endl;}
void g()
{h(); cout<<"(10)"<<endl;}
void i()
{ h(); cout<<"(11)"<<endl;}
virtual void h()
{cout<<"(12)"<<endl;}
virtual ~B()
{cout<<"(13)"<<endl;}
1
12
};
class C : public B
{
A a2;
B b2;
public:
C(const B& b):b2(b)
C(const A& a):a2(a)
void f()
void h()
~C()
{cout<<"(14)"<<endl;}
{cout<<"(15)"<<endl;}
{cout<<"(16)"<<endl;}
{f(); cout<<"(17)"<<endl;}
{cout<<"(18)"<<endl;}
};
int main()
{
//----------------------// Part 1:
A* ptr_A = new A;
B* ptr_B = new B;
C* ptr_C = new C(*ptr_A);
//----------------------// Part 2:
ptr_A->g();
ptr_B->g();
ptr_C->g();
//----------------------// Part 3:
A* ptr_AtoB = ptr_B;
A* ptr_AtoC = ptr_C;
B* ptr_BtoC = ptr_C;
ptr_AtoB ->g();
ptr_AtoC ->g();
ptr_BtoC ->g();
//----------------------// Part 4:
B* ptrB = dynamic_cast<B*>(ptr_AtoC);
if(ptrB)
ptrB->i();
else
{
ptr_A->h();
1
12
ptr_A->i();
}
//----------------------// Part 5:
delete ptr_A;
delete ptr_B;
delete ptr_C;
return 0;
}
A class VTABLE
A::i()
A:: g()
~A()
B class VTABLE
B:: f()
B:: g()
B:: i()
B:: h()
~B()
C class VTABLE
C:: f()
C:: h()
B:: i()
B:: g()
~C()
1
12
//----------------------// Part 1:
1,1,8,1,8,2,1,8,15
//----------------------// Part 2:
3,4,3,5,12,10,16,17,10
//----------------------// Part 3:
12,10,16,17,10,16,17,10
//----------------------// Part 4:
16,17,11
//----------------------// Part 5:
6,13,6,18,13,6,6,13,6
class Epoly
{
1
12
?????????
?????????
};
void main()
{
Epoly temp;
temp = Squar;
{
Epoly temPoly=*this;
edgeLong=edgeLong+1;
return temPoly;
}
Epoly Epoly::operator+(int num) const
{
Epoly temPoly=*this;
temPoly.edgeLong=edgeLong+num/numOfEdges;
return temPoly;
}
1
12
#include <iostream>
using namespace std;
void Swap(int& a, int& b)
{
int temp=a;
a=b;
b=temp;
}
void Sort(int* array, int size)
{
for(int i=0; i<size; ++i)
for(int j=i; j<size; ++j)
if(array[j]<array[i])
Swap(array[i],array[j]);
}
1
12
void main()
{
const int size=3;
int* ArrayInt = new int[size];
student * ArrayStudet = new studet[size];
student *sa = new studet(10,"ABC");
student *sb = new studet(10,"ABD");
Studet* sc = new studet(9,"ABD");
ArrayStudet[0]=*sa;
ArrayStudet[1]=*sb;
ArrayStudet[2]=*sc;
for(int i=size-1; i>=0; --i)
ArrayInt[i]=size-i;
Sort<int>(ArrayInt,size);
Print<int>(ArrayInt,size);
Sort<Studet>(ArrayStudet,size);
Print<Studet>(ArrayStudet,size);
delete[] ArrayInt;
delete[] ArrayStudet;
}
1
12
T temp=a;
a=b;
b=temp;
}
template<class T> void Sort(T* array, int size)
{
for(int i=0; i<size; ++i)
for(int j=i; j<size; ++j)
if(array[j]<array[i])
Swap<T>(array[i],array[j]);
}
template<class T> void Print(const T* array, int size)
{
for(int i=0; i<size; ++i)
cout<<array[i];
cout<<endl;
public:
student ();
student(int id,const char* name);
const student& operator=(const student & st)const;
bool operator<(const student & st)const;
friend ostream& operator<<(ostream & out ,const student & st);
#include<iostream>
using namespace std;
class A{
public:
A() {cout <<"1" << f();}
A(int i) { cout <<"1" << i;}
virtual char f() {return 'A';}
};
class B: virtual public A
{
public:
B(int i): A(2){ cout <<"2" << i;}
virtual char f() {return 'B';}
};
class C: virtual public A
{
public:
C(int i): A(i){ cout << "3"<< i;}
virtual char f() {return 'C';}
};
1
12
1
12
1A22334
1
12