- 3
- 0
- 约 69页
- 2016-11-27 发布于河南
- 举报
Chapter 7_01 Defining Classes
Chapter 7 Defining Classes定义类 Overview 7.1 Structures (结构体) 7.2 Classes(类) 7.3 Abstract Data Types (抽象数据类型) 本章重点 从本章开始进入面向对象的知识介绍 掌握面向对象程序设计的基本思路、主要特点 掌握类和对象的概念 掌握类和对象的定义 回顾:面向对象与面向过程 过程化程序设计: Procedural Programming,如C 面向对象程序设计(OOP): object oriented programming,如C++,Java 过程化程序设计的理念是:How to do? 面向对象的设计理念是:What to do? 面向对象中处理的事物比较复杂,无法通过一个基本数据类型来表示,我们需要根据处理的对象定义出合适的数据类型,这种数据类型,在面向对象中称为“类” 下面我们为类下一个定义: 什么是类 类是一组具有相同属性和行为的对象的集合 。 类是抽象的,并不是具体指某一个人,而是泛指某一类人 。 什么是对象 世间万物皆对象,在生活中,可能是一只猫、一只狗,或者是一张订单、银行卡,也可能是一个想法、一个数学中的概念,等等都是对象。 对象就是类的具体实现。 类与对象的关系 类是对象的抽象,对象是类的具体实例。 面向对象的特征 Abstraction(抽象性) 抽象的作用是表示同一类事物的本质。即,定义一些类 Encapsulation(封装性) 将有关的数据和操作代码封装在一个对象中,形成一个基本单位,各个对象之间相互独立,互不干扰。 将对象中某些部分隐藏,只留下少量接口,以便于与外界联系。 Inheritance(继承性) 一个对象可以继承其他对象的所有属性 代码重用 Polymorphism(多态性) 同一个名字可以有多个不同的含义 下面我们具体介绍如何定义和使用类与对象 类的定义与结构体有一点类似 7.1 Structures(结构体) We will start by defining structures as a first step toward defining classes 我们在C语言中学习过structure, C++语言的structure与C语言的类似,本节只做简单介绍 Structure Is useful to have a collection of values of different types and to treat the collection as a single item 7.1 Structures(结构体) Eg: the information of a student struct Student {string no; string name; char sex; int age; }; Keyword struct begins a structure definition Student is the structure tag or the structure’s type Member names are identifiers declared in the braces Using the Structure To declare two variables of type Student: Student s1,s2; s1,s2 contain distinct member variables no, name, sex, age Use the dot operator to specify a member variable s1.no s1.name s2.age s1.no=“090101”; s1.age=20; Initializing structures Eg: Student s={“090201”,”Lily”,’M’,21}; 注意:初始化值按照结构体定义中给出的成员变量的顺序给出 7.2 Classes(类) 类是一种数据类型 类的声明包含两部分: member variables成员变量,用以描述类属性 member functions成员函数,用以描述施加在类上的动作 A class description is somewhat like a structure definition plus the member functions 类的定义格式: class 类名{ public(或protected或private ): 成员函数; 成员变量; }; 各个成员函数的实现;//类的实现 Class Stu
您可能关注的文档
最近下载
- DHG9070A型电热鼓风干燥箱说明书.pdf VIP
- 【老照片】1966年-1967年的彩色中国.docx VIP
- 蒙特利尔认知评估量表.doc VIP
- 解放前后大不同.连环画.1950年.pdf VIP
- 2026年济南工程职业技术学院单招《数学》题库含完整答案详解【考点梳理】.docx VIP
- 1949--1989年国产老电影目录.doc VIP
- 1949--166年国产老电影目录.doc VIP
- 简易精神状态检查量表(-MMSE).doc VIP
- LNG-CNG-LPG(液化石油气、液化天然气、压缩天然气)汽车、罐车事故处置.pptx VIP
- 简易精神状态检查量表(MMSE).docx VIP
原创力文档

文档评论(0)