×Ô¿¼C++ÊÔÌâÓë´ð°¸(9Ì׺ϼ¯)-3-4. ÏÂÔر¾ÎÄ

ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2025/3/23 19:42:36ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£

---

ËÄ¡¢Íê³É³ÌÐòÌâ(±¾´óÌâ¹²5СÌ⣬ÿСÌâ4·Ö£¬¹²20·Ö)

1. ÔÚÏÂÃæ³ÌÐòºáÏß´¦ÌîÉÏÊʵ±ÄÚÈÝ£¬Ê¹³ÌÐòÖ´Ðнá¹ûΪ£º\¡£

#include #include using namespace std; class mystring {public:

char * pdata; mystring(int len)

{pdata=new char£Ûlen+1£Ý; }

~mystring() {delete pdata;}

void show(){cout<

void fun(mystring** array,int len) {mystring*old=*array;

_*array=new mystring(len)______; memcpy(*array, old, len); }

void main()

{mystring str(20); mystring*pstr=&str; mystring**ppstr=&pstr;

strcpy(str.pdata,\fun(ppstr, 20); str.show();_______ }

2. ÔÚÏÂÃæ³ÌÐòºáÏß´¦ÌîÉÏÊʵ±×־䣬Íê³ÉÀàµÄ¶¨Òå¡£

class line; class box { private: int color; int upx, upy; int lowx, lowy; public:

friend int same_color(line l, box b); void set_color (int c){color=c;}

void define_box (int x1, int y1, int x2, int y2) {upx=x1;upy=y1;lowx=x2;lowy=y2;} };

class line { private:

----

---

int color;

int startx, starty; int endx, endy; public:

friend int same_color(line l,box b); void set_color (int c) {color=c;}

void define_line (int x1,int y1,int x2,int y2) {startx=x1;starty=y1;endx=x2;endy=y2;} };

int same_color(line l, box b) {if (l.color==b.color) return 1; return 0; }

3. ÏÂÃæ³ÌÐòÓÃÀ´ÇóÖ±½ÇÈý½ÇÐÎб±ß³¤¶È¡£

#include #include class Point {private: double x,y;

friend Line;__________ public:

Point(double i=0,double j=0) {x=i;y=j;}

Point(Point &p) {x=p.x;y=p.y;} };

class Line {private: Point p1,p2; public:

Line(Point &xp1,Point &xp2):p1(xp1),p2(xp2)________{} double GetLength(); };

double Line::GetLength() {double dx=p2.x-p1.x; double dy=p2.y-p1.y;

return sqrt(dx*dx+dy*dy); }

void main()

{ Point p1,p2(6,8); Line L1(p1,p2);

cout<

----

---

4. ÔÚÏÂÃæ³ÌÐòµÄµ×»­Ïß´¦ÌîÉÏÊʵ±µÄ×־䣬ʹ¸Ã³ÌÐòÖ´Ðнá¹ûΪ40¡£

#include class Test { public:

static int x______; Test (int i=0) {x=i+x;} int Getnum()

{return Test::x+7;} };

int Test::x=33_______; void main() {Test test;

cout<

5. ÔÚÏÂÁгÌÐòµÄ¿Õ¸ñ´¦ÌîÉÏÊʵ±µÄ×־䣬ʹÊä³öΪ£º0£¬2£¬10¡£

#include #include class Magic {double x; public:

Magic(double d=0.00):x(fabs(d)) {}

Magic operator+(Magic &c______) {

return Magic(sqrt(x*x+c.x*c.x)); }

friend ostream& operator<<(ostream & stream,Magic & c) { stream<

void main() {Magic ma;

cout<

Îå¡¢³ÌÐò·ÖÎöÌâ(±¾´óÌâ¹²2СÌ⣬ÿСÌâ5·Ö,¹²10·Ö)

----

---

1. ÔËÐгÌÐò£¬Ð´³ö³ÌÐòÖ´ÐеĽá¹û¡£

#include void main() {int a,b,c; char ch;

cin>>a>>ch>>b>>c;//´Ó¼üÅÌÉÏÊäÈë1.5¡Ác¡Á10¡Á20,¡Á±íʾһ¸ö¿Õ¸ñ cout<

0

2. ¸ø³öÏÂÃæ³ÌÐòÊä³ö½á¹û¡£

#include class A {public: A()

{cout<<\virtual ~A()

{cout<<\virtual void f()

{cout<<\void g() {f();} };

class B:public A {public: B()

{f();cout<<\~B()

{cout<<\};

class C:public B {public: C()

{cout<<\~C()

{cout<<\void f()

{cout<<\};

void main() {A *a=new C; a->g(); delete a; }

----

---

As cons. As f(). Bs cons. Cs cons. Cs f(). Cs des. Bs des. As des.

Áù¡¢³ÌÐòÉè¼ÆÌ⣨±¾´óÌâ¹²1СÌ⣬¹²10·Ö£© 1. ÒÑÖª½»Í¨¹¤¾ßÀඨÒåÈçÏ¡£

ÒªÇ󣺣¨1)ʵÏÖÕâ¸öÀࣻ£¨2£©¶¨Ò岢ʵÏÖÒ»¸öС³µÀàcar£¬ÊÇËüµÄ¹«ÓÐÅÉÉúÀ࣬С³µ±¾ÉíµÄ˽ÓÐ ÊôÐÔÓÐÔØÈËÊý£¬Ð¡³µµÄº¯ÊýÓÐinit(ÉèÖóµÂÖÊý£¬ÖØÁ¿ºÍÔØÈËÊý£©£¬getpassenger(»ñÈ¡ÔØÈËÊý )£¬print£¨´òÓ¡³µÂÖÊý£¬ÖØÁ¿ºÍÔØÈËÊý£©¡£ class vehicle {protected:

int wheels;//³µÂÖÊý float weight;//ÖØÁ¿ public:

void init(int wheels,float weight); int get_wheels(); float get_weight(); void print(); };

void vehicle::init(int wheels,float weight) {this->wheels=wheels; this->weight=weight; cout<

int vehicle::get_wheels() {return wheels; }

float vehicle::get_weight() {return weight;}

void vehicle::print()

{cout<<\³µÂÖÊý£º\ÖØÁ¿£º\

----