Mình có bài này :
PHP Code:
#include <iostream>
#include <cstdlib>
#include <new>
#include <iomanip>
#define MAX 1000
class Square
{
public :
Square(); /*Constructor*/
~Square(); /*Destructor*/
Square( Square &T ); /*Copy constructor*/
void Display(int, int);
void Output_Data();
void Initialization();
void Dynamic_Programming( int**, int**, int**);
void Cut_Process(int, int);
private :
int **Rectangle;
int **Dx, **Dy;
int length, width;
int _cP; /*cutting point*/
};
Square::Square()
{
cout << "Enter the Length and Width : ";
cin >> length >> width;
Rectangle = new int *[length+1];
Dy = new int *[length+1];
Dx = new int *[length+1];
for( int x = 0; x <= length; x++){
Rectangle[x] = new int[width+1];
Dx[x] = new int[width+1];
Dy[x] = new int[width+1];
}
Display (length+1, width+1);
Initialization();
Dynamic_Programming ( Rectangle, Dx, Dy);
Output_Data();
}
/*Destructor*/
Square::~Square()
{
for( int x = 0; x <= length; x++){
delete[] Rectangle[x];
delete[] Dx[x];
delete[] Dy[x];
}
delete[] Rectangle;
delete[] Dx;
delete[] Dy;
}
void Square::Dynamic_Programming( int **Rectangle, int **Dx, int **Dy)
{
/*Matrix solution*/
int xPos,
yPos;
xPos = 1;
do
{
yPos = 1;
do
{
/*Hyposthesis Dynamic Programming*/
if ( xPos == yPos )
{
Rectangle[xPos][yPos] = 1;
}
else
{
Rectangle[xPos][yPos] = MAX;
for ( _cP = 1; _cP <= xPos-1; _cP++ )
{
if ( Rectangle[xPos][yPos] > Rectangle[_cP][yPos] + Rectangle[xPos-_cP][yPos] )
{
Rectangle[xPos][yPos] = Rectangle[_cP][yPos] + Rectangle[xPos-_cP][yPos];
Dx[xPos][yPos] = _cP;
Dy[xPos][yPos] = 0;
}
}
for ( _cP = 1; _cP <= yPos-1; _cP++ )
{
if ( Rectangle[xPos][yPos] > Rectangle[xPos][_cP] + Rectangle[xPos][yPos-_cP] )
{
Rectangle[xPos][yPos] = Rectangle[xPos][_cP] + Rectangle[xPos][yPos-_cP];
Dy[xPos][yPos] = _cP;
Dx[xPos][yPos] = 0;
}
}
}
yPos++;
}
while ( yPos <= width);
xPos++;
}
while ( xPos <= length );
}
void Square::Display( int xRow, int yCol)
{
for( int i = 0; i <= xRow; i++){
for (int x = 1; x <= yCol*5; x++ ){
cout << '-';
}
cout << "\n";
for( int j = 1; j <= yCol; j++){
cout << "|";
cout.width(5);
}
cout << "|";
cout << "\n";
}
for ( int x = 1; x < yCol*5 +1; x++ ){
cout << '-';
}
cout << "\n\n";
}
/*Constructor*/
void Square::Initialization()
{
for( int x = 0; x <= length; x++)
{
for ( int y = 0; y <= width; y++ )
{
Rectangle[x][y] = 0;
Dx[x][y] = 0;
Dy[x][y] = 0;
}
}
}
void Square::Cut_Process(int xPos, int yPos)
{
if ( xPos == yPos )
{
cout << xPos << " x " << yPos;
cout << "\n\n";
Display (xPos, yPos);
cout << endl;
}
else
{
if ( Dx[xPos][yPos] != 0 )
{
Cut_Process(Dx[xPos][yPos], yPos);
Cut_Process(xPos - Dx[xPos][yPos], yPos);
}
else
{
Cut_Process(xPos, Dy[xPos][yPos]);
Cut_Process(xPos, yPos - Dy[xPos][yPos]);
}
}
}
void Square::Output_Data()
{
cout << "Minimum square : " << Rectangle[length][width];
cout << "\n\n\n";
Cut_Process( length, width);
}
int main(void)
{
Square Object;
return 0;
}
Nếu mình đặt cái lời gọi hàm vào constructor như trên :
Code:
Square::Square()
{
cout << "Enter the Length and Width : ";
cin >> length >> width;
Rectangle = new int *[length+1];
Dy = new int *[length+1];
Dx = new int *[length+1];
for( int x = 0; x <= length; x++){
Rectangle[x] = new int[width+1];
Dx[x] = new int[width+1];
Dy[x] = new int[width+1];
}
Display (length+1, width+1);
Initialization();
Dynamic_Programming ( Rectangle, Dx, Dy);
Output_Data();
}
thì được nhưng cho mình hỏi nếu mình muốn tạo 1 đối tượng và dùng đối tượng này để gọi tới các hàm thành phần trong main thì mình phải làm sao ? Có phải mình cần dùng copy constructor không ?
Ví dụ nếu mình gọi hàm như sau :
Code:
Square Obj;
Obj.Dynamic_Programming ( Rectangle, Dx, Dy );
thì nó không cho phép ? Mình phải làm sao để có thể gọi các hàm thành phần khi thành phần dữ liệu của nó có con trỏ và dữ liệu đc cấp phát trên Heap.
Các bạn giúp mình nhé ! Thanks !