04737_C++程序设计_第4章_类和对象

例4.1

描述点的Point类。

例4.2

根据上面对Point类的定义,演示使用Point类的对象。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point//类名Point
 8 {
 9 private://声明为私有访问权限
10     int x, y;//私有数据权限
11 public://声明为公有访问权限
12     void Setxy(int a, int b);//无返回值的公有成员函数
13     void Move(int a, int b);//无返回值的公有成员函数
14     void Display();//无返回值的公有成员函数
15     int Getx();//返回值为int的公有成员函数
16     int Gety();//返回值为int的公有成员函数
17 };//类声明以分号结束
18 
19 void Point::Setxy(int a, int b)
20 {
21     x = a;
22     y = b;
23 }
24 
25 void Point::Move(int a, int b)
26 {
27     x = x + a;
28     y = y + b;
29 }
30 
31 void Point::Display()
32 {
33     cout << x << "," << y << endl;
34 }
35 
36 int Point::Getx()
37 {
38     return x;
39 }
40 
41 int Point::Gety()
42 {
43     return y;
44 }
45 
46 void print(Point a)//使用Point的对象a作为函数参数
47 {
48     a.Display();//显示对象a的数据成员的值
49 }
50 
51 void main()
52 {
53     Point A, B;//声明对象
54     A.Setxy(25, 55);//为对象A赋初值
55     B = A;//B的数据成员取A的数据成员之值
56 
57     A.Display();//显示A的数据成员
58     A.Move(-10, 20);//移动A
59 
60     print(A);//等价于A.Display();
61     print(B);//等价于B.Display();
62 
63     cout << A.Getx() << endl;//只能使用A.Getx(),不能使用A.x
64 }

例4.3

演示使用内联函数定义Point类及使用Point类指针和引用的完整例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>//包含头文件
 4 
 5 using namespace std;//声明命名空间
 6 
 7 class Point//使用内联函数定义类Point
 8 {
 9 private://声明为私有访问权限
10     int x, y;//私有数据权限
11 public://声明为公有访问权限
12     void Setxy(int a, int b)//无返回值的内联公有成员函数
13     {
14         x = a;
15         y = b;
16     }
17     void Move(int a, int b)//无返回值的内联公有成员函数
18     {
19         x = x + a;
20         y = y + b;
21     }
22     void Display()//无返回值的内联公有成员函数
23     {
24         cout << x << "," << y << endl;
25     }
26     int Getx()//返回值为int的内联公有成员函数
27     {
28         return x;
29     }
30     int Gety()//返回值为int的内联公有成员函数
31     {
32         return y;
33     }
34 };//类定义以分号结束
35 
36 void print(Point *a)//类指针作为print函数的参数定义重载函数
37 {
38     a->Display();
39 }
40 
41 void print(Point&a)//类引用作为print函数的参数定义重载函数
42 {
43     a.Display();
44 }
45 
46 void main()//主函数
47 {
48     Point A, B, *p;//声明对象和指针
49     Point &RA = A;//声明对象RA引用对象A
50 
51     A.Setxy(25, 55);//使用成员函数为对象A赋值
52     B = A;//使用赋值运算符为对象B赋值
53     
54     p = &B;//类Point的指针指向对象B
55     p->Setxy(112, 115);//使用指针调用函数Setxy重新设置B的值
56     print(p);//传递指针显示对象B的属性
57     p->Display();//再次显示对象B的属性
58 
59     RA.Move(-80, 23);//引用对象RA调用Move函数
60     
61     print(A);//验证RA和A同步变化
62     print(&A);//直接传递A的地址作为指针参数
63 }

例4.4

构造函数的定义和执行过程实例程序。

例4.5

