Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ how to create double class operator [][] [duplicate]

Tags:

c++

I am building an type to be able to manage matrixes so i have searched how to make an [][] operator but no luck so any idea how to do that i just need a way to make double operator this the class am building

#include<iostream>
#include<conio.h>

using namespace std;

class ddouble{
private:
unsigned short int  x, y;
public:
ddouble();
ddouble(unsigned short int, unsigned short int);
double **M;
void read();
void print();
};

ddouble::ddouble(unsigned short int m, unsigned short int n){
for (int i = 0; i < m; i++){
    M = new (nothrow) double *[i];
    for (int I = 0; I < n; I++){
        M[i] = new (nothrow) double[I];
    }
}
}
void ddouble::read(){
for (int i = 0; i < x; i++){

    cout << "plz enter line \n";

    for (int I = 0; I < y; I++){
        cin >> M[i][I];
    }
}
}
void ddouble::print(){

cout << "i,j\t|\t";

for (int i = 0; i < y; i++){
    cout << i << "\t";
}

cout << endl;

for (int i = 0; i < x; i++){

    cout << i << "\t|\t";

    for (int I = 0; I < y; I++){
        cout << M[i][I] << "\t";
    }

    cout << endl;
}
} 

void main(){

ddouble a(2, 2);
a.read();
a.print();

_getch();
}
like image 977
Karim Avatar asked Jan 02 '15 14:01

Karim


People also ask

Can we overload == operator?

Overloading Binary Operators Suppose that we wish to overload the binary operator == to compare two Point objects. We could do it as a member function or non-member function. To overload as a member function, the declaration is as follows: class Point { public: bool operator==(const Point & rhs) const; // p1.

What is operator double?

operator double() const. Remarks. This method hands the value of the object back to the caller as a double. If the object's current value is not a double, the method attempts to convert the value.

How do you define copy assignment operator?

A copy assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T, T&, const T&, volatile T&, or const volatile T&. For a type to be CopyAssignable, it must have a public copy assignment operator.

Can assignment operator be used instead of copy constructors?

The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.


1 Answers

I would recommend using std::vector<std::vector<double>>. That said, given the structure you have now, you can simply define an operator[] that returns a double *, e.g.:

double * ddouble::operator[](int i) {
    return M[i];
}

You can now access your variable in main as a[i][j].

Also, you are missing a destructor that deallocates the memory; without it you have memory leaks. You should define a destructor as:

void ddouble::~ddouble() {
    for(int I = 0; I < y; I++) {
        delete [] M[I];
    }

    delete [] M;
}
like image 135
wolfPack88 Avatar answered Oct 15 '22 11:10

wolfPack88