News:

Semoga anda berbahagia _/\_

Main Menu

c++ programming

Started by Lex Chan, 03 January 2010, 12:20:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Lex Chan

aye baru belajar OOP pake c++ neh.. :-[

gimana ya bikin class matrix pake constructor dan destructor?
alias pake dynamic memory (ukuran matrix 2 dimensinya bebas)..
"Give the world the best you have and you may get hurt. Give the world your best anyway"
-Mother Teresa-

Lex Chan

intinya sih mau belajar bikin array yang dynamic pake class..

aye googling buat tutorial, ngga ketemu yang bikin array.. :(
"Give the world the best you have and you may get hurt. Give the world your best anyway"
-Mother Teresa-

Sumedho

buat matrix yak, definenya sih begini

int matrix[x][y];

kalau dynamic sih diset waktu constructor aja x,y nya
There is no place like 127.0.0.1

exam



coba cari buku c++ from the ground up (herbert)

tesla

basicnya dynamic array:

int* arr;
arr = new int[5];
delete [] arr;

multi-dimension:

int** arr;

arr* = new int*[5];

arr[0] = new int[3]; // cuma contoh... ntar pake looping aja
arr[1] = new int[4];
arr[2] = new int[6];
arr[3] = new int[7];
arr[4] = new int[2];

// delete
delete [] arr[0];
delete [] arr[1];
delete [] arr[2];
delete [] arr[3];
delete [] arr[4];
delete [] arr;
Lepaskan keserakahan akan kesenangan. Lihatlah bahwa melepaskan dunia adalah kedamaian. Tidak ada sesuatu pun yang perlu kau raup, dan tidak ada satu pun yang perlu kau dorong pergi. ~ Buddha ~

tesla

kalau tujuannya belajar OOP, pake java aja...
lebih enak, ga perlu delete...

syntaxnya hampir sama dg C++


int[][] arr = new int[a][b]; ---> kalau simetris

int[][] arr = new int[][b]; ---> ga simetris
arr[0] = new int[x];
...
arr[b-1] = new int[z];
Lepaskan keserakahan akan kesenangan. Lihatlah bahwa melepaskan dunia adalah kedamaian. Tidak ada sesuatu pun yang perlu kau raup, dan tidak ada satu pun yang perlu kau dorong pergi. ~ Buddha ~

Lex Chan

Quoteclass matrix
{
 private:

 public:
   matrix(int, int);
   ~matrix();
};

matrix::matrix (int row, int col)
{
 int matrix[row][col];
 int i, j;
 for (i=0; i<row; i++)
 {
   for (j=0; j<col; j++)
   {
     cout << "mtx[" << i << "][" << j << "] = ";
     cin >> matrix[ i ][ j ];
   }
 }
};

matrix::~matrix()
{
 delete[] matrix;
};

program ini salahnya di mana? soalnya di-compile, error di bagian yang bold
"Give the world the best you have and you may get hurt. Give the world your best anyway"
-Mother Teresa-

tesla

delete[] matrix;

properties (atau variable dalam class) yg namanya matrix ga ada bro...

di deklarasi classs sama sekali ga ada variable namanya matrix:

class matrix // nama class = matrix
{
  private:

  public:
    matrix(int, int); // constructor
    ~matrix(); // destructor
};


di constructor itu bro deklarasikan sebagai local variable
dipastikan code tsb memory leak ;D


matrix::matrix (int row, int col)
{
  [b]int matrix[row][col][/b]; // ini jadi deklarasi local variable, bukan variable milik class jadi harus di delete dalam scope { } ini juga
  int i, j;
  for (i=0; i<row; i++)
  {
    for (j=0; j<col; j++)
    {
      cout << "mtx[" << i << "][" << j << "] = ";
      cin >> matrix[ i ][ j ];
    }
  }
};
Lepaskan keserakahan akan kesenangan. Lihatlah bahwa melepaskan dunia adalah kedamaian. Tidak ada sesuatu pun yang perlu kau raup, dan tidak ada satu pun yang perlu kau dorong pergi. ~ Buddha ~

tesla

kalau solusi utk non dynamic tambahin di deklarasi  classnya:

class matrix
{
 private:
   int[3][3] data;
 public:
   matrix();
   ~matrix();
};

matrix::matrix ()
{
 int i, j;
 for (i=0; i<3; i++)
 {
   for (j=0; j<3; j++)
   {
     cout << "mtx[" << i << "][" << j << "] = ";
     cin >> data[ i ][ j ];
   }
 }
};

matrix::~matrix()
{
 // ga ada yg perlu di delete bro... sebab ini semua ga pake new
};













ps. code bro sebelumnya ga memory leak. krn tidak ada pake alokasi memory secara manual (new)...
sorry salah judge :P
Lepaskan keserakahan akan kesenangan. Lihatlah bahwa melepaskan dunia adalah kedamaian. Tidak ada sesuatu pun yang perlu kau raup, dan tidak ada satu pun yang perlu kau dorong pergi. ~ Buddha ~

tesla

utk dynamic coba ini:

class matrix {
private:
int** data;
int x;
int y;
public:
matrix(int, int);
~matrix();
void fill();
}

matrix::matrix(int x, int y) {
this.x = x;
this.y = y;
  data = new int*[x];
for (int i = 0; i < x; i++) data[i] = new int[y];
};

matrix::~matrix()
{
if (data != null) { // sorry gw ga tau keyword null di c++ apa... null dipake di java
for (int i = 0; i < x; i++) delete [] data[i];
delete [] data;
}
};

void matrix::fill() { // ga yakin syntaxnya bener... try ur luck
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++) {
// cin >> bla bla bla
}
}
Lepaskan keserakahan akan kesenangan. Lihatlah bahwa melepaskan dunia adalah kedamaian. Tidak ada sesuatu pun yang perlu kau raup, dan tidak ada satu pun yang perlu kau dorong pergi. ~ Buddha ~

Lex Chan

thank you bro.. aye coba utak-atik dulu.. ;D
"Give the world the best you have and you may get hurt. Give the world your best anyway"
-Mother Teresa-

Lex Chan

class matrix
{
  private:
    int** data;
int x, y;
  public:
    matrix(int, int);
    ~matrix();
    void fill(int, int);
    void add(int, int, int**, int**);
};

matrix::matrix(int x, int y)
{
  data = new int *[x];
  for (int i = 0; i < x; i++)
    data[i] = new int[y];
};

matrix::~matrix()
{
  if (data != '\0')
  {
    for (int i = 0; i < x; i++)
      delete[] data[i];
delete[] data;
delete data;
  }
};

void matrix::fill(int x, int y)
{
  for (int i = 0; i < x; i++)
  {
    for (int j = 0; j < y; j++)
    {
  cout << "mtx[" << i << "][" << j << "] = ";
      cin >> data[i][j];
    }
  }
}

void matrix::add(int x, int y, int **P, int **Q)
{
  for (int i = 0; i < x; i++)
  {
    for (int j = 0; j < y; j++)
    {
  data[i][j] = P[i][j] + Q[i][j];
    }
  } 
}

int main ()
{
  int rows, cols;

  cout << "input number of row: ";
  cin >> rows;
  cout << "input number of column: ";
  cin >> cols;
 
  matrix A(rows, cols);
  A.fill(rows, cols);

  matrix B(rows, cols);
  B.fill(rows, cols);
 
  matrix C(rows, cols);
  C.add(rows, cols, A, B);

  return 0;
}


sekarang kendalanya di baris C.add(rows, cols, A, B);..
gimana caranya kalo cuma mau menjumlahkan 2 matrix ke suatu matrix baru?
misalnya matrix C = matrix A + matrix B
"Give the world the best you have and you may get hurt. Give the world your best anyway"
-Mother Teresa-

Sumedho

There is no place like 127.0.0.1

Lex Chan

Quote from: Sumedho on 03 January 2010, 09:35:35 PM
http://www.codeproject.com/KB/architecture/ymatrix.aspx

makasih suhu.. ^:)^

tapi aye perlu belajar dari awal.. mulai dari class dulu.. ;D
jangan langsung main vector, soalnya ilmu aye belom nyampe.. 8)
"Give the world the best you have and you may get hurt. Give the world your best anyway"
-Mother Teresa-