1 / 24

类与对象

类与对象. 教学内容:. C++ 类的定义. 成员函数的定义. 对象的创建与使用. 类与对象. 封装( Encapsulation ) 是面向对象程序设计最基本的特性,就是把数据(属性)和函数(操作)合成一个整体,并且采用一定的方式提供一定访问权限。 通过本次课程的学习: 掌握 C++ 的类( class )和对象( object )的概念,建立 “ 函数也可以是数据类型的成员 ” 的思想。. C++ 类的定义. 引入: 类是一种用户自数据类型。组成这种类型的成员不仅可以是数据,而且可以是对数据进行操作的函数。

altessa
Télécharger la présentation

类与对象

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 类与对象 教学内容: C++类的定义 成员函数的定义 对象的创建与使用

  2. 类与对象 封装(Encapsulation)是面向对象程序设计最基本的特性,就是把数据(属性)和函数(操作)合成一个整体,并且采用一定的方式提供一定访问权限。 通过本次课程的学习: 掌握C++的类(class)和对象(object)的概念,建立“函数也可以是数据类型的成员”的思想。

  3. C++类的定义 引入: 类是一种用户自数据类型。组成这种类型的成员不仅可以是数据,而且可以是对数据进行操作的函数。 描述客观事物必须用不同的数据类型来描述事物不同的方面。如要描述商品,需要描述如下信息: 商品名称(用字符串描述),该商品数量(用整型数描述),该商品单价(用浮点数描述),该商品总价(用浮点数描述)。 这里用了属于三种不同数据类型的四个数据成员来描述一种商品。我们可以通过定义一个类来描述商品的信息。

  4. C++类的定义 类的表述: class CGoods{ public: char Name[21] ; int Amount ; float Price ; float Total_value ; } ;//最后的分号不可少,这是一条说明语句 说明:关键字class是数据类型说明符,指出下面说明的是一个类。标识符CGoods是商品这个类的类型名。花括号中是构成类的系列成员,关键字public是一种访问限定符。

  5. 定义格式: • class<类名> • { • public: • <成员说明> • profecfed: • <成员说明> • private: • <成员说明> • }; • //实现部分 • 成员函数的具体定义: • <函数类型><表名>::<函数名>(<参数表)> • { • <函数体> • }}; //注意:所有说明都以分号结束

  6. C++类的定义 • 访问限定符: • public(公共的)说明的成员能从外部进行访问。 • private(私有的) • protected(保护的)说明的成员不能从外部进行访问。 • 注:1)每种说明符可在类体中使用多次。 • 2)访问限定符的作用域是从该说明符出现开始到下一个说明符之前或类体结束之前结束。 • 3)如果在类体起始点无访问说明符,系统默认定义为私有(private)。 • 访问限定符private(私有的)和protected(保护的)体现了类具有封装性(Encapsulation)。

  7. C++类的定义 商品类的定义: class CGoods{ private : char Name[21] ; int Amount ; float Price ; float Total_value ; public : void RegisterGoods(char[],int,float); //输入数据 void CountTotal(void) ; //计算商品总价值 void GetName(char[]) ; //读取商品名 int GetAmount(void) ; //读取商品数量 float GetPrice(void) ; //读取商品单价 float GetTotal_value(void) ; };//读取商品总价值

  8. 注意: 类是一种数据类型,定义时系统不为类分配存储空间,所以不能对类的数据成员初始化。类中的任何数据成员也不能使用关键字extern、auto或register限定其存储类型。 成员函数可以直接使用类定义中的任一成员,可以处理数据成员,也可调用函数成员。

  9. 成员函数的定义 函数定义: 通常在类定义中,成员函数仅作声明。函数定义通常在类的说明之后进行,其格式如下: 返回值类型类名::函数名(参数表) {……}//函数体 其中运算符“::”称为作用域解析运算符,它指出该函数是属于哪一个类的成员函数。

  10. 类CGoods的函数定义 voidCGoods::RegisterGoods(char name[] , int amount , float price){ strcpy(Name , name) ; //字符串复制函数 Amount=amount ; Price=price ; } voidCGoods::CountTotal(void){ Total_value = Price*Amount;} voidCGoods::GetName(char name[]){ strcpy(name , Name);} intCGoods::GetAmount(void){return(Amount) ;} floatCGoods::GetPrice(dvoi){return(Price) ;} floatCGoods::GetTotal_value(void){return(Total_value) ;}

  11. 课堂讨论: 问题 : 类的定义和实现放在一起好不好? 回答: 不好! 方法 : 类的定义放在以h为扩展名的文件中 类的实现(成员函数的定义)放在以cpp为扩展名的文件中。 优点 :1、把类的定义与实现分离开来, 便于文挡管理、维护。 2、可将类的实现隐蔽起来,使软 件开发商能独立开发软件。 3、便于团体式的大型软件开发。

  12. 内联成员函数 可以在进行类的定义时给出成员函数的实现,这时的成员函数是内联函数。 例如: class location { private : int x,y; public : void init(int initx,int inty) { x=intx,y=inty;} int getx(){return x;} int gety(){return y; };

  13. 也可以在类定义之外使用inline 关键字给出成员函数的实现。 例如: class location{ private: int x,y; public: void init(int initx,int inity); int getx(); int gety(); }; inline void location::init(int initx,int inity) { x=initx; y=inity; }

  14. 成员函数的重载 在类中可以说明重载函数以及带有缺省参数的函数。重载函数的名字相同,但参数的类型却不同。 例如: #include<iostream.h> class location { private : int X,Y; public: void init(int x=0,int y=0);

  15. 成员函数的重载 void valueX(int val){X=val;} int valuex(){return X;} void valueY(int val){Y=val;} int valueY(){return Y;} }; void location::init(int initx,int inity) {X=initx,Y=initY;} void main() { location A,B; A.init(); A.valueX(5); B.init(6,4); B.valueY(8); cout<<A.valueX()<<endl<<A.valueY()<<endl; cout<<B.valueX()<<endl<<B.valueY()<<endl;

  16. 对象的创建与使用 定义对象: 对象是类的实例(instance)。定义一种数据类型只是告诉编译系统该数据类型的构造,并没有分配内存。 定义对象的方法: 1)先定义表,再定义对象 <类名><对象名类>; 2)定义表的同时定义对象 class<类名> { <类体> }<对象名类> 例如:CGoods Car; 创建了CGoods类的一个对象Car,同时为它分配了属于它自己的存储块,用来存放数据和对这些数据实施操作的成员函数(代码)。对象只在定义它的域中有效。

  17. 对象1 对象n 对象2 数据区 数据区 数据区 ......  公共代码区 注意:不同对象的数据成员的内容是不一样的;而行为(操作)用函数来描述的,这些操作的代码对所有对象都是一样的。 仅为每个对象分配一个数据区,代码区(放成员函数的区域)为各对象类共用。

  18. 对象的创建与使用 例题:商品类对象应用实例 对象使用:只要在对象名后加点号(点操作符),再加成员数据或成员函数名就可以了。但是这些成员必须是公有的成员,只有公有成员才能在对象的外面对它进行访问。

  19. class CGoods{ private : char Name[21] ; int Amount ; float Price ;float Total_value ; public : CGoods(); CGoods(char [],int,float); CGoods(char [],float); void RegisterGoods(char[],int,float) ; void CountTotal(void) ; void GetName(char[]) ; int GetAmount(void) ; float GetPrice(void) ; float GetTotal_value(void) ;};

  20. voidCGoods::RegisterGoods(char name[] , int amount , float price){ strcpy(Name , name) ; //字符串复制函数 Amount=amount ; Price=price ; } voidCGoods::CountTotal(void){ Total_value = Price*Amount;} voidCGoods::GetName(char name[]){ strcpy(name , Name);} intCGoods::GetAmount(void){return(Amount) ;} floatCGoods::GetPrice(dvoi){return(Price) ;} floatGoods::GetTotal_value(void){return(Total_value) ;}

  21. int main( ){ char string[21]={'\0'}; CGoods Car1("夏利2000",30,98000.0); CGoods Car2("桑塔那2000",164000.0); Car1.GetName(string); //string赋值car.Name cout<<setw(20)<<string<<setw(5) <<Car1.GetAmount(); cout<<setw(10)<<Car1.GetPrice()<<setw(20) << Car1.GetTotal_value()<<endl; Car2.GetName(string); //string赋值car.Name cout<<setw(20)<<string<<setw(5) << Car2.GetAmount(); cout<<setw(10)<<Car2.GetPrice()<<setw(20) << Car2.GetTotal_value()<<endl;return 0;}

  22. this指针 this 是一个隐含于每一个类的成员函数的特殊指针该指针是一个指向正在被某个成员函数操作的对象的指针。 当一个对象调用成员函数时,编译程序先将对象的地址赋给this 指针,然后调用成员函数,每次成员函数存取数据成员时,则隐含使用this 指针。通常不显式使用this指针。 例题:分析下列程序的输出结果。 #include<iostream.h> class A {

  23. public: A(){a=b=0;} A(int I,int j){a=I,b=j;} void copy(A &aa); void print(){cout<<a<<“,”<<b<<endl; private: int a,b; }; void A::copy(A &aa) { if(this==&aa) return; *this=aa; } void mian() { A a1,a2(3,4); a1.copy(a2); a1.print() } result:3,4

  24. this指针是c++实现封装的一种机制,它将对象和该对象调用的成员函数连接在一起,在外部看来,每个对象都拥有自己的成员函数。

More Related