使用前面定义的Point类演示全局对象的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point//使用内联函数定义类Point
 8 {
 9 private:
10     int x, y;
11 public:
12     Point();//使用参数列表声明不带参数的构造函数
13     Point(int, int);//使用参数列表声明带2个参数的构造函数
14 };
15 
16 Point global(5, 7);
17 
18 Point::Point() :x(0), y(0)//定义不带参数的构造函数
19 {
20     cout << "Initializing default" << endl;
21 }
22 
23 Point::Point(int a, int b) : x(a), y(b)///定义带2个参数的构造函数
24 {
25     cout << "Initializing " << a << "," << b << endl;
26 }
27 
28 void main()
29 {
30     cout << "Entering main and exiting main" << endl;
31 
32     Point A;//使用不带参数的构造函数产生对象A
33     Point B(15, 25);//使用带参数的构造函数产生对象B
34     Point C[2];//使用不带参数的构造函数产生对象数组C
35     Point D[2] = { Point(5,7),Point(8,12) };//使用带参数的构造函数产生对象数组D
36 }

例4.6

使用前面的Point类演示new运算符和构造函数的关系的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point//使用内联函数定义类Point
 8 {
 9 private:
10     int x, y;
11 public:
12     Point();//使用参数列表声明不带参数的构造函数
13     Point(int, int);//使用参数列表声明带2个参数的构造函数
14 };
15 
16 Point::Point() :x(0), y(0)//定义不带参数的构造函数
17 {
18     cout << "Initializing default" << endl;
19 }
20 
21 Point::Point(int a, int b) : x(a), y(b)///定义带2个参数的构造函数
22 {
23     cout << "Initializing " << a << "," << b << endl;
24 }
25 
26 void main()
27 {
28     Point *ptr1 = new Point;
29     Point *ptr2 = new Point(5, 7);
30 
31     delete ptr1;
32     delete ptr2;
33 }

例4.7

设计构造函数的默认参数。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point
 8 {
 9 private:
10     int x, y;
11 public:
12     Point(int = 0, int = 0);//声明两个参数均为默认参数
13 };
14 
15 Point::Point(int a, int b) : x(a), y(b)//定义构造函数
16 {
17     cout << "Initializing " << a << "," << b << endl;
18 }
19 
20 void main()
21 {
22     Point A;//构造函数产生对象A
23     Point B(15, 25);//构造函数产生对象B
24     Point C[2];//构造函数产生对象数组C
25 }

例4.8

使用Point类演示建立和释放一个动态对象数组的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point
 8 {
 9 private:
10     int x, y;
11 public:
12     Point(int = 0, int = 0);//声明两个参数均为默认参数
13     ~Point();//声明析构函数
14 };
15 
16 Point::Point(int a, int b) : x(a), y(b)//定义构造函数
17 {
18     cout << "Initializing " << a << "," << b << endl;
19 }
20 
21 Point::~Point()//定义析构函数
22 {
23     cout << "Destrucor is active" << endl;
24 }
25 
26 void main()
27 {
28     Point *ptr = new Point[2];
29     delete[]ptr;
30 }

例4.9

演示调用构造函数、复制构造函数及析构函数的综合例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point
 8 {
 9 private:
10     int X, Y;
11 public:
12     Point(int a = 0, int b = 0)//构造函数
13     {
14         X = a;
15         Y = b;
16         cout << "Initializing" << endl;
17     }
18     Point(const Point &p);//复制构造函数
19     int GetX()
20     {
21         return X;
22     }
23     int GetY()
24     {
25         return Y;
26     }
27     void Show()
28     {
29         cout << "X=" << X << ",Y=" << Y << endl;
30     }
31     ~Point()
32     {
33         cout << "delete..." << X << "," << Y << endl;
34     }
35 };
36 
37 Point::Point(const Point &p)//定义复制构造函数
38 {
39     X = p.X;
40     Y = p.Y;
41     cout << "Copy Intializing " << endl;
42 }
43 
44 void display(Point p)//Point类的对象作为函数的形参
45 {
46     p.Show();
47 }
48 
49 void disp(Point&p)//Point类对象的引用作为函数的形参
50 {
51     p.Show();
52 }
53 
54 Point fun()//函数的返回值为Point类的对象
55 {
56     Point A(101, 202);
57     return A;
58 }
59 
60 void main()
61 {
62     Point A(42, 35);//对象A
63     //第1次调用复制构造函数
64     Point B(A);//(1)用A初始化B
65     Point C(58, 94);//对象C
66 
67     cout << "called display(B)" << endl;
68     //第2次调用复制构造函数
69     display(B);//(2)对象B作为display的实参
70 
71     cout << "Next..." << endl;
72     cout << "called disp(B)" << endl;
73 
74     disp(B);
75 
76     cout << "call C=fun(C)" << endl;
77     //第3次调用复制构造函数
78     C = fun();//(3)fun的返回值赋给对象C
79 
80     cout << "called disp(C)" << endl;
81 
82     disp(C);
83 
84     cout << "out..." << endl;
85 }

