Visual C++ ʵÓÃ½Ì³Ì µÚ4°æ - Ö£°¢Ææ - - ϰÌâ²Î¿¼´ð°¸ ÏÂÔØ±¾ÎÄ

ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2025/7/8 17:06:01ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£

{

char *p1, *p2; public:

void init(char *s1, char *s2); void print()

{ cout<<\ void copy(CSample &one); void free(); };

³ÉÔ±º¯Êýinit()Êǽ«s1ºÍs2ËùÖ¸ÏòµÄ×Ö·û´®·Ö±ðË͵½p1ºÍp2ËùÖ¸ÏòµÄ¶¯Ì¬ÉêÇëµÄÄÚ´æ¿Õ¼äÖУ¬º¯Êýcopy½«¶ÔÏóoneÖеÄÁ½¸ö×Ö·û´®¸´ÖƵ½µ±Ç°µÄ¶ÔÏóÖУ¬free()º¯ÊýÊÍ·Åp1ºÍp2ËùÖ¸ÏòµÄ¶¯Ì¬·ÖÅäµÄÄÚ´æ¿Õ¼ä¡£Éè¼ÆÒ»¸öÍêÕûµÄ³ÌÐò£¬°üÀ¨Íê³ÉÕâ3¸öº¯ÊýµÄ¶¨ÒåºÍ²âÊÔ¹¤×÷¡£

#include #include class CSample {

char *p1, *p2;

public:

void init(char *s1, char *s2);

void print() { cout<<\ } void copy(CSample &one); void free(); };

void CSample::init(char *s1, char *s2) {

p1=new char[strlen(s1)+1]; p2=new char[strlen(s2)+1]; strcpy(p1,s1); strcpy(p2,s2); }

void CSample::copy(CSample &one) {

if (this!=&one ) *this=one; }

void CSample::free() {

delete[] this->p1; delete[] this->p2; }

void main() {

CSample a,b;

a.init(\ a.print(); b.copy(a); b.print(); a.free(); }

±¾³ÌÐòµÄÉè¼ÆÊÇÓÐÎÊÌâµÄ£¬×îºÃÊǰÑnew·ÖÅäÄÚ´æÐ´ÔÚ¹¹Ô캯ÊýÖУ¡Èç´ËÎÞ·¨½øÐÐb.free()!ÒòΪ¶ÔÏóbûÓÐÓÃnew·ÖÅäÄڴ棬Òò´Ë²»¿ÉÒÔÓÃdeleteÔËËã·û¡£

18£®ÉèÓÐÒ»¸öÀ࣬Æä¶¨ÒåÈçÏ£º

#include class CArray

{ int nSizeOfInt; //ÕûÐÍÊý×éµÄ´óС

int nNumOfInt; //ÕûÐÍÊý×éÖÐʵ¼Ê´æ·ÅµÄÔªËØ¸öÊý int nSizeOfFloat; //¸¡µãÊý×éµÄ´óС

int nNumOfFloat; //¸¡µãÊý×éÖÐʵ¼Ê´æ·ÅµÄÔªËØ¸öÊý int *pInt; //Ö¸ÏòÕûÐÍÊý×飬¶¯Ì¬·ÖÅäÄÚ´æ¿Õ¼ä float *pFloat; //Ö¸Ïò¸¡µãÊý×飬¶¯Ì¬·ÖÅäÄÚ´æ¿Õ¼ä public:

CArray(int nIntSize=100, int nFloatSize=200); void put(int n); //½«n¼ÓÈëµ½ÕûÐÍÊý×éÖÐ

void put(float x); //½«x¼ÓÈëµ½¸¡µãÊý×éÖÐ

int getInt(int index); //È¡ÕûÐÍÊý×éÖеÚindex¸öÔªËØ£¬index´Ó0¿ªÊ¼ float getFloat(int index); //È¡¸¡µãÊý×éÖеÚindex¸öÔªËØ£¬index´Ó0¿ªÊ¼ ~CArray(); //Îö¹¹º¯Êý£¬ÊͷŶ¯Ì¬·ÖÅäµÄÄÚ´æ¿Õ¼ä void print(); //·Ö±ðÊä³öÕûÐÍÊý×éºÍ¸¡µãÊý×éÖеÄËùÓÐÔªËØ };¹¹ÔìÍêÕûµÄ³ÌÐò£¬°üÀ¨Àà³ÉÔ±º¯ÊýµÄ¶¨ÒåºÍ²âÊÔ³ÌÐòµÄÉè¼Æ¡£¹¹Ô캯ÊýÖеÄnIntSize ºÍnFloatSize·Ö±ð±íʾÕûÐÍÊý×éºÍ¸¡µãÊý×éµÄ´óС¡£

CArray::CArray(int nIntSize, int nFloatSize) {

nSizeOfInt=nIntSize;

pInt=new int[nSizeOfInt]; nSizeOfFloat=nFloatSize;

pFloat=new float[nSizeOfFloat]; nNumOfInt=0;nNumOfFloat=0; }

void CArray::put(int n) {

pInt[nNumOfInt++]=n; }

void CArray::put(float x) { pFloat[nNumOfFloat++]=x; }

int CArray::getInt(int index) {

if (index>=0 && index<=nNumOfInt)

return pInt[index];

}

float CArray::getFloat(int index) {

if (index>=0 && index<=nNumOfFloat) return pFloat[index]; }

CArray::~CArray() {

delete [] pFloat; delete [] pInt; }

void CArray::print() {

for( int i=0;i

cout<<'\\n';

for( int j=0;j

void main() { int a=10;

float x=10.5;

CArray MyArray(10,15); MyArray.put(a); MyArray.put(x); MyArray.put(a+1); MyArray.put(x+1); MyArray.put(a+2); MyArray.put(x+2); MyArray.print(); }

19£®ÔÚÒ»¸ö³ÌÐòÖУ¬ÊµÏÖÈçÏÂÒªÇó£º (1)¹¹Ô캯ÊýÖØÔØ£»

(2)³ÉÔ±º¯ÊýÉèÖÃĬÈϲÎÊý£»

(5)ʹÓò»Í¬µÄ¹¹Ô캯Êý´´½¨²»Í¬µÄ¶ÔÏó¡£

#include #include class stud{ private:

int num;

char name[10]; char sex; public: stud() { num=10010;

strcpy(name,\ sex='F';

} stud(int n,char nam[ ],char s ) { num=n; strcpy(name, nam) ; sex=s; }

void display()

{ cout<<\ cout<<\ cout<<\ };

void main()

{ stud stud1; stud1.display();

stud stud2(10011,\ stud2.display(); }