ช่วยทำ C++ ข้อนี้ให้สมบูรณ์หน่อยครับ ทำมาหลาย ชม. ละ

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

fujikura
PHP Newbie
PHP Newbie
โพสต์: 3
ลงทะเบียนเมื่อ: 17/12/2011 4:36 pm

ช่วยทำ C++ ข้อนี้ให้สมบูรณ์หน่อยครับ ทำมาหลาย ชม. ละ

โพสต์ที่ยังไม่ได้อ่าน โดย fujikura »

#include"iostream.h"
class Rectangle
{
private:
int H;
int W;
int row;
int col;
int **p;
public:

Rectangle(){row=col=0;}
Rectangle(int r,int c);
void SetValue(int h,int w)
{H=h,W=w;}
int GetH(){return H;}
int GetW(){return W;}
void set(Rectangle *p)
{
for(int h=0;h<row;h++)
for(int y=0;y<col;y++)
p[h][y]=*p;
}


void show(){
for(int a=0;a<row;a++)
for(int b=0;b<col;b++)
cout<<p[a]<<endl;}

};
Rectangle::Rectangle(int r,int c)
{
row=r,col=c;
p=new int *[r];
for(int i=0;i<r;i++)
p=new int[c];
}

void main()
{
Rectangle x;

Rectangle(2,3);
Rectangle *R=new Rectangle;
R->SetValue(2,3);
set(R);
R->show();

}

ผมต้องการสร้าง Object แล้วนำ Object ไปเก็บไว้ใน array 2 มิติ
แล้วเรียกโชว์ ค่าออกมา โดยแต่ละ Object ประกอบไปด้วย Height,Width,Name
fujikura
PHP Newbie
PHP Newbie
โพสต์: 3
ลงทะเบียนเมื่อ: 17/12/2011 4:36 pm

Re: ช่วยทำ C++ ข้อนี้ให้สมบูรณ์หน่อยครับ ทำมาหลาย ชม. ละ

โพสต์ที่ยังไม่ได้อ่าน โดย fujikura »

#include"iostream.h"
#include"string.h"

class Rectangle
{
private:
float Width;
float Height;
char Name[30];
public:
Rectangle()
{
SetObj("",0.0,0.0);
}
Rectangle(char Na[],float H,float W)
{
SetObj(Na,H,W);
}
~Rectangle() {
cout << "Delete object " << Name << "\n\n";
}
void SetObj(char Na[],float H,float W)
{
strcpy(Name,Na);
Height=H;
Width=W;
}
void Set_sub()
{
cout<<"Hello"<<endl;
cout<<"Name:";
cin>>Name;
cout<<"Height:";
cin>>Height;
cout<<"Width:";
cin>>Width;
}
float GetHeight()
{
return Height;
}

float GetWidth()
{
return Width;
}

char *GetName()
{
return Name;
}
void ShowObj()
{
int i=0;
cout<<"Object "<<++i<<" Name: "<<this->GetName()<<endl;
cout<<"Height : "<<this->GetHeight()<<endl;
cout<<"Width : "<<this->GetWidth()<<endl;
}
};
class Sub_Rec
{
int row,col;
char name[30];
Rectangle **z;
public:
Sub_Rec()
{
row=col=0;
strcpy(name,"NULL");
}
Sub_Rec(int r,int c,char *n)
{
row=r;
col=c;
strcpy(name,n);
z = new Rectangle *[row];
for(int i=0;i<row;i++)
z = new Rectangle [col];
}
~Sub_Rec() {
if (row != 0) {
for (int i = 0; i < row; i ++) {
cout << "Delete Object " << name << "[" << i << "][]" << "\n";
delete[] z;
}
cout << "Delete Object " << name << "\n\n";
delete[] z;
}
}
void SetArray(){
for (int i = 0; i < row; i ++)
for (int j = 0; j < col; j ++) {
cout << name << "\n";
z[j].Set_sub();
}
}
void ShowSub() {
cout << name << "\n";
cout << "ROW = " << row << "\n";
cout << "COLUMN = " << col << "\n\n";

for (int i = 0; i < row; i ++) {
for (int j = 0; j < col; j ++) {
cout << name << "[" << i << "][" << j << "]\n";
z[j].ShowObj();
}
}
}
};
void main()
{
{
Sub_Rec x(2,2,"This MyProgram Golf");
x.SetArray();
x.ShowSub();
}
cin.ignore(256, '\n');
cin.get();
}


แก้ได้แล้วครับ ขอบคุณสำหรับทุกคอมเม้นนะครับ
ตอบกลับโพส

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 111