例4.10

构造一个求4个正整数中最大者的类Max,并用主程序验证它的功能。

 1 class Max//声明类
 2 {
 3 private://封装数据成员和成员函数
 4     int a, b, c, d;//数据成员
 5     int Maxi(int, int);//只允许类内部的成员函数调用
 6 public://对外界的接口
 7     void Set(int, int, int, int);//设置对象初值
 8     int Maxi();//求最大值
 9 }A[3];//声明类的对象数组,定义结束
10 //类中成员函数的实现
11 int Max::Maxi(int x, int y)//求两个数的最大值
12 {
13     return (x > y) ? x : y;
14 }
15 
16 void Max::Set(int x1, int x2, int x3 = 0, int x4 = 0)//使用两个默认参数
17 {
18     a = x1;
19     b = x2;
20     c = x3;
21     d = x4;
22 }
23 
24 int Max::Maxi()//求自己类中四个数的最大值
25 {
26     int x = Maxi(a, b);//x和y为Maxi()函数的局部整数对象
27     int y = Maxi(c, d);
28     return Maxi(x, y);
29 }
30 
31 #define _SCL_SECURE_NO_WARNINGS
32 
33 #include <iostream>
34 
35 using namespace std;
36 //主程序
37 void main()
38 {
39     A[0].Set(12, 45, 76, 89);//为数组对象A[0]置初值
40     A[1].Set(12, 45, 76);//为数组对象A[1]置初值
41     A[2].Set(12, 45);//为数组对象A[2]置初值
42 
43     for (int i = 0; i < 3; i++)//输出对象求值结果
44     {
45         cout << A[i].Maxi() << " ";
46     }
47 }

例4.11

使用对象成员的例子。

 1 #define _SCL_SECURE_NO_WARNINGS
 2 
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 class Point//定义点类
 8 {
 9     int x, y;
10 public:
11     void Set(int a, int b)
12     {
13         x = a;
14         y = b;
15     }
16     int Getx()
17     {
18         return x;
19     }
20     int Gety()
21     {
22         return y;
23     }
24 };
25 
26 class Rectangle//在矩形类里使用Point类的成员
27 {
28     Point Loc;//定义一个Point类的对象作为顶点
29     int H, W;//H为高,W为宽
30 public:
31     void Set(int x, int y, int h, int w);
32     Point *GetLoc();//声明返回Point类指针的成员函数
33     int GetHeight()
34     {
35         return H;
36     }
37     int GetWidth()
38     {
39         return W;
40     }
41 };
42 
43 void Rectangle::Set(int x, int y, int h, int w)
44 {
45     Loc.Set(x, y);//初始化坐标顶点
46     H = h;
47     W = w;
48 }
49 
50 Point *Rectangle::GetLoc()//返回类型Point *,作为Rectangle的成员函数
51 {
52     return &Loc;//返回对象Loc的地址
53 }
54 
55 void main()
56 {
57     Rectangle rect;
58 
59     rect.Set(10, 2, 25, 20);
60 
61     cout << rect.GetHeight() << "," << rect.GetWidth() << ",";
62 
63     Point *p = rect.GetLoc();//定义Point类的指针对象p并初始化
64 
65     cout << p->Getx() << "," << p->Gety() << endl;
66 }
原文地址:https://www.cnblogs.com/denggelin/p/5555369.html