类与对象
【学习要点】
1.掌握面向过程程序设计的基本思想。
2.掌握面向对象程序设计的基本思想。
3.掌握面向对象程序设计的特点━━抽象性、封装性、继承性、多态性
4.掌握类的概念和定义方法。
5.掌握类成员的访问属性。
6.掌握对象的概念和定义方法。
7.掌握对象成员的引用。
------------------------------------------------------------------------------------------------------------------------------------------------- 【例题分析】
1.下面程序段中的错误有几处?
class A
{ int a = 1 ;
int b ; }
A a , b ;
【答案】2
【解析】
①在类中不允许初始化成员数据。
②类定义的结束处要加分号。
2.下面程序中的错误有几处?
# include
class Time
{int hour , minite , second ;
public :
void print() ;
void set ( int h , int m , int s ) ;
};
void print( )
{ cout << hour << " :" << minite << " :" << second << endl ; }
void Time :: set ( int h , int m , int s )
{ hour = h ; minite = m ; second = s ; } ;
void main()
{ Time a ;
a.set( 5 , 10 , 20 ) ;
a. print() ;
cout << a.hour << " :" << a.minite << " :" << a.second << endl ;
}
【答案】2
【解析】
①在类外定义成员函数,应在成员函数名前加“类名:: ”。
②在private 后面声明的成员数据和成员函数,只允许本类中的成员函数访问,而任何类外的函数均不能访问。若在类体的起始处声明私有成员,关键字private 可以省略不写。
------------------------------------------------------------------------------------------------------------------------------------------------- 【思考题】
㈠选择题
1.以下有关类的描述,不正确的是______。
A.类是一种用户自定义的数据类型
B.只有本类中的成员函数才能引用本类中的私有数据
C.在类中,若不作权限说明,成员数据为私有类型
D.在类中,若不作权限说明,成员函数为公有类型
【答案】???D
2.以下有关对象的描述,不正确的是______。
A.对象是类的一个实例
B.任何一个对象只能属于一个某个具体的类
C.一个类只能有一个对象
D.类和对象的关系与数据类型和变量的关系相似
【答案】???C
3.以下有关成员函数的描述,不正确的是______。
A.成员函数一定是内联函数
B.成员函数可以重载
C.成员函数可以设置参数默认值
D.成员函数可以是私有的
【答案】???A
4.面向对象程序设计的核心是类和对象,而类=______。
A.数据+ 对数据操作的函数
B.对象+ 对象+ ……+ 对象
C.算法+ 数据结构
D.对象+ 算法
【答案】???A
5.C++是______。
A.面向对象的程序设计语言
B.结构化的程序设计语言
C.既是面向对象的程序设计语言,又是结构化的程序设计语言
D.非结构化的程序设计语言
【答案】???C
6.以下有关对象的描述,不正确的是______。
A.对象只能描述有形的东西
B.对象具有属性和行为(服务)两个主要因素
C.现实世界中的一切事物(问题)都可以看作对象
D.对象的属性是其静态特征,对象的行为是其动态特征,对象的属性只能由本对象的行为来操作
【答案】???A
7.以下有关类与对象的描述,不正确的是______。
A.类是对象的类,对象是类的对象
B.系统不为类分配内存空间,但为对象分配内存空间
C.对象是类的抽象描述
D.类可以看作数据类型,对象可以看作类类型的变量
【答案】???C
8.类的封装性体现在用户只能通过公共接口来使用类中私有的成员数据,这里的“公共接口”指的是______。
A.类中所有的成员函数
B.构造函数
C.类中公有成员函数
D.类外函数
【答案】???C
9.以下有关类的描述,正确的是______。
A.在程序执行时,C++为类的对象分配存储空间
B.只有在定义了类的对象后,C++才给类分配存储空间
C.一个类的所有对象(实例)共享同一块内存区域
D.在程序执行时,C++为类分配存储空间
【答案】??? A
-------------------------------------------------------------------------------------------------------------------------------------------------㈡填空题
1.类中允许指定的三种访问权限是:______、______、______。
【答案】??? private
【答案】??? public
【答案】??? protected
2.类中成员默认的访问权限是:______。
【答案】??? private
3.类中定义的______、______访问权限的成员数据不能被类外的普通函数引用。
【答案】??? private
【答案】??? protected
------------------------------------------------------------------------------------------------------------------------------------------------- ㈢编程题
1.编写程序:存放在myDate.cpp文件中,包含内容如下:
①定义日期类Date。私有成员数据有:存放年、月、日值的year、month、day。公有成员函数有:可设置日期的成员函数void set( int y , int m , int d ) ;可进行加1天操作的成员函数void add( ) ;可按“2008/12/12”格式输出的成员函数void show( )。
②编写main()函数去测试Date类。
编写程序如下:
【答案】???
#include
#include
using namespace std;
#define cout fout
ofstream fout("output.txt");
int Emonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool leapyear(int yy)
{
if ( (yy%4==0 && yy%100!=0) || (yy%400==0) ) return true;
return false;
}
class Date
{
int year,month,day;
public:
void set(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void add()
{
if (leapyear(year)) Emonth[2]=29;
else Emonth[2]=28;
if (day+1>Emonth[month])
{
day=1;
if (month+1>12)
{
year++;
month=1;
}
else month++;
}
else day++;
}
void show()
{
cout< } }; int main() { Date n; int a,b,c; cin>>a>>b>>c; n.set(a,b,c); for (int i=1;i<=367;i++) { n.show(); n.add(); } return 0; } 2.(选做)编写程序:将第1题的程序分离成: ①Date类声明,存放在Date.h头文件中。 ②Date类成员函数的类外定义,存放在Date.cpp文件中,并单独编译产生Date.obj文件。 ③main()主函数,存放在myDate.cpp文件中,并单独编译产生myDate.obj文件。 ④将整个程序组织成多文件结构,然后连接运行。注意:两个cpp文件中都需要包含类声明的头文件Date.h。编写程序如下: 【答案】??? //myDate.cpp #include #include "Date.h" using namespace std; int main() { Date n; int a,b,c; cin>>a>>b>>c; n.set(a,b,c); for (int i=1;i<=367;i++) { n.show(); n.add(); } return 0; } //Date.h class Date { int year,month,day; public: void set(int y,int m,int d); void add(); void show(); }; //Date.cpp #include "Date.h" #include using namespace std; int Emonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; bool leapyear(int yy) { if ( (yy%4==0 && yy%100!=0) || (yy%400==0) ) return true; return false; } void Date::set(int y,int m,int d) { year=y; month=m; day=d; } void Date::add() { if (leapyear(year)) Emonth[2]=29; else Emonth[2]=28; if (day+1>Emonth[month]) { day=1; if (month+1>12) { year++; month=1; } else month++; } else day++; } void Date::show() { std::cout< 3.编写程序:存放在myStudent.cpp文件中,包含内容如下: ①定义学生类Student。私有成员数据有:存放学号、姓名、成绩值的id、name、score。公有成员函数有:可键盘输入学生资料的成员函数void input( ) ;可按“学号=04081111 姓名=张小笑成绩=99”格式输出的成员函数void output( )。 ②编写main()函数去测试Student类。 编写程序如下: 【答案】??? #include using namespace std; class Student { int stuid,score; char name[10]; public: void input() { cin>>stuid>>name>>score; } void output() { cout<<"学号="< } }; int main() { Student stu[100]; int n; cin>>n; for (int i=1;i<=n;i++) stu[i].input(); for (int i=1;i<=n;i++) stu[i].output(); return 0; } 4.(选做)编写程序:将第3题的程序分离成: ①Student类声明,存放在Student.h头文件中。 ②Student类成员函数的类外定义,存放在Student.cpp文件中,并单独编译产生Student.obj文件。 ③main()主函数,存放在myStudent.cpp文件中,并单独编译产生myStudent.obj文件。 ④将整个程序组织成多文件结构,然后连接运行。注意:两个cpp文件中都需要包含类声明的头文件Student.h。编写程序如下: 【答案】??? -------------------------------------------------------------------------------------------------------------------------------------------------