面向对象程序设计一深入类与对象_第1页
面向对象程序设计一深入类与对象_第2页
面向对象程序设计一深入类与对象_第3页
面向对象程序设计一深入类与对象_第4页
面向对象程序设计一深入类与对象_第5页
已阅读5页,还剩41页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、深入类与对象内存布局&例:Point类 class Point public:Point(int newX = 0, int newY = 0); void SetX(int newX); void SetY(int newY); int GetX(); int GetY(); private: int m_iX; int m_iY; ;Point pt;cout sizeof(pt);J结果为:8深入类与对象内存布局Point对象的内存布局每一个对象的内存布局相同深入类与对象this指针&例:再以Point类为例 void main() Point pt3; for(int

2、id = 0; id m_iX = newX; 深入类与对象this指针!this指针:隐藏的隐藏的 Point pt1(3, 5), pt2(1, 4); Point:Point(int newX, int newY) m_iX = newX; m_iY = newY; this-m_iX = newX; this-m_iY = newY; pt1.m_iX = newX; pt1.m_iY = newY; pt2.m_iX = newX; pt2.m_iY = newY; 深入类与对象this指针&思考:希望能对Point加入一个行为:移动。 class Point /其它同原类

3、public: void MoveTo(int newX, int newY) m_iX = newX; m_iY = newY; cout “Point move to” newX “,” newY m_iX = this-m_iY = this实际指向的是pt1深入类与对象拷贝构造函数!拷贝构造函数(Copy Constructor)的实质: !拷贝构造函数的形式:!拷贝构造函数的调用: 深入类与对象拷贝构造函数例:为建立好的Rectangle类添加拷贝构造函数 class Rectangle public: Rectangle(Rectangle& rect); /其它同原类 ;

4、 Rectangle:Rectangle(Rectangle& rect) m_fLength = rect.m_fLength; m_fWidth = rect.m_fWidth; 深入类与对象Const成员例1:为Round类加入一个成员pi,并限定其值为3.14。 class Round public: Round(float _radius); /同原Round类 private: float m_fRadius; float m_fPi; ;Round:Round(float _radius) m_fRadius = _radius; m_fPi = 3.14const/在某

5、个Round方法中 /oops! m_fPi = 3.2;深入类与对象Const成员Class Round public: Round(float _radius);private: float m_fRadius; const float m_fPi;Round:Round(float _radius) : m_fPi(3.14) m_fRadius = _radius ;const成员变量深入类与对象Const成员例2:考虑Round类中的这样一个方法 float Round:GetRadius() return m_fRadius; 此方法的目的仅仅是获取半径m_fRadius *= 2

6、;return m_fRadius;此方法的目的没变,但却修改了半径float Round:GetRadius() constm_fRadius *= 2; 非法操作深入类与对象Const成员&总结:const成员可以分为const成员变量和const成员函数两类。!const成员变量: 一旦对象构建,该变量的值将不可更改。 const 成员变量 例: const float m_fPi; 在构造函数的中赋值。 深入类与对象Const成员! const成员函数: 表示该函数不能修改成员变量的值 普通成员函数声明 const; 例: float GetRadius() const; co

7、nst成员函数定义时也必须说明 const。 例: int GetX() const; int Point:GetX() const /函数实现深入类与对象static成员!回顾:静态变量 静态变量的意义:如果是全局变量,则在程序开始时(main函数前)初始化一次,并自动将该变量的值定义为0。 例1: static int i; void main() i+; /do Something 由于i为静态变量,此时已经初始化为0深入类与对象static成员例2:void Test() int test = 5; cout test+;void main() Test(); Test(); Test

8、();static int test = 5;结果都为5结果为5、6、7深入类与对象static成员Fstatic在c+中的作用:用于表示但该类的的成员变量或成员函数。 例:序号学号姓名性别其它1111104006何志男2111104007何雄飞男3101104008余凯男J这里的序号实际上不应该属于任何一个学生对象深入类与对象static成员!类的Static成员变量class Student public: Student(); private: string m_sStuName; string m_sStuNo; bool m_bSex; ;Student:Student() m_sS

9、tuName = “default”; m_sStuNo = “default”; Student stu20;注意:注意:static成员变量必须现在类外赋值,成员变量必须现在类外赋值,而不能直接使用构造函数构造而不能直接使用构造函数构造深入类与对象static成员&思考:加入static成员后,类的内存布局 class Test public: /Some Function; private: int m_iTestNum; sTestNum; ; Test t1; sizeof(t1)为多少呢?结果为4深入类与对象static成员!类的Static成员函数class Studen

10、t public: Student(); static int GetSeriNo();private: ;int Student:GetSeriNo() return seriNo; 怎么调用呢?Student stu;Student:GetSeriNo();深入类与对象static成员&思考,加入static函数后Student类的内存布局深入类与对象static成员:类的static成员分为static成员变量和成员函数两种,其作用为:用于表示但该类的的成员变量或成员函数。其共同特征为,而独立存在于全局变量区。 例: static int seriNo; 例:int Studen

11、t:seriNo = 0; 深入类与对象static成员例:static int GetSeriNo();例: Student stu; stu.GetSeriNo(); Student:GetSeriNo();深入类与对象static成员练习1:请找出以下程序错误的地方,说明原因并改正class T1 public: T1() a = 10; int GetMinusA() return -a; static int GetA() return a;private: static int a; int b;void main() T1 t; cout t.GetA() endl T1:Get

12、MinusA() endl T1:GetA() endl;深入类与对象static成员F练习答案:Class Round public: /其它同原Round类 static float GetPi(); static void SetPi(float _pi);private: /其它同原Round类 static float sPi;float static:sPi = 3.14;float Round:GetPi() return sPi; Round rd1(5);float Round:SetPi(float _pi) sPi = _pi; Round:SetPi(3.1415926

13、);cout rd1.GetPi();练习:为构建好的Round类添加static成员pi以及修改 和获取该static变量的static函数静态成员的典型应用:对象构造计数深入类与对象static成员有些时候,我们希望能够对某一个类构造的对象数量进行统计,静态成员能很好的帮我们完成这个功能。class Test public: Test(); Test();private: static int count;/在类外初始化静态变量int Test:count = 0;Test:Test() count+; cout count “ object constructed endl;Test:T

14、est() count-; cout count “ object destructed endl;深入类与对象友元(friend)&思考:一个计算两点之间距离的函数 int CalcDistance(Point& pt1, Point& pt2) int xDis = pt1.m_iX; int yDis = pt2.m_iY; return sqrt(xDis * xDis + yDis * yDis); m_iX和m_iY均是私有成员,无法直接访问可以使用GetX(),GetY()/Point类申明中friend int CalcDistance(Point&am

15、p; pt1, Point& pt2);深入类与对象友元(friend)一个不属于当前类的普通函数或者属于其它类的成员函数,其可以访问类内的任何成员(包括私有成员)。 friend 函数声明; 或 friend 类名:函数声明; 普通函数实现方式 或 类成员函数实现方式 (友元函数属于其他类)深入类与对象友元(friend)&例1:Class A public: /Some Function private: int num;Class B public: void Func(A a);Private: /;void B:Func(A a) / do something on

16、a.num ;深入类与对象友元(friend)&思考:例1的修改Class A public: /Some Function private: int num;Class B public: void Func();private: ;void B:Func() / do something on ;void B:OtherFun() / do something on ;深入类与对象友元(friend)如果类B是类A的友元,则类B的所有成员函数都可以访问类A的私有成员。反之不成立(即友元关系是单向的)。 class friend 类名 。 深入类与对象再议构造函数&思考.1:

17、要实现一个表示一条线段的类。F由于两点可以表示一条线段,因此我们可以使用构造好的Point类来方便的实现 class Line public: Line(); int GetLength(); private: Point m_ptBegin; Point m_ptEnd; ;此两成员均为Point类的对象,怎么构造呢? 如果由Line类负责构造,此构造函数应该怎么写呢?深入类与对象再议构造函数&思考.2:假设Line类已经可用,考虑一下如何使用F情况1:假设要创建一条从点(3,5)到点(10, 22)的线 Line line(3, 5, 10, 22); Line(int _begi

18、nX, int _beginY, int _endX, int _endY);怎么实现,并构建两个点呢?Line:Line(int _beginX, int _beginY, int _endX, int _endY) : m_ptBegin(_beginX, _beginY), m_ptEnd(_endX, _endY) 调用了构造函数深入类与对象再议构造函数F情况2:创建一条从点(3,5)到点(10, 22)的线,但先创建这两个点。 Point pt1(3, 5), pt2(10, 22);Line line( );Line(Point& pt_begin, Point&

19、pt_end);Line:Line(Point& pt_begin, Point& pt_end) : m_ptBegin(pt_begin), m_ptEnd(pt_end) 调用了拷贝构造函数pLine(Line& oldLine); Line:Line(Line& oldLine) : m_ptBegin(oldLine.m_ptBegin), m_ptEnd(oldLine.m_ptEnd)深入类与对象再议构造函数复习:简单的Line类 /in “Line.h” #include “Point.h” class Line public: Line(int

20、 _beginX, int _beginY, int _endX, int _endY); Line(Point& pt_Begin, Point& pt_end); int GetLength(); private: Point m_ptBegin; Point m_ptEnd; ;深入类与对象再议构造函数/in “Line.cpp”#include “Line.h”#include Line:Line(int _beginX, int _beginY, int _endX, int _endY) : m_ptBegin(_beginX, _beginY), m_ptEnd(

21、_endX, _endY)Line:Line(Point& pt_Begin, Point& pt_End) : m_ptBegin(pt_Begin) m_ptEnd(pt_End)int Line:GetLength() int xLength = m_ptEnd.GetX() m_ptBegin.GetX(); int yLength = m_ptEnd.GetY() m_ptBegin.GetY(); return sqrt(xLength * xLength + yLength * yLength);ClassC的实现: ClassC:ClassC() : clA()

22、, clB() cout “C:Constructor” endl; ClassC:ClassC() cout “C:Destructor” endl; void main() ClassC clC; 深入类与对象再议构造函数FClassC的实现: ClassC:ClassC() : clA(), clB() cout “C:Constructor” endl; ClassC:ClassC() cout “C:Destructor” endl;&思考.1:如下使用后的结果 void main() ClassC clC; 深入类与对象再议构造函数深入类与对象再议构造函数& 思考.2:如下修改后的结果是多少? ClassC:ClassC() : clB(), clA() cout “C:Constructor” endl;深入类

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论