清华大学C++课件2

上传人:油条 文档编号:20684212 上传时间:2017-09-11 格式:PDF 页数:51 大小:364.96KB
返回 下载 相关 举报
清华大学C++课件2_第1页
第1页 / 共51页
清华大学C++课件2_第2页
第2页 / 共51页
清华大学C++课件2_第3页
第3页 / 共51页
清华大学C++课件2_第4页
第4页 / 共51页
清华大学C++课件2_第5页
第5页 / 共51页
点击查看更多>>
资源描述

《清华大学C++课件2》由会员分享,可在线阅读,更多相关《清华大学C++课件2(51页珍藏版)》请在金锄头文库上搜索。

1、1计算机程序设计基础 (2)-C+程序设计 (2)孙甲松电子工程系信息认知与智能系统研究所罗姆楼6-104电话: 62796193/139012161802012.3.22.函数重载函数重载 (function overloading) 是一种数据抽象 , 允许函数同名 , 但参数类型和个数不一样 , 许多情况下需要对不同的数据类型作同样的操作 。例如 : 求一个数的绝对值 , 在 C中有 :int abs(int);long labs(long);double fabs(double);分别求 int,long,double类型数据的绝对值 , 如果用错函数 , 往往结果会不正确 。 为避免

2、出现这个问题 , C+允许函数同名 , 这种现象称为函数重载int abs(int);long abs(long);double abs(double);32.1 函数重载实例#include using namespace std;int abs(int i) return iusing namespace std;int abs(int i) return itest.cpp(12) : error C2668: “abs”: 对重载函数的调用不明确1 test.cpp(7): 可能是 “ float abs(float)”1 test.cpp(5): 或 “ long abs(long)

3、”1 c:program filesmicrosoft visual studio 9.0vcincludestdlib.h(380): 或 “ int abs(int)”1 试图匹配参数列表 “ (double)”时1test - 1 个错误 , 0 个警告为什么呢 ?因为 -1.5是默认的 double类型 , 编译器找不到相应double类型的 abs函数 , 除非写成 -1.5f7#include using namespace std;float f(int i) return (float)(i+3); int f(float i) return (int)(i+2); void

4、main( ) float f1=0;for (int i=0; iusing namespace std;double f(double x) return x*x;double f(double x, double y=0) return x*x+y*y;void main( ) double x(3.0), y(4.0);cout test.cpp(6): 可能是 “ double f(double,double)”1 test.cpp(3): 或 “ double f(double)”1 试图匹配参数列表 “ (double)”时1test - 1 个错误 , 0 个警告原因是 :由于

5、函数缺省参数说明 , 导致编译器根据 f(x)调用无法静态束定到底应该调用 f(x) 还是 f(x,0)102.3 函数重载与缺省参数#include using namespace std;double f(double x) return x*x;double f(double x, double y) return x*x+y*y;void main( ) double x(3.0), y(4.0);cout using namespace std;double f(double x, double y=0) return x*x + y*y;void main( ) double x(

6、3.0), y(4.0);cout using namespace std;class Tdate / 可以把成员函数直接在类说明中定义 , 这样 public: / 成员函数若符合条件 , 将自动成为内联函数void Set(int d,int m, int y) day=d; month=m; year = y; int IsLeapYear( ) return(year%4=0 & year%100!=0) | (year%400=0);void Print( ) cout using namespace std;class Myclass public:Myclass(int i);M

7、yclass( );void Print( );private:int member; / 注意 : 这个分号不能少 !Myclass:Myclass(int i) member = i;cout 10constructor called 20Print called = 20destructor called 20destructor called 10243.2 构造函数 /析构函数 ( 续 2)分析 : 在定义 Myclass对象时 , 都赋了初值Myclass Object(10); / Object的初值为 10Myclass Object2(20); / Object2的初值为 2

8、0如果确实想定义对象而不赋初值 , 必须再加一个无参数的构造函数 Myclass( )现在的程序不赋初值是不行的 , 因为构造函数 Myclass要求必须有初值只有对构造函数进行函数重载后 , 定义Myclass对象时 , 才可以赋不赋初值都可以25#include using namespace std;class Myclass public:Myclass(int i);Myclass( );Myclass( ) /注意 : 以上 3个函数都没有返回类型cout 10constructor2 called 0Print called = 0destructor called 0destr

9、uctor called 10273.2 构造函数 /析构函数 ( 续 3)也可以通过把构造函数改写成带缺省参数的函数 , 实现定义对象时赋不赋初值都可以 。 实际上是自动赋初值 !class Myclass public:Myclass(int i=0);Myclass( );void Print( );private:int member;Myclass:Myclass(int i) /注意 :这里不要再写 int i=0,否则编译出错 member = i;cout using namespace std;class Point /Point 类的声明public: /外部接口Point

10、(int xx=0, int yy=0) X=xx;Y=yy; / 构造函数Point(Point &p); / 拷贝构造函数int GetX( ) return X;int GetY( ) return Y;private: /私有成员int X,Y;333.2.2拷贝构造函数(4)Point:Point(Point &p) /拷贝构造函数形参是对象的引用 static int n=0;X=p.X; Y=p.Y;cout#include using namespace std;class Strings public:Strings(char *s);Strings( );void Prin

11、t( );void Set(char *s);private:int length;char *str;38Strings:Strings(char *s) length = strlen(s);str = new charlength+1;strcpy(str, s);cout #include using namespace std;class Strings public:Strings(char *s);Strings( );void Print( );void Set(char *s);private:int length;char str100;43Strings:Strings(

12、char *s) length = strlen(s);if (length #include using namespace std;class Strings public:Strings(char *s);Strings(Strings &p);Strings( );void Print( );void Set(char *s);private:int length;char *str;Strings:Strings( ) delete str;cout 析构函数被调用 ! endl;48Strings:Strings(char *s) length = strlen(s);str =

13、new charlength+1;strcpy(str, s);cout 构造函数被调用 ! endl;Strings:Strings(Strings &p) length = strlen(p.str);str = new charlength+1;strcpy(str, p.str);cout 拷贝构造函数被调用 ! endl;void Strings:Print( ) cout str endl;cout length= length endl;49void Strings:Set(char *s) length = strlen(s);delete str;str = new charlength+1;strcpy(str, s);void main( ) Strings S1(Hello!);Strings S2(S1);S1.Print( );S2.Print( );S1.Set(New String!);S1.Print( );S2.Print( );运行结果是 :50构造函数被调用 !拷贝构造函数被调用 !Hello!length=6Hello!length=6New String!length=11Hello!length=6析构函数被调用 !析构函数被调用 !请按任意键继续 . . .51第 3次作业 :见网络学堂第 3次作业2题必做 , 1题选做

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号