Académique Documents
Professionnel Documents
Culture Documents
------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <conio.h>
class Matrix
{
private:
const short rows;
const short cols;
int *elems;
int a[Linii][Coloane];
public:
Matrix(const short rows, const short cols);
Matrix(const Matrix&);
~Matrix(void){ delete elems; }
int& operator () (const short row, const short col);
Matrix& operator=(const Matrix&);
friend Matrix operator+(Matrix&, Matrix&);
friend Matrix operator-(Matrix&, Matrix&);
friend Matrix operator*(Matrix&, Matrix&);
const short Rows(void) { return rows; }
const short Cols(void) { return cols; }
void citire();
void afisare();
void afisdp();
void afisds();
};
elems[i] = m.elems[i];
else
printf("\nAuto-atribuire!\n");
return *this;
}
m(r, c) = 0.0;
for (int i = 0; i < p.cols; ++i)
m(r, c) += p(r, i) * q(i, c);
}
return m;
}
}
void Matrix::afisare()
{
for (int i = 0; i<rows; i++)
{
void Matrix::citire()
void Matrix::afisdp()
{
void Matrix::afisds()
{
cout << "\n Elementele de pe ds : ";
for (int i = 0; i<rows; i++)
cout << a[i][rows - 1 - i] << " ";
}
void main(void)
{
int i, j;
Matrix m(Linii, Coloane);
m(0, 0) = 10; m(0, 1) = 20; m(0, 2) = 30;
m(1, 0) = 15; m(1, 1) = 25; m(1, 2) = 35;
m.afisare();
class Angajat
{
char nume[256];
float salariu;
public:
Angajat() {}
Angajat(char *n,float s)
{
strcpy(nume,n);
salariu=s;
}
char *get_nume(void)
{
return nume;
}
float get_salariu(void)
{
return salariu;
}
};
class Departament
{
Angajat a[5];
public:
Departament(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4);
void operator[] (const int i);
};
void main()
{
int ind;
_getch();
}//end main
---------------------------------------------------------------------------
/* Overload the new and delete operators for one of the classes
implemented before, in order to
allocate / de- allocate the necessary amount of memory. */
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Angajat
{
char nume[256];
float salariu;
public:
Angajat() {}
Angajat(char *n,float s) { strcpy(nume,n);salariu=s; }
Angajat& operator=(const Angajat&);
char *get_nume(void) { return nume; }
float get_salariu(void) { return salariu; }
};
class Departament
{
Angajat a[5];
public:
Departament(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4);
Departament() {}
void set(Angajat a0,Angajat a1,Angajat a2,Angajat a3,Angajat a4);
void operator[] (const int i);
void *operator new(size_t dim);
void operator delete(void *p);
};
void main()
{
int ind;
Departament d(a0,a1,a2,a3,a4);
Departament *e=new Departament;
cout<<" Index: ";
cin>>ind;
d[ind];
e->set(a0,a1,a2,a3,a4);
e[2];
_getch();
}
-------------------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <conio.h>
using namespace std;
class Obiect
{
public:
int myFunction(int x)
{
return x;
}
};
void main()
{
Obiect ob;
cout<<ob.myFunction(3);
cout<<"\n";
cout<<ob.myFunction(3, 5);
_getch();
}
------------------------------------------------------------------------
/* Write the program that uses a class called Calculator that has as
public methods:
- int calcul(int x) that returns the square value of x;
- int calcul(int x, int y) that returns the product of x and y;
- float calcul(int x, int y, int z) that returns the result of
f(x,y,z) = (x-y)(x+z)/2;
The program receives the parameters from the command line.
Consider the case when all the methods are static. Is it possible to
have in the same time static and non-static public methods? */
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class Calculator
{
public:
int calcul(int x);
int calcul(int x,int y);
float calcul(int x,int y,int z);
};
int Calculator::calcul(int x)
{
return (int)pow((double)x,2);
}
int Calculator::calcul(int x, int y)
{
return x*y;
}
float Calculator::calcul(int x, int y, int z)
{
return (float)(x-y)*(x+z)/2;
}
---------------------------------------------------------------
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
#include<stdio.h>
#include<math.h>
class Complex
{
float r;
float i;
public:
Complex();
Complex(float real);
Complex(float real,float imaginar);
Complex& operator=(const Complex&);
friend Complex operator+(Complex&, Complex&);
friend Complex operator-(Complex&, Complex&);
friend Complex operator*(Complex&, Complex&);
friend Complex operator/(Complex&, Complex&);
float get_modul(Complex);
float get_faza(Complex);
float get_re();
float get_im();
void print();
};
Complex::Complex()
{ r=0; i=0;
}
Complex::Complex(float real)
{
r=real; i=0;
}
Complex::Complex(float real,float imaginar)
{
r=real; i=imaginar;
}
Complex& Complex::operator = (const Complex &c)
{
i=c.i;
r=c.r;
return *this;
}
Complex operator + (Complex &a, Complex &b)
{
Complex rez;
rez.i=a.i+b.i;
rez.r=a.r+b.r;
return rez;
}
Complex operator - (Complex &a, Complex &b)
{
Complex rez;
rez.i=a.i-b.i;
rez.r=a.r-b.r;
return rez;
}
Complex operator * (Complex &a, Complex &b)
{
Complex rez;
rez.i=a.r*b.i+a.i*b.r;
rez.r=a.r*b.r-a.i*b.i;
return rez;
}
Complex operator / (Complex &a, Complex &b)
{
Complex rez;
rez.i=(a.i*b.r-a.r*b.i)/pow(b.r,2)-pow(b.i,2);
rez.r=(a.r*b.r+a.i*b.i)/pow(b.r,2)-pow(b.i,2);
return rez;
}
float Complex::get_modul(Complex c) { return
sqrt(pow(c.r,2)+pow(c.i,2)); }
float Complex::get_faza(Complex c) { return
atan(c.i/c.r); }
float Complex::get_re() { return r; }
float Complex::get_im() { return i; }
void Complex::print() { printf("%.2f+%.2fi",r,i); }
int main(void)
{
Complex a(5,4),b(4,2),s,d,p,i;
s=a+b;
d=a-b;
p=a*b;
i=a/b;
printf("\n a=");a.print();
printf("\n b=");b.print();
printf("\n a+b=");s.print();
printf("\n a-b=");d.print();
printf("\n a*b=");p.print();
printf("\n a/b=");i.print();
_getch();
}
---------------------------------------------------------------------------