ch1++类与对象剖析

上传人:今*** 文档编号:107471775 上传时间:2019-10-19 格式:PPT 页数:183 大小:2.03MB
返回 下载 相关 举报
ch1++类与对象剖析_第1页
第1页 / 共183页
ch1++类与对象剖析_第2页
第2页 / 共183页
ch1++类与对象剖析_第3页
第3页 / 共183页
ch1++类与对象剖析_第4页
第4页 / 共183页
ch1++类与对象剖析_第5页
第5页 / 共183页
点击查看更多>>
资源描述

《ch1++类与对象剖析》由会员分享,可在线阅读,更多相关《ch1++类与对象剖析(183页珍藏版)》请在金锄头文库上搜索。

1、第1章 类和对象 第2章 运算符重载 第3章 继承与派生 第4章 多态性与虚函数 第5章 输入输出操作 第6章 模板与标准库 第7章 异常处理,C+面向对象程序设计,第1章 类和对象,1.1 类 1.2 对象 1.3 类与结构 1.4 类的封装性和信息隐蔽 1.5 构造函数与析构函数,第1章 类和对象,1.6 复杂对象 1.7 this指针 1.8 静态成员 1.9 const与类 1.10 友元,对象和类,属性: 年龄; 性别; 身高; 体重; 动作: 跑; 跳; 说话;,person,对象和类,属性: 姓名; 性别; 年龄; 年级; 动作: 跑; 跳; 说话; 更改年级; 显示姓名、年龄、

2、年级;,属性: 姓名:小王; 性别:M 年龄:20; 年级:二; 动作: 跑; 跳; 说话; 更改年级; 显示姓名、年龄、年级;,学生,小王,对象和类,属性: 姓名; 性别; 年龄; 授课名称; 动作: 跑; 跳; 说话; 上课; 批改作业;,属性: 姓名:大王; 性别:M; 年龄:40; 授课名称:政治经济学 动作: 跑; 跳; 说话; 上课; 批改作业,教师,大王,面向对象编程的程序基本单位是类 类是数据和操作数据的函数的封装 类的对象使用自己的方法完成对数据的操作 类可以隐藏数据和操作细节 对象通过类接口与外部通信,1.1 类,结构类,struct Time int hour; / 0-

3、23 int minute; / 0-59 int second; / 0-59 ;,#include struct Time int hour; int minute; int sec; ;,int main( ) Time t1; cint1.hourt1.minute t1.sec; coutt2.hourt2.minutet2.sec; coutt2.hour:t2.minute :t2.secendl; return 0; ,结构类,set_time(t1);,set_time(t2);,show_time( t1 );,show_time( t2 );,void set_time(

4、Time,void show_time(Time,#include struct Time int hour; int minute; int sec; ; void set_time(Time ,int main( ) Time t1; set_time(t1); show_time(t1); Time t2; set_time(t2); show_time(t2); return 0; ,结构类,结构类,1.1 类,class Time private : 关于时间的数据; public : 读取时间值 ; 调整时间值 ; ;,类是数据和 操作数据的函数的封装,结构类,class Time

5、 private : int hour; int minute; int second; public : 读取时间值 ; 调整时间值 ; ;,public : void set( int, int, int) ; void get( );,对象使用自己的方法 对数据操作,对象通过类接口与外部通信,结构类,class Time private : int hour; int minute; int second; public : 读取时间值 ; 调整时间值 ; ;,public : void set( int, int, int) ; void get( );,改用含成员函数的类来处理 #i

6、nclude class Time public: void set_time( ); void show_time( ); private: int hour; int minute; int sec; ;,int main( ) Time t1; t1.set_time( ); t1.show_time( ); Time t2; t2.set_time( ); t2.show_time( ); return 0; ,void Timeset_time( ) cinhour; cinminute; cinsec; void Timeshow_time( ) couthour: minute:

7、 secendl; ,class Time public: void set_time( ) cinhourminutesec; void show_time( ) couthour: minute: secendl; private: int hour; int minute; int sec; ;,结构类,C+ access control,class Point public: int GetX(); / retuarn the value of x int GetY(); / return the value of y void SetX(int valX) x=valX; / set

8、 x void SetY(int valY) y=valY; /set y private: int x,y; /the coordinates ; / semicolon: dont forget it!,Member Functions,Member variable,Access Control,Styles of creating a class,int Point:GetX() return x; int Point:GetY() return y; int GetX() cout “Global Function!”; ,void main() /define two object

9、s Point P, P2; /call member function P.SetX(300); cout “x= “ P.GetX(); P2 = P; /object assigned GetX(); / Call Global Fun ,Because different class can have member functions with the same name, we must specify the class name when defining a member function,Class Members,A class object contains a copy

10、 of the data defined in the class. The functions are shared. The data belonged to themselves.,Point(int, int) int GetX() int GetY(),P,Q,R,Point P(5,10); Point Q(20,10); Point R(50,60);,Sharing :,Controlling Member Access,class class_name public: /public members private: /private members ;,The part o

11、f public constitutes the public interface to objects of the class. If the mem_fun() is the public member function of class, you can wirte like these: class_name obj; obj.mem_fun();,Controlling Member Access,class class_name public: /public members private: /private members ;,The part of private can

12、be used only by member function. If the mem_fun() is the private member function of class, you cannt write like these: class_name obj; obj.mem_fun(); /error!,Again Point class,class Point public: int GetX(); / return the value of x int GetY(); / return the value of y void SetX(int valX) x=valX; / se

13、t x void SetY(int valY) y=valY; /set y private: int x, y; /the coordinates ;,void main() Point P; P.SetX(10); P.GetX(); /error: x is private member P.x = 10; ,类定义举例:student.cpp,using namespace std ; 所谓namespace,是指标识符的各种可见范围。C标准程序库中的所有标识符都被定义于一个名为std的namespace中。 当使用时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c+实现

14、;当使用的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。,1. 类的声明,面向对象编程的基础是把自己的数据类型定义为类,类是用户定义的数据类型。 定义一个类后,就可以用类名声明类类型的变量,即将类实例化为不同的对象; C+中用关键字class定义包含数据成员和成员函数的类型。类使程序员可以构造对象的属性和行为或操作。,1.声明类类型,类是一种用户自定义的数据类型,它的一般定义格式如下: class 类名 private: 私有数据成员和成员函数; protected: 保护数据成员和成员函数; public: 公有数据成员和成员函数; ; 各

15、个成员函数的实现;,1 class Time 2 public: 3 void setTime( int, int, int); 4 void printMilitary(); 5 void printStandard(); 6 private: 7 int hour; / 0-23 8 int minute; / 0-59 9 int second; / 0-59 10 ;,定义类类型,/分号不能忽略!,1.声明类类型,1 class Time 2 public: 3 Time( ); 4 void setTime( int, int, int); 5 void printMilitary(); 6 void printStandard(); 7 private: 8 int hour; / 0-23 9 int minute; / 0-59 10 int second; / 0-59 11 ;,1.声明类类型,公有成员 成员函数,私有成员 数据成员,/成员

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 大学课件

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