Sie sind auf Seite 1von 11

Ex: Overloading operators

/*
program to implement the class 'String'
overload the operator
+ : to concatenate 2 strings
- : to eliminate one string from another
= : to assign 2 strings
*/
#include <iostream>
using namespace std;
class String {
private:
// data
char *p;
public:
// constructor, it reads from cin (or stdin ? :)) )
String();
// constructor 2
String(char *);
// destructor
~String(void);
// operators
String & operator = (String);
friend String operator + (String &, String &);
friend String operator - (String &, String &);

// functions
char *getstr(void){
return p;
}
};
// main
int main(void){
String a,b;
cout<<"a+b (concatenation) result:
"<<(a+b).getstr(); // nice C++ syntax
cout<<"\na-b (elimination) result: "<<(a-b).getstr();
return 0;
}
// constructor, it reads from cin (or stdin ? :)) )
String::String(){
char buf[200];
cout<<"reading a string: "; cin>>buf;
p = new char[ strlen(buf)+1];
strcpy(p,buf);
}
// constructor 2
String::String(char *buf){
p = new char[ strlen(buf)+1];
strcpy(p,buf);

}
// destructor
String::~String(void){
delete [] p;
}
// operators
// =
// NOTE: operator = returns String _&_
String& String::operator = (String x){
if( this == &x)
return *this;
if( p != NULL)
delete [] p;
p = new char[strlen(x.p)+1];
strcpy(p,x.p);
return *this;
}
// +
String operator + (String &a,String &b){
char buf[200];
strcpy(buf,a.p);
strcat(buf,b.p);
return String(buf);
}
// String operator - (String &o, String &x){
char *pp;

pp = strstr(o.p, x.p);
if(pp == NULL)
return String(o.p);
else {
// a little pointer arithmetic here
char buf[200];
strncpy(buf,o.p,pp-o.p);
strcpy(buf+(pp-o.p),pp+strlen(x.p));
return String(buf);
}
}

// Overloading new and delete


#include <stdio.h>
#include <stdlib.h>
class Pozitie{
int x;
int y;
public:
Pozitie(){}

Pozitie(int oriz, int vert){


x = oriz;
y = vert;
}
void arata(){
printf("\nPozitia in plan are coordonatele: %d, %d", x,
y);
}
void *operator new(size_t marime);
void operator delete(void *p);
};
void *Pozitie :: operator new(size_t marime){
printf("\n Supraincarc local operatorul new.");
return malloc(marime);
}
void Pozitie :: operator delete(void *p){
printf("\n Supraincarc local operatorul delete.");
free(p);
}
void main(void){
Pozitie *p1, *p2;
p1 = new Pozitie(100, 200);
if(!p1){
printf("\n Nu am putut aloca memorie pt. p1 ");
exit(0);
}
p2 = new Pozitie(10, 20);

if(!p2){
printf("\n Nu am putut aloca memorie pt. p2 ");
exit(1);
}
p1->arata();
p2->arata();
delete(p1);
delete(p2);
}
Exemple carte 2011:
/* O alta clasa de tip string pentru efectuarea de operatii pe siruri de caractere*/
#include <iostream>
#include <string.h>
using namespace std;
class CString {
char *p;
int len;
public:
CString(char *);
CString();
CString(const CString &);
~CString() {delete []p;}
void show() { cout << p << "\n"; }
CString operator=(CString &);
CString operator+(CString &);
CString operator-(CString &);
int operator==(CString &s) { return !strcmp(p, s.p);}
int operator!=(CString &s) { return strcmp(p, s.p);}
int operator<(CString &s) { return strcmp(p, s.p) < 0;}
int operator<=(CString &s) { return strcmp(p, s.p) <= 0;}
int operator>(CString &s) { return strcmp(p, s.p) > 0;}
int operator>=(CString &s) { return strcmp(p, s.p) >= 0;}
int operator==(char *s) { return !strcmp(p, s);}
int operator!=(char *s) { return strcmp(p, s);}
};
CString::CString()
{
len = 1;
p = new char[len];
if(!p)
{

cout << "eroare de alocare \n";


exit(1);
}
strcpy(p, "");
}
CString::CString(char *s)
{
len = strlen(s) + 1;
p = new char[len];
if(!p)
{
cout << "eroare de alocare \n";
exit(1);
}
strcpy(p, s);
}
CString::CString(const CString &s)
{
len = s.len;
p = new char[len];
if(!p)
{
cout << "eroare de alocare \n";
exit(1);
}
strcpy(p, s.p);
}
CString CString::operator=(CString &s)
{
if( this != &s)
{
if(s.len > len)
{
delete p;
p = new char[s.len];
len = s.len;
if(!p)
{
cout << "eroare de alocare \n";
exit(1);
}
}
strcpy(p, s.p) ;
}
return *this;
}
CString CString::operator+(CString &s)
{
int len;
CString temp;
delete temp.p;
len = strlen(s.p) + strlen(p) + 1;
temp.p = new char[len];
temp.len = len;

if(!temp.p)
{
cout << "eroare de alocare \n";
exit(1);
}
strcpy(temp.p, p);
strcat(temp.p, s.p);
return temp;
}
CString CString::operator-(CString &s)
{
// aici trebuie scris codul ce face supraincarcarea
}
CString dictionar[][2] = {
{"carte", "volum, tom"},
{"magazin", "pravalie, shop, butic"},
{"",""}};
void main(void)
{
char temp[16];
cout << "Introdu un cuvant : ";
cin >> temp;
CString x(temp);
for(int i=0; dictionar[i][0] != ""; i++)
if(dictionar[i][0] == x)
{
cout << "\tSinonime : ";
dictionar[i][1].show();
}
}

