Documente Academic
Documente Profesional
Documente Cultură
1) Fie clasa:
class Complex
{ double re,im;
public:
friend double real(Complex &);
friend double imag(Complex &);
friend Complex conj(Complex &);
1
2) Se dă următorul cod:
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <conio.h>
class stiva_de_int
{
int sp,dim, *vector;
public:
stiva_de_int( );
~stiva_de_int() { if(vector != NULL) delete vector; }
void push(int);
int pop();
int top() { return vector[sp]; }
};
stiva_de_int::stiva_de_int()
{
sp = -1;
const int dim = 10;
vector = new int[dim];
}
2
3) Se dă următorul cod:
#include <iostream>
using namespace std;
class X
{ int a, b;
public:
X(int a1, int b1):a(a1), b(b1) { }
int geta() { return a; }
int getb() { return b; }
X operator+(X &x) {return X(a + x.a, b + x.b); }
};
int main()
{
X x1(4,2), x2(2,3);
X z = x1 + x2;
cout << z.geta() << " " << z.getb();
X *pz = &z;
int i = !(*pz);
cout << " " << i << "\n\n"; return 0;
}
3
4)
Se dă clasa "un_int_mic", cinci variante de supraîncărcare a funcţiei fct(),
numerotate la dreapta, în comentariu şi trei declaraţii de variabile.
Se mai dau şi cinci apeluri de funcţie numerotate şi ele, dar la stânga.
un_int_mic im;
long iM;
char cc;
a1) fct(iM,cc);
a2) fct(iM, im);
a3) fct(im, cc);
a4) fct(cc, cc);
a5) fct(im, iM);
4
5) Se dă următorul program:
#include<iostream>
#include<string.h>
using namespace std;
#pragma warning(disable:4996)
class Student
{
char *nume;
public:
Student(char* n);
Student(const Student&);
~Student();
char* getnume() { return nume;}
Student& operator=(const Student &);
};
Student::Student(char* n)
{
nume = new char[strlen(n)+1];
strcpy(nume,n);
}
Student::Student(const Student& s)
{
nume = new char[strlen(s.nume)+1];
strcpy(nume,s.nume);
}
Student::~Student()
{
if(nume != NULL)
delete nume;
}
Student& Student::operator=(const Student &s)
{
memcpy(nume,s.nume,sizeof(strlen(s.nume)+1));
return *this;
}
int main(void)
{
Student s1("Popescu Ion");
Student s2("Ionescu Mihai");
cout<<s1.getnume()<<endl;
cout<<s2.getnume()<<endl;
s1 = s2;
cout<<s1.getnume()<<endl;
return 0;
}
5
După execuţia programului, pe ecranul monitorului apare:
a) b) c)
Popescu Ion Popescu Ion Popescu Ion
Ionescu Mihai Ionescu Mihai Ionescu Mihai
Ionescu Mihai Ionescu Ion
d)
Nimic, pentru ca exista erori de sintaxă.
class CTestA
{
public:
virtual void f(){cout << "A::f()" << " ";}
};
class CTestB :public CTestA
{
public:
void f() {cout << "B::f()" << " ";}
void g() {cout << "B::g()" << " ";}
};
void main()
{
CTestA *pAa = new CTestA;
CTestA *pAb = new CTestB;
CTestA *pAc = new CTestC;
pAa->f(); pAb->f(); pAb->g();
pAc->f(); pAc->g(); pAc->h();
}