【文档说明】C++程序设计-cchp8善于使用指针与引用-类.ppt,共(67)页,423.000 KB,由小橙橙上传
转载请保留链接:https://www.ichengzhen.cn/view-2174.html
以下为本文档部分文字说明:
8.1面向对象程序设计方法概述8.2类的声明和对象的定义8.3类的成员函数8.4对象成员的引用8.5类的封装性和信息隐蔽8.6类和对象的简单应用举例第8章类和对象的特性8.1面向对象程序设计(OOP)方法概述传
统的面向过程的程序设计方法是围绕功能进行的,用一个函数实现一个功能。当程序规模大、数据很多、数据结构复杂、功能种类繁多时,程序设计者会难以应付。面向对象的程序设计的设计思路是围绕数据对象进行的,先将数据和对数据的操作封装成一个个对象,然后调用各个对象完成所需的任务。对大型程序设计来说
,面向对象程序设计的方法十分有效,能大大降低程序设计者的工作难度,减少出错的机会。OOP的最重要的特性:◼抽象---将一个个同类的个体抽象成一个“类”如:张三、李四等具体的人抽象出“人”的概念◼封装和数据隐藏将数据和对数据
的操作代码封装在一个对象中,形成一个基本单位。对象中的数据可以对外部隐蔽。8.1面向对象程序设计(OOP)方法概述◼继承一个新类可以利用已有的类的基础上建立起来,该新类是已有类(叫基类)的派生类。派生类中含有基类的数据
和操作,即继承了基类的内容。◼可重用代码利用派生类继承机制,可以重用已有软件的大部分代码。从已有软件的类中派生出新类,新类可使用基类的代码。这样可减少编程工作量。◼多态性利用派生类继承机制,各个派生类既有基类的共同特性,又有各个派生类自己新
增的特性。例:设计求圆的周长和面积的程序。面向过程---首先想的是处理方法(过程):输入圆的半径计算面积;计算周长;显示相关的计算结果面向过程和面向对象的编程例:设计求圆的周长和面积的程序。面向过程:要输入圆的半径;==〉在main函数中输入
计算面积;==〉调用一个函数计算面积;计算周长;==〉调用一个函数计算周长;显示相关的计算结果;==>在main函数中输出结果面向过程的编程面向过程方法:求圆的面积//计算圆的面积和周长#include<iostream.h>usingnamespac
estd;constfloatpi=3.1415;floatarea(float);//函数原型声明floatgirth(float);//函数原型声明intmain(){floatr;cout<<"Pleaseinputradius:\n";cin>>r;cout<<"rad
ius="<<r<<endl;cout<<"girth="<<girth(r)<<endl;cout<<"area="<<area(r)<<endl;return0;}//定义求周长函数floatgirth(floatr){return(2*pi*r
);}//定义求面积函数floatarea(floatr){return(pi*r*r);}面向对象设计有关圆的程序,跟踪的对象是圆首先考虑的是数据:圆的半径;不仅要考虑如何表示数据,还要考虑如何使用数据;因此要有一个“类”表示圆的各个方面:◼表示圆的数据:半径◼一些处理该数
据的方法:首先要有获得“圆”信息的方法;其次,要有关于“圆”的各种计算方法,如:周长、面积等。◼另外还需要一些更新和显示“圆”信息的方法。#include<iostream>classCircle{doubleradius;public:voidSet_Radius(do
ubler){radius=r;}doubleGet_Radius(){returnradius;}doubleGet_Girth(){return2*3.14f*radius;}doubleGet_Area(){re
turn3.14f*radius*radius;}};intmain(){CircleA,B;A.Set_Radius(6.23);cout<<"A.Radius="<<A.Get_Radius()<<endl;cout<<"A.Girth="<<A.Get_Girth
()<<endl;cout<<"A.Area="<<A.Get_Area()<<endl;B.Set_Radius(10.5);cout<<"B.radius="<<B.Get_Radius()<<endl;cout<<"B.Girth="<
<B.Get_Girth()<<endl;cout<<"B.Area="<<B.Get_Area()<<endl;return0;}1.1.2一个简单的C++程序1.1概述用面向对象的方法编程#includ
e<iostream>classCircle{doubleradius;public:voidSet_Radius(doubler){radius=r;}doubleGet_Radius(){returnradius;}doubleGet_Girth(){return2*3.14*rad
ius;}doubleGet_Area(){return3.14*radius*radius;}};intmain(){CircleA,B;A.Set_Radius(6.23);cout<<"A.Radius="<<A.Get_Radius(
)<<endl;cout<<"A.Girth="<<A.Get_Girth()<<endl;cout<<"A.Area="<<A.Get_Area()<<endl;B.Set_Radius(10.5);cout<<"B.radius="<<B.Get_Radius()<<en
dl;cout<<"B.Girth="<<B.Get_Girth()<<endl;cout<<"B.Area="<<B.Get_Area()<<endl;return0;}Circle类声明1.1.2一个简单的C++程序1.1概述用面向对象的方法编程#include<iostr
eam>classCircle{doubleradius;public:voidSet_Radius(doubler){radius=r;}doubleGet_Radius(){returnradius;}doubleGet_Girth(){return2*3.14*radius;}dou
bleGet_Area(){return3.14*radius*radius;}};intmain(){CircleA,B;A.Set_Radius(6.23);cout<<"A.Radius="<<A.Get_Radius()<<endl;cout<<"A.Girth="<
<A.Get_Girth()<<endl;cout<<"A.Area="<<A.Get_Area()<<endl;B.Set_Radius(10.5);cout<<"B.radius="<<B.Get_Radius()<<endl;
cout<<"B.Girth="<<B.Get_Girth()<<endl;cout<<"B.Area="<<B.Get_Area()<<endl;return0;}数据成员:圆的半径1.1.2一个简单的C++程序1.1概述用面向对象的方
法编程#include<iostream>classCircle{doubleradius;public:voidSet_Radius(doubler){radius=r;}doubleGet_Radius(){returnradius;}doubleGet_Girth
(){return2*3.14*radius;}doubleGet_Area(){return3.14*radius*radius;}};intmain(){CircleA,B;A.Set_Radiu
s(6.23);cout<<"A.Radius="<<A.Get_Radius()<<endl;cout<<"A.Girth="<<A.Get_Girth()<<endl;cout<<"A.Area="<<A.Get_Area()<<endl;B.Set
_Radius(10.5);cout<<"B.radius="<<B.Get_Radius()<<endl;cout<<"B.Girth="<<B.Get_Girth()<<endl;cout<<"B.Area="<<B.Get_Area()<<endl;return0;}成员函数:对
圆的操作1.1.2一个简单的C++程序1.1概述用面向对象的方法编程#include<iostream>classCircle{doubleradius;public:voidSet_Radius(doubler){radius=r;}double
Get_Radius(){returnradius;}doubleGet_Girth(){return2*3.14*radius;}doubleGet_Area(){return3.14*radius*radius;}};intmain(){CircleA,B;A.Set_Radiu
s(6.23);cout<<"A.Radius="<<A.Get_Radius()<<endl;cout<<"A.Girth="<<A.Get_Girth()<<endl;cout<<"A.Area="<<A
.Get_Area()<<endl;B.Set_Radius(10.5);cout<<"B.radius="<<B.Get_Radius()<<endl;cout<<"B.Girth="<<B.Get_Girth()<<endl;cout<<"B.Area="<<B.Get
_Area()<<endl;return0;}建立对象(类类型变量)1.1.2一个简单的C++程序1.1概述用面向对象的方法编程#include<iostream>classCircle{doubleradius;public:voidSet_Radius(d
oubler){radius=r;}doubleGet_Radius(){returnradius;}doubleGet_Girth(){return2*3.14*radius;}doubleGet_Area(){return3.14*radius*radius;}};intmain(
){CircleA,B;A.Set_Radius(6.23);cout<<"A.Radius="<<A.Get_Radius()<<endl;cout<<"A.Girth="<<A.Get_Girth()<<endl;cout
<<"A.Area="<<A.Get_Area()<<endl;B.Set_Radius(10.5);cout<<"B.radius="<<B.Get_Radius()<<endl;cout<<"B.Girth="<<B.Get_Girth()<<endl;cout<<
"B.Area="<<B.Get_Area()<<endl;return0;}1.1.2一个简单的C++程序1.1概述通过对象调用类的成员函数用面向对象的方法编程8.2类的声明和对象的定义8.2.1类和对象的关系类-
---相当于类型,是具体对象的抽象。类不占用内存空间。对象--是类的具体实例。是属于“类”类型的变量。对象是占用内存空间的。8.2.2声明类类型举例说明:#include<iostream>classTdate{
public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<
month<<"."<<day<<endl;}private:intmonth;intday;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}为便于识别类,一般可约定将类名首字母大写#incl
ude<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return(yea
r%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;intday;intyear
;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLea
pYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;intday;intyea
r;};类声明#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return(year%4==0&&year%100!=0)||(year
%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;intday;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Pr
int();}classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return(year%4==0&&year%100!=0)||(
year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;intday;intyear;};关键字定义一个类标识符类名class,s
truct都可以定义一个类:class缺省说明时,其成员被认为是私有的struct若不特别指出,其所有成员都是公有的类声明的关键字#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;da
y=d;year=y;}intIsLeapYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;i
ntday;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}aTdate类的一个对象(实例)8.2.3定义对象的方法1.先声明类类型,然后再定义对象如:先定义了Student类类型再定义属于Student类的对象stu
;classStudent{..........};Studentstu;2.在声明类类型的同时定义对象如:定义Student类类型的同时定义属于Student类的对象stu;classStudent{
..........}stu;3.在声明类类型时不要类名,并同时定义对象如:class{..........}stu;#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}int
IsLeapYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<end
l;}private:intmonth;intday;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}类由成员构成:数据成员——描述对象的属性成员函数——
描述对象的方法(行为)8.3类的成员函数#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return(year%4==0&
&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;intday;intyear;};voidmain(){Tdatea;a
.Set(10,16,2003);a.Print();}数据成员数据成员#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return(year%4
==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmonth;intd
ay;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}类中定义成员函数直接在类中定义成员函数代码#include<iostream>classTdate{public:voidSet(intm,int
d,inty);intIsLeapYear();voidPrint();private:intmonth;intday;intyear;};voidTdate::Set(intm,intd,inty){month=m;day=d;year=y;}intTdate::IsLea
pYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidTdate::Print(){cout<<year<<"."<<month<<"."<<day<<endl;}在类外定义成员函数::作用域限定符—类中声明函数,类外定
义代码#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day=d;year=y;}intIsLeapYear(){return
(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:intmon
th;intday;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}成员的性质由关键字public、protected、private决定public公有公有段的成员是提供给外部的接口protected保护
保护段成员在该类和它的派生类中可见private私有私有段成员仅在类中可见各段中既可以包含数据成员,也可以包含成员函数成员访问限定符#include<iostream>classTdate{public:voidSet(intm,intd,inty
){month=m;day=d;year=y;}intIsLeapYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<
endl;}private:intmonth;intday;intyear;};voidmain(){Tdatea;a.Set(10,16,2003);a.Print();}类说明的一般形式为:class类名{public:公有段数据成员和成员函数;protected:保护段数据成员和成
员函数;private:私有数据成员和成员函数;};类声明的一般形式例学生类的定义classStudent{public:voiddisplay()//公有的成员函数{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;};private:intnum;//私有的数据成员stringname;charsex;};在类内定义成员函数例学生类的定义classStudent{public:voiddisplay();//
公有的成员函数private:intnum;//私有的数据成员stringname;charsex;};voidStudent::display(){cout<<"num:"<<num<<endl;cout<<"
name:"<<name<<endl;cout<<"sex:"<<sex<<endl;};在类内声明成员函数在类外定义成员函数#include<iostream>classTdate{public:voidSet(intm,intd,inty){month=m;day
=d;year=y;}intIsLeapYear(){return(year%4==0&&year%100!=0)||(year%400==0);}voidPrint(){cout<<year<<"."<<month<<"."<<day<<endl;}private:int
month;intday;intyear;};voidmain(){Tdatea,b,c;a.Set(10,16,2003);a.Print();b.Set(12,16,2003);b.Print();c.S
et(12,16,2003);c.Print();}8.3.4成员函数的存储方式属于同一类的不同对象a,b,c中的数据成员的值是不同的,而同一类的不同对象a,b,c的成员函数的代码是相同的。如:a.month,b.month,c.month是不同
的数据a.Set(),b.Set(),c.Set()三个函数使用的数据分别是a,b,c中的数据,但函数代码是公用的。8.3.4成员函数的存储方式classStudent//声明类类型{public:intnum;charname[20];floatscore;voiddisplay
(){cout<<"num:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"sex:"<<sex<<endl;}};Studentstud1,stud2;//定义了两个Student类的对象Student类的
练习数据成员在对象中占用内存空间;成员函数不占用对象的存储空间。8.4对象成员的使用成员运算符“.”stud1.numstud1.scorestud1.display();stud2.display();指针p=&stud1;p->nump->scorep->disp
lay();引用Studentstud1;Student&stud2=stud1;stud2.numstud2.scorestud2.display();成员运算符classStudent{public:intnum;charname[20
];floatscore;voiddisplay(){cout<<″num:″<<num<<endl;cout<<″name:″<<name<<endl;cout<<″score″<<score<<endl;}};intm
ain(){Studentstud1;//定义Student类的对象stud1.num=1001;//引用成员变量strcpy(stud1.name,“zhangsan”);stud1.score=89;stud1.dis
play();//调用成员函数return0;}调用成员函数——向对象发送消息intmain(){Studentstu1;stu1.num=1001;strcpy(stu1.name,"zhangsan");stu1.score=89.5;stu1.display();
return0;}向对象发出命令,使对象完成相应的操作指针intmain(){Studentstu1;Student*p;p=&stu1;p->num=1001;strcpy(p->name,"zhangsan");p->score=89.5;p->display()
;return0;}指针intmain(){Student*p=newStudent;p->num=1001;strcpy(p->name,"zhangsan");p->score=89.5;p->display();del
etep;return0;}引用intmain(){Studentstu1;Student&s=stu1;s.num=1001;strcpy(s.name,"zhangsan");s.score=89.5;cout<<"****&*****"<<endl;s.display
();return0;}公有的还是私有的classStudent{public:==〉private://私有成员intnum;charname[20];floatscore;voiddisplay(){cout<<″num:
″<<num<<endl;cout<<″name:″<<name<<endl;cout<<″score″<<score<<endl;}};intmain(){Studentstud1;//定义Student类的对象stud1.num=1001;//
stud1.num是私有成员,不能在类外使用strcpy(stud1.name,“zhangsan”);stud1.score=89;stud1.display();return0;}公有的还是私有的无论类成员是数据成员还是成员
函数,都可以在类的公有部分或私有部分中声明它;但隐藏数据是OOP的主要目标之一,因此数据项通常放在私有部分;将作为类接口的成员函数放在公有部分,否则,就无法从程序中调用这些函数.8.5类的封装性和信息隐蔽8.5.1公有接口与私有实现的分离数据隐藏:让数据表示成为私有,使得数据只能被授权的
函数访问;在声明一个类时,一般都是把所有的数据成员指定为私有的,使它们对外隔离。把需要让外界调用的成员函数指定为公用的。在类外虽然不能直接访问私有数据成员,但可以通过调用公用的成员函数来使用甚至修改私有数据
成员。数据隐藏classStudent{private://私有成员数据intnum;charname[20];floatscore;public:voiddisplay(){cout<<″num:″<<num<<endl;cout<<″name:″<<name<
<endl;cout<<″score″<<score<<endl;}};intmain(){Studentstud1;//定义Student类的对象stud1.num=1001;//非法,num是私有成员,不能再类外引用strcpy(st
ud1.name,“zhangsan”);//非法stud1.score=89;//非法stud1.display();//合法return0;}类声明中增加input成员函数classStudent{private://
私有成员数据intnum;charname[20];floatscore;public://公有成员函数接口voiddisplay(){cout<<″num:″<<num<<endl;cout<<″na
me:″<<name<<endl;cout<<″score″<<score<<endl;}};voidinput(intno,char*nam,floats);}voidStudent::input(
intno,char*nam,floats){num=no;strcpy(name,nam);score=s;}使用类intmain(){Studentstu1;stu1.input(1001,"zhangsan
",89.5);stu1.display();return0;}创建一个程序,使用类对象,测试类的特性.改进类声明classStudent{private:intnum;charname[20];floatscore[3];floataver;v
oidaverage();public:voiddisplay();voidinput(intn,char*p,floats);voidshow_aver();};数据:三门课成绩、平均分方法:求平均分、
显示平均分成员函数定义voidStudent::input(intno,char*pnam,float*ps){num=no;strcpy(name,pnam);score[0]=ps[0];score[1]=ps[1];score[2]=ps[2];}voidStud
ent::display(){cout<<"\nnum:"<<num<<endl;cout<<"name:"<<name<<endl;cout<<"score:"<<score[0]<<""<<score[1]<<""<<score[2]<<endl;}voidStudent::
average(){aver=(score[0]+score[1]+score[2])/3;}voidStudent::show_aver(){average();cout<<"\naverage="<<aver<<endl;}主函数intmain(){Studentst
u1;floats[3]={60,70,80};stu1.input(1001,“zhangsan”,s);//将1个学生的数据存入stu1中stu1.display();//输出stu1中的学生数据stu1.show_aver();//输出stu1中的平均分return0;}改主
函数:输入、输出10学生intmain(){Studentstu[10];intno;charname[20];floats[3];inti;for(i=0;i<10;i++){cout<<“输入第”<<i+1<<“个学生数据:”<<endl;cin>>no>>
name>>s[0]>>s[1]>>s[2];stu[i].input(no,name,s);}for(i=0;i<10;i++){cout<<“第”<<i+1<<“个学生:”;stu[i].display();stu[i].sh
ow_aver();}return0;}8.5.2类声明和成员函数定义的分离在面向对象的程序开发中,一般做法是将类的声明(其中包含成员函数的声明)放在指定的头文件中,用户如果想用该类,只要把有关的头文
件包含进来即可,不必在程序中重复书写类的声明,以减少工作量,节省篇幅,提高编程的效率。为了实现上一节所叙述的信息隐蔽,对类成员函数的定义一般不放在头文件中,而另外放在一个文件中。将声明Student类安排在student.h文件//student.h第1个文件
classStudent{private:intnum;charname[20];floatscore[3];floataver;voidaverage();public:voiddisplay();v
oidinput(intn,char*p,floats);voidshow_aver();};将定义Student类的成员函数安排在student.cpp文件//student.cpp第2个文件#include<iostream>#include"stu
dent.h“usingnamespacestd;voidStudent::input(intno,char*pnam,float*ps){num=no;strcpy(name,pnam);score[0]=ps[0];
score[1]=ps[1];score[2]=ps[2];}voidStudent::display(){cout<<"\nnum:"<<num<<endl;cout<<"name:"<<name<
<endl;cout<<"score:"<<score[0]<<""<<score[1]<<""<<score[2]<<endl;}voidStudent::average(){aver=(score[0]+score[1]+scor
e[2])/3;}voidStudent::show_aver(){average();cout<<"\naverage="<<aver<<endl;}将包括主函数在内的内容安排在源文件main.cpp//main.cpp第3个文件#include<iostream>
#include"student.h"usingnamespacestd;intmain(){Studentstu[10];intno;charname[20];floats[3];inti;for(i=0;i<10;i+
+){cout<<“输入第”<<i+1<<“个学生数据:”<<endl;cin>>no>>name>>s[0]>>s[1]>>s[2];stu[i].input(no,name,s);}for(i=0;i<10;i++){cout<<“第”<<i+1<<“个学生:”;stu[i].d
isplay();stu[i].show_aver();}return0;}这是一个包括3个文件的程序,组成两个文件模块:一个是主模块main.cpp,一个是student.cpp。在主模块main.cpp中包含头文件stu
dent.h。在模块student.cpp中也包含头文件student.h。编译时,分别编译2个cpp文件,得到2个obj文件,在连接成1个exe文件。在实际编程中,并不是将一个类声明做成一个头文件,而是将若干个常用的功能相近的类声明集中在一起,形成类库。类库
有两种:一种是C++编译系统提供的标准类库;一种是用户根据自己的需要做成的用户类库,提供给自己和自己授权的人使用,这称为自定义类库。在程序开发工作中,类库是很有用的,它可以减少用户自己对类和成员函数进行定义的工作量。类库包括
两个组成部分:(1)类声明头文件(如student.h);(2)已经过编译的成员函数的定义,它是目标文件(如student.obj)。用户只需把类库装入到自己的计算机系统中(一般装到C++编译系统所在的子
目录下),并在程序中用#include命令行将有关的类声明的头文件包含到程序中,就可以使用这些类和其中的成员函数,顺利地运行程序。由于要求接口与实现分离,为软件开发商向用户提供类库创造了很好的条件。开发商把用户所需的各种类的声明按类放在不同的头文件中,同时对包含成员函数定义的源文件进行编译,得到
成员函数定义的目标代码。软件商向用户提供这些头文件和类的实现的目标代码(不提供函数定义的源代码)。用户在使用类库中的类时,只需将有关头文件包含到自己的程序中,并且在编译后连接成员函数定义的目标代码即可。由于类库的出现,
用户可以像使用零件一样方便地使用在实践中积累的通用的或专用的类,这就大大减少了程序设计的工作量,有效地提高了工作效率。类和结构体的区别C++对结构体进行了扩展,使之具有与类相同的特性;唯一的区别是,结
构体的默认访问类型是public,而类为private.C++程序员通常使用类来实现类描述,而把结构体限制为只表示纯粹的数据对象或没有私有部分的类.小结指定类设计的第一步是提供类声明,典型的类声明格式如下;class类名{private:数据成员、成员函数p
ublic:成员函数};公有部分的内容构成设计的抽象部分—公有接口第二步是实现类成员函数创建类的实例—对象向对象发送消息8.6类和对象的简单应用举例例8.1最简单的例子。#include<iostream>usi
ngnamespacestd;classTime//定义Time类{public://数据成员为公用的inthour;intminute;intsec;};intmain(){Timet1;//定义t1为Time类对象cin>>t1.hour;/
/输入设定的时间cin>>t1.minute;cin>>t1.sec;cout<<t1.hour<<″:″<<t1.minute<<″:″<<t1.sec<<endl;return0;}例8.3将程序改用含成员函数的类来处理。#i
nclude<iostream>usingnamespacestd;classTime{public:voidset_time();//公用成员函数voidshow_time();//公用成员函数private://数据成员为私有in
thour;intminute;intsec;};intmain(){Timet1;//定义对象t1t1.set_time();//向t1的数据成员输入数据t1.show_time();//输出t1的数据
成员的值Timet2;//定义对象t2t2.set_time();//向t2的数据成员输入数据t2.show_time();//输出t2的数据成员的值return0;}voidTime∷set_time(
)//在类外定义set_time函数{cin>>hour;cin>>minute;cin>>sec;}voidTime∷show_time()//在类外定义show_time函数{cout<<hour
<<":"<<minute<<":"<<sec<<endl;}例8.4找出一个整型数组中的元素的最大值。这个问题可以不用类的方法来解决,现在用类来处理,读者可以比较不同方法的特点。#include<iostream>
usingnamespacestd;classArray_max//声明类{public://以下3行为成员函数原型声明voidset_value();//对数组元素设置值voidmax_value();//找
出数组中的最大元素voidshow_value();//输出最大值private:intarray[10];//整型数组intmax;//max用来存放最大值};voidArray_max∷set_value()//成员函数定义,向数组元素输入数值{inti;for(i=0;i<
10;i++)cin>>array[i];}voidArray_max∷max_value()//成员函数定义,找数组元素中的最大值{inti;max=array[0];for(i=1;i<10;i++)if(array[i]>
max)max=array[i];}voidArray_max∷show_value()//成员函数定义,输出最大值{cout<<″max=″<<max;}intmain(){Array_maxarrmax;//定义对象arrmaxarrmax.set_value();//调用ar
rmax的set_value函数,//向数组元素输入数值arrmax.max_value();//调用arrmax的max_value函数,//找出数组元素中的最大值arrmax.show_value();//调用arrmax的
show_value函数,//输出数组元素中的最大值return0;}运行结果如下:121239-3417134045-9176↙(输入10个元素的值)max=134(输入10个元素中的最大值)请注意成员函数定义与调用成员函数的关系,定义成员函数只是设计了一组操作代码,并未实际执行
,只有在被调用时才真正地执行这一组操作。可以看出:主函数很简单,语句很少,只是调用有关对象的成员函数,去完成相应的操作。在大多数情况下,主函数中甚至不出现控制结构(判断结构和循环结构),而在成员函数中使用控制结构。在面向对象的程序设计中,最关键的工作是类的设计。所有的数据和
对数据的操作都体现在类中。只要把类定义好,编写程序的工作就显得很简单了。题目9.6求3个长方体的体积。编写基于对象的程序。#include<iostream>usingnamespacestd;clas
sBox{private:floatlength;floatwidth;floatheight;public:voidinput_data(floatl,floatw,floath);floatvolume(void);voidshow
_Box(void);voidshow_volume(void);};//将参数中的长、宽、高存入Box的数据成员中voidBox::input_data(floatl,floatw,floath){length=l;width=w;heig
ht=h;}//计算体积,成员函数返回其值floatBox::volume(void){returnlength*width*height;}//显示长方体的长、宽、高voidBox::show_Box(void){cout<<"长方体的长="<<length<<",宽="
<<width<<",高="<<height<<endl;}//显示长方体的体积voidBox::show_volume(void){cout<<"长方体的体积="<<volume()<<endl;}intmain(){Boxbx[3];int
i;floatl,w,h;for(i=0;i<3;i++){cout<<"输入第"<<i+1<<"个长方体的长,宽,高=";cin>>l>>w>>h;bx[i].input_data(l,w,h);}for(i=0;i<3;i++){cout<<
"第"<<i+1<<"个长方体:";bx[i].show_volume();}return0;}输入第1个长方体的长,宽,高=123输入第2个长方体的长,宽,高=456输入第3个长方体的长,宽,高=789第1个长方体
:长方体的体积=6第2个长方体:长方体的体积=120第3个长方体:长方体的体积=504Pressanykeytocontinue另一种做法:intmain(){Boxboxa,boxb,boxc;floatl,w,h;cout<<"输入第1个长方体的长,宽,高=";cin>>l>>w>>h
;boxa.input_data(l,w,h);cout<<"输入第2个长方体的长,宽,高=";cin>>l>>w>>h;boxb.input_data(l,w,h);cout<<"输入第1个长方体的长,宽,高=";cin>>l>>w>>h;
boxc.input_data(l,w,h);cout<<"第1个长方体:";boxa.show_volume();cout<<"第2个长方体:";boxb.show_volume();cout<<"第3个长方体:";boxc.show_volume();retur
n0;}