码迷,mamicode.com
首页 > 编程语言 > 详细

C++专题(二)

时间:2015-12-11 06:50:44      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

10.分析下列程序的执行结果.

头文件:

技术分享
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Point
 6 {
 7     float x, y;
 8 
 9 public:
10     Point()
11     {
12         x = 0;
13         y = 0;
14         cout << "执行Point()构造函数" << endl;
15     }
16 
17     Point(float a, float b)
18     {
19         x = a;
20         y = b;
21         cout << "执行Point(float a, float b)构造函数" << endl;
22     }
23 
24     ~Point()
25     {
26         cout << ( << x << , << y << ")执行析构函数" << endl;
27     }
28 
29     void Set(float a = 0, float b = 0)
30     {
31         x = a;
32         y = b;
33     }
34 
35     void Display()
36     {
37         cout << "点的位置是:(" << x << , << y << ")" << endl;
38     }
39 };
point.h

源文件:

技术分享
 1 #include "point.h"
 2 
 3 void main (void)
 4 {
 5     Point a, b(3, 5);
 6     a.Display();
 7     b.Display();
 8     a.Set(6, 8);
 9     b.Set(12.5, 25.37);
10     a.Display();
11     b.Display();
12 }
View Code

结果:

执行Point()构造函数

执行Point(float a, float b)构造函数

点的位置是:(0,0)

点的位置是:(3,5)

点的位置是:(6,8)

点的位置是:(12.5,25.37)

(12.5,25.37)执行析构函数

(6,8)执行析构函数

 

11.分析下列程序的执行结果:

技术分享
 1 #include "point.h"
 2 
 3 void main (void)
 4 {
 5     Point a(4,6);
 6     a.Display();
 7     {
 8         cout << "进入了子程序块\n";
 9         Point b(3,5);
10         a.Display();
11         b.Display();
12     }
13     cout << "退出了子程序块\n";
14     a.Display();
15     //b.Display(); 若加了这一句,则会显示出错
16 }
View Code

结果:

执行Point(float a, float b)构造函数

点的位置是:(4,6)

进入了子程序块

执行Point(float a, float b)构造函数

点的位置是:(4,6)

点的位置是:(3,5)

(3,5)执行析构函数

退出了子程序块

点的位置是:(4,6)

(4,6)执行析构函数

 

  执行结果显示,对象a的作用域可以进花括号,但对象b的作用域不能出花括号,对象b在右花括号处就执行了析构函数,然后消失.

 

12.分析下列程序的执行结果.

头文件:

技术分享
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Point1
 6 {
 7     float x, y;
 8 
 9 public:
10     Point1();
11     Point1(float a, float b);
12     Point1(Point1 &obj);
13     ~Point1()
14     {
15         cout << ( << x << , << y << ")执行析构函数" << endl;
16     }
17 
18     void Set(float a = 0, float b = 0)
19     {
20         x = a;
21         y = b;
22     }
23 
24     void Display()
25     {
26         cout << "点的位置是:(" << x << , << y << ")\n";
27     }
28 };
29 
30 Point1::Point1()
31 {
32     x = 0;
33     y = 0;
34     cout << "执行Point1()构造函数" << endl;
35 }
36 
37 Point1::Point1(float a, float b)
38 {
39     x = a;
40     y = b;
41     cout << "执行Point1(float a, float b)构造函数" << endl;
42 }
43 
44 Point1::Point1(Point1 &obj)
45 {
46     x = obj.x;
47     y = obj.y;
48     cout << "执行Point1(Point1 &obj)构造函数" << endl;
49 }
point1.h

源文件:

技术分享
 1 #include "point1.h"
 2 
 3 void main (void)
 4 {
 5     Point1 a(53, 24);
 6     a.Display();
 7     Point1 b(a);
 8     b.Display();
 9     a.Set(16, 28);
10     Point1 c(a);
11     c.Display();
12     c.Set(3.14, 6.28);
13     c.Display();
14     cout << "现在a";
15     a.Display();
16     cout << "现在b";
17     b.Display();
18     cout << "现在c";
19     c.Display();
20 }
View Code

结果:

执行Point1(float a, float b)构造函数

点的位置是:(53,24)

执行Point1(Point1 &obj)构造函数

点的位置是:(53,24)

执行Point1(Point1 &obj)构造函数

点的位置是(16,28)

点的位置是(3.14,6.28)

现在a点的位置是:(16,28)

现在b点的位置是:(53,24)

现在c点的位置是:(3.14,6.28)

(3.14,6.28)执行析构函数

(53,24)执行析构函数

(16,28)执行析构函数

 

Mark:

  (1)复制构造函数的一般形式为:<类名>(<类名> &<对象名>);

  (2)应用复制构造函数定义新对象格式为:

  <类名> <新对象名>(老对象名)  或  <类名> <新对象名> = <老对象名>,上面Point b(a)可写成 Point b = a.

 

13.利用point1.h为头文件,定义函数和主程序如下:

技术分享
 1 #include "point1.h"
 2 
 3 void f1(Point1 x)
 4 {
 5     cout << "f1函数在运行" << endl;
 6     x.Display();
 7     cout << "f1函数结束运行" << endl;
 8 }
 9 
10 void main (void)
11 {
12     Point1 a(3, 6.5);
13     cout << "准备运行f1函数" << endl;
14     f1(a);
15     cout << "回到了主程序" << endl;
16 }
View Code

结果:

执行Point1(float a, float b)构造函数

准备运行f1函数

执行Point1(Point1 &obj)构造函数

f1函数在运行

点的位置是:(3,6.5)

f1函数结束运行

(3,6.5)执行析构函数

回到了主程序

(3,6.5)执行析构函数

 

  若在void f1(Point1 x)改为void f1(Point1 &x),即:

技术分享
 1 #include "point1.h"
 2 
 3 void f1(Point1 &x)
 4 {
 5     cout << "f1函数在运行" << endl;
 6     x.Display();
 7     cout << "f1函数结束运行" << endl;
 8 }
 9 
10 void main (void)
11 {
12     Point1 a(3, 6.5);
13     cout << "准备运行f1函数" << endl;
14     f1(a);
15     cout << "回到了主程序" << endl;
16 }
View Code

结果:

执行Point1(float a, float b)构造函数

准备运行f1函数

f1函数在运行

点的位置是:(3,6.5)

f1函数结束运行

回到了主程序

(3,6.5)执行析构函数

 

Mark:

  在用对象作为参数时,应该养成使用引用的习惯,这是一种好的C++程序设计风格.

 

14.利用point1.h为头文件,分析下列程序的功能.

技术分享
 1 #include "point1.h"
 2 
 3 Point1 f2(void)
 4 {
 5     Point1 x(10, 20);
 6     return x;
 7 }
 8 
 9 void main (void)
10 {
11     cout << "开始执行f2函数" << endl;
12     f2();
13     cout << "f2函数运行结束" << endl;
14 }
View Code

结果:

开始执行f2函数

执行Point1(float a, float b)构造函数

执行Point1(Point1 &obj)构造函数

(10,20)执行析构函数

(10,20)执行析构函数

f2函数运行结束

  这说明f2()中定义的对象x,在执行返回操作时,要先复制构造函数建立一个临时对象,然后再调用析构函数消失,上面的运行结果第一个"(10,20)执行析构函数"是对象x执行的,第二个"(10,20)执行析构函数“是临时对象执行的.

 

15.对象指针应用,定义Location类表示点.

头文件:

技术分享
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Location
 6 {
 7     float x, y;
 8 
 9 public:
10     Location(float a = 0, float b = 0)
11     {
12         x = a;
13         y = b;
14     }
15 
16     float getx()
17     {
18         return x;
19     }
20 
21     float gety()
22     {
23         return y;
24     }
25 
26     void print()
27     {
28         cout << "(X, Y) = (" << x << ", " << y << ")\n";
29     }
30 };
location.h

源文件:

技术分享
 1 #include "location.h"
 2 
 3 void main(void)
 4 {
 5     Location A(115.5, 38.75);
 6     Location *ptr;
 7     float x, y;
 8 
 9     ptr = &A;
10     x = ptr->getx();
11     y = ptr->gety();
12     cout << "X = " << x << ", Y = " << y << endl;
13     ptr->print();
14 }
View Code

结果:

X = 115.5, Y = 38.75

(X, Y) = (115.5, 38.75)

 

C++专题(二)

标签:

原文地址:http://www.cnblogs.com/zero-jh/p/5037863.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!