C++ Operators

Switch View :
ScriptFree
Community Content

sayler666
Another example of class with overloaded operators
// wariant_2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<Windows.h>
#include<iostream>

using namespace std;
class coord {
protected:
    double x,y;
public:
    coord() :x(0), y(0){}
    coord(double xx, double yy) : x(xx), y(yy){}
    void disp(){
        cout << " x= " << x << " y =" << y << endl;

    }
    coord operator+ (coord &prawy);
};

coord coord::operator+ (coord &prawy){
    coord tmp;
    tmp.x = this->x + prawy.x;
    tmp.y = this->y + prawy.y;

    return tmp;
}

class Polinom : public coord{
    coord * vert;
    size_t nvert;

public:

    Polinom():nvert(0), vert(NULL){}
    Polinom(size_t novert, coord *v);
    //konstruktor kopii
    Polinom(const Polinom & prawy);
    //operator przeciazenia
    Polinom & operator = (const Polinom & prawy);


    void disp(char *c){
        cout << c << endl;
        int i;
        for(i=0; i< nvert; i++){
            vert[i].disp();
        }
    }
private:
    void my_alloc();

};

Polinom::Polinom(const Polinom & prawy){
    this->nvert=0;
    this->vert = NULL;
    //cout << prawy.getM();
    if(prawy.vert){
        this->nvert = prawy.nvert;
        this->my_alloc();

        memcpy(this->vert, prawy.vert, this->nvert*sizeof(coord));

    }

}

Polinom & Polinom::operator = (const Polinom & prawy){

    if(prawy.vert){
        this->nvert = prawy.nvert;
        this->my_alloc();

        memcpy(this->vert, prawy.vert, this->nvert*sizeof(coord));

    }

    return *this;
}

Polinom::Polinom(size_t novert, coord *v){
    this->nvert = novert;
    this->vert = NULL;

    if(novert){
        my_alloc();
        size_t roz = sizeof(coord);
        memcpy((void *)this->vert, (const void *)v, this->nvert*roz);

    }

}
void Polinom::my_alloc(){
    try{
        if(this->vert){
            delete [] vert;
            vert = NULL;
        }
        this->vert = new coord[this->nvert];
    }catch(bad_alloc){
        exit(1);
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    coord vv[] = {coord(0,0),coord(2,1),coord(1,1)};
    vv[0] = vv[1]+vv[2];

    Polinom p1, p2(3, vv);
    Polinom p3 = p2;
    p1 = p2;
    
    

    p1.disp("p1");
    p2.disp("p2");
    p3.disp("p3");

    system("PAUSE");
    return 0;
}


sayler666
Example of class with overloaded operators
//wariant1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
using namespace std;


class mycl
{
    double *ptr;
public:
    mycl(size_t dm);//konstr ma alokować pamiec dla ptr o rozm dim elementow
    mycl() {ptr=NULL;}
    double & mycl::operator [] (unsigned int ktory);

    //konstruktor kopii
    mycl(mycl & prawy);
    //operator przeciazenia
    mycl & operator = (mycl & prawy);


    size_t get_dim();
private:
    void my_alloc(size_t ndim); // alokuje pamiec dla tablicy ptr o rozm ndim
};

mycl::mycl(mycl & prawy){
    ptr = NULL;
    if(prawy.ptr){
        size_t roz = prawy.get_dim();
        my_alloc(roz);
        memcpy(this->ptr, prawy.ptr, roz*sizeof(double));
    }

}

mycl & mycl::operator = (mycl & prawy){


    if(prawy.ptr){
        my_alloc(prawy.get_dim());
        memcpy(this->ptr, prawy.ptr,prawy.get_dim()*sizeof(double));
    }


    return *this;
}


mycl::mycl(size_t dm)
{
    ptr=NULL;
    my_alloc(dm);
}
double & mycl::operator [] (unsigned int ktory)
{
    return ptr[ktory];
}

void mycl::my_alloc(size_t ndim)
{

    try{
        if(ptr){
            delete [] ptr;
            ptr=NULL;
        }
        ptr=new double [ndim];
    }
    catch(bad_alloc)
    {
        exit(1);
    }
}

size_t mycl::get_dim()
{
    size_t roz = _msize(ptr)/sizeof(double);
    return roz;
}

mycl fun(mycl ob);    

/////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{

    int i,dim;
    cout<<"podaj dim \n";
    cin>> dim;
    mycl ob1(dim), ob2, ob;

    for (i=0; i<dim; i++)
        ob1[i]=(double)(2*i+1);
    
    ob2=ob;
    ob=ob2=ob1;

    cout<<"ob\t  ob2\n";
    for (i=0; i<dim; i++)
        cout<< ob[i] <<"\t"<<ob2[i]<<endl;

    mycl ob3=fun(ob2);
    cout<<"\n\n ob3 \n\n";

    for (i=0; i<dim; i++)
        cout<< ob3[i]<<endl;

    system("pause");
    return 0;
}

mycl fun(mycl ob)
{
    mycl tmp = ob;
    size_t i, dim =ob.get_dim();
    for(i=0; i<dim ; i++){

        tmp[i]=ob[dim-i-1];
    }
    return tmp;
}