16.使用this指针复制数据.
头文件:
1 #include2 3 using namespace std; 4 5 class Obj 6 { 7 int a, b; 8 9 public:10 Obj(int x = 0, int y = 0)11 {12 a = x;13 b = y;14 }15 16 void copy(Obj &);17 void display()18 {19 cout << "a = " << a << ", b = " << b << endl;20 }21 };22 23 void Obj::copy(Obj &aObj)24 {25 if (this == &aObj)26 return;27 this->a = aObj.a;28 this->b = aObj.b;29 }
源文件:
1 #include "obj.h" 2 3 void main(void) 4 { 5 Obj x1(22, 25), x2(33, 46); 6 7 cout << "x1: "; 8 x1.display(); 9 cout << "x2: ";10 x2.display();11 x1.copy(x2);12 cout << "x1: ";13 x1.display();14 }
结果:
x1: a = 22, b = 25
x2: a = 33, b = 46
x1: a = 33, b = 46
Mark:
当一个对象调用成员函数时,系统先将该对象的地址赋给this指针,然后调用成员函数,成员函数对成员数据进行操作时,隐含使用了this指针.
17.完整实现str类的例子.
头文件:
1 #ifndef _STR_H 2 #define _STR_H 3 4 #include5 #include 6 7 using namespace std; 8 9 class str10 {11 private:12 char *st;13 14 public:15 str(char *s);16 str(str &s);17 str & operator = (str &a);18 str & operator = (char *s);19 void print()20 {21 cout << st << endl;22 }23 24 ~str()25 {26 delete st;27 }28 };29 30 str::str(char *s)31 {32 st = new char[strlen(s) + 1];33 strcpy(st, s);34 }35 36 str::str(str &a)37 {38 st = new char[strlen(a.st) + 1];39 strcpy(st, a.st);40 }41 42 str & str::operator = (str &a)43 {44 if (this == &a)45 return *this;46 delete st;47 st = new char[strlen(a.st) + 1];48 strcpy(st, a.st);49 return *this;50 }51 52 str & str::operator = (char *s)53 {54 delete st;55 st = new char[strlen(s) + 1];56 strcpy(st, s);57 return *this;58 }59 60 #endif
源文件:
1 #include "str.h" 2 3 void main(void) 4 { 5 str s1("We"), s2("They"), s3(s1); 6 7 s1.print(); 8 s2.print(); 9 s3.print();10 11 s2 = s1 = s3;12 s3 = "Go home!";13 s3 = s3;14 s1.print();15 s2.print();16 s3.print();17 }
结果:
We
They
We
We
We
Go home!
18.求4个数的最大值的类模板程序.
头文件:
1 #ifndef _MAX4_H 2 #define _MAX4_H 3 4 #include5 6 using namespace std; 7 8 template 9 class Max410 {11 T a, b, c, d;12 T Max(T a, T b)13 {14 return (a > b) ? a : b;15 }16 17 public:18 Max4(T, T, T, T);19 T Max(void);20 };21 22 template 23 Max4 ::Max4(T x1, T x2, T x3, T x4):a(x1),b(x2),c(x3),d(x4) 24 {25 }26 27 template 28 T Max4 ::Max(void)29 {30 return Max(Max(a, b), Max(c, d));31 }32 33 #endif
源文件:
1 #include "max4.h"2 3 void main(void)4 {5 Max4C('W', 'w', 'a', 'A');6 Max4 A(-25, -67, -66, -256);7 Max4 B(1.25, 4.3, -8.6, 3.5);8 cout << C.Max() << " " << A.Max() << " " << B.Max() << endl;9 }
结果:
w -25 4.3
19.演示对4个数字求和的类模板程序.
头文件:
1 #ifndef _SUM_H 2 #define _SUM_H 3 4 template5 class Sum 6 { 7 private: 8 T m[size]; 9 10 public:11 Sum(T a, T b, T c, T d)12 {13 m[0] = a;14 m[1] = b;15 m[2] = c;16 m[3] = d;17 }18 19 T S()20 {21 return m[0] + m[1] + m[2] + m[3];22 }23 };24 25 #endif
源文件:
1 #include2 #include "sum.h" 3 4 using namespace std; 5 6 void main(void) 7 { 8 Sum num1(-23, 5, 8, -2); 9 Sum f1(3.5f, -8.5f, 8.8f, 9.7f);10 Sum d1(355.4, 253.8, 456.7, -67.8);11 Sum c1('W', -2, -1, -1); //字符减,等效于'W','U','T','S'12 cout << num1.S() << ", " << f1.S() << ", " << d1.S() << ", " << c1.S() << endl;13 }
结果:
-12, 13.5, 998.1, S
20.使用缺省内联函数实现单一继承
头文件:
1 #include2 3 using namespace std; 4 5 class Point 6 { 7 private: 8 int X, Y; 9 10 public:11 Point(int a, int b)12 {13 X = a;14 Y = b;15 cout << "Point..." << endl;16 }17 18 void Showxy()19 {20 cout << "X = " << X << ", Y = " << Y << endl;21 }22 23 ~Point()24 {25 cout << "Delete Point" << endl;26 }27 };28 29 class Rectangle:public Point30 {31 private:32 int H, W;33 34 public:35 Rectangle(int a, int b, int h, int w):Point(a, b)36 {37 H = h;38 W = w;39 cout << "Rectangle..." << endl;40 }41 42 void Show()43 {44 cout << "H = " << H << ", W = " << W << endl;45 }46 47 ~Rectangle()48 {49 cout << "Delete Rectangle" << endl;50 }51 };
源文件:
1 #include "rectangle.h"2 3 void main(void)4 {5 Rectangle r1(3, 4, 5, 6);6 r1.Showxy();7 r1.Show();8 }
结果:
Point...
Rectangle...
X = 3, Y = 4
H = 5, W = 6
Delete Rectangle
Delete Point
Mark:
当定义派生类的一个对象时,首先调用基类的构造函数,对基类成员进行初始化,然后执行派生类的构造函数,如果某个基类仍是一个派生类,则这个过程递归进行.