/* Implementarea clasei Matrix pt. efectuarea unor operatii cu matrici */


#include <iostream>
using namespace std;
#include <conio.h>
class Matrix {
private:
const short
rows;
const short
cols;
int *elems;
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&);
Matrix operator-(Matrix&);
friend Matrix
operator*(Matrix&, Matrix&);
const short Rows (void){ return rows; }

const short Cols (void) { return cols; }


};
Matrix::Matrix (const short r, const short c) : rows(r), cols(c)
{
if (rows > 0 || cols > 0)
elems = new int[rows * cols];
}
Matrix::Matrix (const Matrix &m) : rows(m.rows), cols(m.cols)
{
int n = rows * cols;
if (rows > 0 || cols > 0)
{
elems = new int[n];
for (register int i = 0; i < n; i++)
elems[i] = m.elems[i];
}
}

int& Matrix::operator () (const short row, const short col)


{
if (row >= 0 && row < rows)
if (col >= 0 && col < cols)
return elems[row*cols + col];
else
{
cout<<"\nIndici eronati\n";
exit(0);
}
}
Matrix& Matrix::operator = (const Matrix &m)
{
if (this != &m)
{
if (rows == m.rows && cols == m.cols)
{
int n = rows * cols;
for (int i = 0; i < n; ++i)
elems[i] = m.elems[i];
}
}
return *this;

}
Matrix operator + (Matrix &p, Matrix &q)
{
if (p.rows == q.rows && p.cols == q.cols)
{
Matrix m(p.rows, p.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < p.cols; ++c)
m(r,c) = p(r,c) + q(r,c);
return m;
}
else
{

cout<<"\n Matricile nu au acelasi numar de linii si coloane! \n";


exit(0);
}
}
//suprancrcarea operatorului - cu metod operator membr
Matrix Matrix::operator-(Matrix& p){
if (p.rows == rows && p.cols == cols)
{
Matrix m(p.rows, p.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < p.cols; ++c)
m(r,c) = (*this)(r,c) -p(r,c);
return m;
}
else
{
cout<<"\n Matricile nu au acelasi numar de linii si coloane! \n";
exit(0);
}
}

/*
Matrix operator - (Matrix &p, Matrix &q)
{
if (p.rows == q.rows && p.cols == q.cols)
{
Matrix m(p.rows, p.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < p.cols; ++c)
m(r,c) = p(r,c) - q(r,c);
return m;
}
else
{
cout<<"\n Matricile nu au acelasi numar de linii si coloane! \n";
exit(0);
}
}
*/
Matrix operator * (Matrix &p, Matrix &q)
{
if (p.cols == q.rows)
{
Matrix m(p.rows, q.cols);
for (register int r = 0; r < p.rows; ++r)
for (register int c = 0; c < q.cols; ++c) {
m(r,c) = 0.0;
for (register int i = 0; i < p.cols; ++i)
m(r,c) += p(r,i) * q(i,c);
}
return m;
}

else
{
cout<<"\n Numar de linii si colane incorect
exit(0);
}
}
void main (void)
{
int i, j;
Matrix m(2,3);
m(0,0) = 10; m(0,1) = 20;
m(1,0) = 15; m(1,1) = 25;

m(0,2) = 30;
m(1,2) = 35;

for(i=0;i<2;i++) {
cout<<endl;
for(j=0;j<3;j++)
cout<<m(i,j)<<"\t";
}
Matrix n=m;
cout<<endl;
for(i=0;i<2;i++) {
cout<<endl;
for(j=0;j<3;j++)
cout<<n(i,j)<<"\t";
}
Matrix dif =n-m;
cout<<endl;
for(i=0;i<2;i++) {
cout<<endl;
for(j=0;j<3;j++)
cout<<dif(i,j)<<"\t";
}
_getch();
}

pentru inmultire ! \n";

Das könnte Ihnen auch gefallen