目录
前言
【例3.1】有关成绩结构体的例子
【例3.2】使用Score类的完整程序
【例 3.3】一个存在错误的程序
【例3.4】用对象赋值语句的例子
【例3.5】为类Score定义一个构造函数
【例3.6】建立对象的同时,用构造函数给数据成员赋初值
【例3.7】用成员初始化列表对数据成员进行初始化
【例3.8】类成员初始化的顺序
【例3.9】带有默认参数的构造函数
【例3.10】含有构造函数和析构函数的Score类
【例3.11】较完整的学生类例子
【例3.12】正确对数据成员赋初值
【例3.13】用初始值列表初始化公有数据成员
【例3.14】分析下列程序的运行结果
【例3.15】构造函数的重载
【例3.16】计时器的实现
【例3.17】自定义拷贝构造函数
【例3.18】默认拷贝构造函数的调用
【例3.19】调拷贝构造函数的三种情况用
【例3.20】分析下列程序的运行结果
【例3.21】有关浅拷贝的例子
【例3.22】有关深拷贝的例子
基于教材:c++面向对象程序设计(第三版)陈维兴 林小茶 编著 第三章的所有例题
//3.1有关成绩结构体的例子
#include
using namespace std;
struct Score //声明了一个名为Score的结构体
{ int mid_exam;
int fin_exam;
};
int main()
{
Score score1;
score1.mid_exam=80;//可以在结构体外直接访问数据mid_exam
score1.fin_exam=88;//可以在结构体外直接访问数据fin_exam
cout<<"期中成绩:"<
//3.2使用Score类的完整程序
#include
using namespace std;
class Score{
public:
void setScore(int m,int f) {
mid_exam=m;
fin_exam=f;
}
void showScore() {
cout<<"\n期中成绩:"<
//3.3一个存在错误的程序
#include
using namespace std;
class Score{
public:
void setScore(int m,int f)
{
mid_exam=m;
fin_exam=f;
}
void showScore()
{
cout<<"\n期中成绩:"<showScore();
return 0;
}
//3.4用对象赋值语句的例子
#include
using namespace std;
class Score{
public:
void setScore(int m,int f) {
mid_exam=m;
fin_exam=f;
}
void showScore() {
cout<<"\n期中成绩:"<
//3.5为类Score定义一个构造函数
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明构造函数Score()的原型
void setScore(int m,int f);
void showScore();
private:
int mid_exam;
int fin_exam;//私有数据成员
};
Score::Score(int m,int f)//定义构造函数Score()
{
cout<<"构造函数使用中..."<
//3.6建立对象的同时,用构造函数给数据成员赋初值
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明构造函数Score()的原型
void setScore(int m,int f);
void showScore();
private:
int mid_exam;
int fin_exam;//私有数据成员
};
Score::Score(int m,int f)//定义构造函数Score()
{
cout<<"构造函数使用中..."<
//3.7用成员初始化列表对数据成员进行初始化
#include
using namespace std;
class A{
public:
A(int x1):x(x1),rx(x),pi(3.14)//用成员初始化列表对引用类型的数据成员赋值
{}
void print()//rx和const修饰的数据成员pi进行初始化
{
cout<<"x="<
//3.8类成员初始化的顺序
#include
using namespace std;
class D{
public:
D(int i):mem2(i),mem1(mem2+1)//是按照mem1,mem2的顺序(数据成员声明的顺序)
{ //进行初始化的。mem1先进行初始化,由于mem2还未被
//初始化,所以此时是个随机数
cout<<"mem1: "<
//3.9带有默认参数的构造函数
#include
using namespace std;
class Score{
public:
Score(int m=0,int f=0);//声明构造函数Score()的原型
void setScore(int m,int f);
void showScore();
private:
int mid_exam;//私有数据成员
int fin_exam;//私有数据成员
};
Score::Score(int m,int f): mid_exam(m),fin_exam(f)//定义构造函数Score()
{
cout<<"构造函数使用中..."<
//3.10含有构造函数和析构函数的Score类
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明构造函数Score()的原型
~Score();//声明析构函数
void setScore(int m,int f);
void showScore();
private:
int mid_exam;//私有数据成员
int fin_exam;//私有数据成员
};
Score::Score(int m,int f): mid_exam(m),fin_exam(f)//定义构造函数Score()
{
cout<<"构造函数使用中..."<
//3.11较完整的学生类例子
#include
#include
using namespace std;
class Student{
public:
Student(char *name1,char *stu_no1,float score1);//声明构造函数
~Student();//声明析构函数
void modify(float score1);//成员函数,用以修改数据
void show();//成员函数,用以显示数据
private:
char *name;//学生姓名
char *stu_no;//学生学号
float score;//学生成绩
};
Student::Student(char *name1,char *stu_no1,float score1)//定义构造函数
{
name=new char[strlen(name1)+1];
strcpy(name,name1);
stu_no=new char[strlen(stu_no1)+1];
strcpy(stu_no,stu_no1);
score=score1;
}
Student::~Student()//定义析构函数
{
delete []name;
delete []stu_no;
cout<
可以正常运行,但编译有警告
//3.12正确对数据成员赋初值
#include
using namespace std;
class Myclass{
public:
int no;
};
int main()
{
Myclass a;
a.no=2015;
cout<
如果将程序修改为:
//将3.12程序修改
#include
using namespace std;
class Myclass{
public:
int no;
};
int main()
{
Myclass a;
cout<
也许我用的是新版编译器,没有出现像书上那样的报错
实际上可以理解为调用了默认的构造函数给数据成员赋初值,默认的构造函数一般只负责给对象分配存储空间,而不承担给数据成员赋初值的任务。
但这个代码如果加上一个带参数的的构造函数一定错误。 因为:
当在类中一旦定义了带参数的构造函数,系统将不再提供无参的默认的构造函数,需要自己再重新定义一个无参的构造函数以代替默认的构造函数。
如下:
#include
using namespace std;
class Myclass{
public:
Myclass(int no1)
{
no=no1;
}
int no;
};
int main()
{
Myclass a;
cout<
编译错误提示:
而加上一个无参的构造函数或者定义对象时给定初值就没问题了。
//3.13用初始值列表初始化公有数据成员
#include
using namespace std;
class Myclass{
public:
char name[10];
int no;
};
int main()
{
Myclass a={"chen",25};
cout<
//3.14分析下列程序的运行结果
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明构造函数Score()的原型
~Score();//声明析构函数
void setScore(int m,int f);
void showScore();
private:
int mid_exam;//私有数据成员
int fin_exam;//私有数据成员
};
Score::Score(int m,int f): mid_exam(m),fin_exam(f)//定义构造函数Score()
{
cout<<"构造函数使用中..."<
//【3.15】构造函数的重载
#include
using namespace std;
class Score{
public:
Score(int m, int f);//声明有参数的构造函数
Score();//声明无参数的构造函数
~Score();//声明析构函数
void setScore(int m,int f);
void showScore();
private:
int mid_exam;//私有数据成员
int fin_exam;//私有数据成员
};
Score::Score(int m,int f)//定义有参数的构造函数
{
cout<<"构造..."<
//3.16计时器的实现
#include
#include
#include
using namespace std;
class Timer{
public:
Timer()//定义无参数的构造函数,将seconds初始化为零
{ seconds=0; }
Timer(char* t)//定义一个含数字串参数的构造函数
{ seconds=atoi(t); }//atoi函数功能:将数字串转为整形
Timer(int t)//定义一个含整形参数的构造函数
{ seconds=t; }
Timer(int min,int sec)//定义含两个整形参数的构造函数
{ seconds=min*60+sec; }
int showtime()
{ cout<<"时间="<
//3.17自定义拷贝构造函数
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明有参数的构造函数
Score();//声明无参数的构造函数
Score(const Score &p);//自定义拷贝构造函数
//拷贝构造函数是函数名与类名相同,参数是同类对象的引用
~Score();//声明析构函数
void setScore(int m,int f);
void showScore();
private:
int mid_exam;
int fin_exam;
};
Score::Score(int m,int f)//定义有参数的构造函数
{
cout<<"构造函数使用中..."<
//3.18默认拷贝构造函数的调用
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明有参数的构造函数
~Score();
void showScore();
private:
int mid_exam;
int fin_exam;//私有数据成员
};
Score::Score(int m,int f)
{
cout<<"构造函数使用中...";
mid_exam=m;
fin_exam=f;
}
Score::~Score() { cout<<"析构函数使用中...\n"; }
void Score::showScore()
{
cout<<"\n期中成绩:"<
//3.19调用拷贝构造函数的三种情况
#include
using namespace std;
class Score{
public:
Score(int m,int f);//声明有参数的构造函数
Score(const Score &p);
void showScore();
private:
int mid_exam;
int fin_exam;
};
Score::Score(int m,int f)
{
cout<<"构造函数使用中...";
mid_exam=m;
fin_exam=f;
}
Score::Score(const Score &p)//自定义拷贝构造函数
{
mid_exam=p.mid_exam;
fin_exam=p.fin_exam;
cout<<"拷贝构造函数使用中...\n";
}
void Score::showScore()
{
cout<<"\n期中成绩:"<
//3.20分析下列程序的运行结果
#include
using namespace std;
class Coord{
public:
Coord(int a=0,int b=0);//声明构造函数
Coord(const Coord &p);//声明拷贝构造函数
~Coord()
{ cout<<"析构函数使用中...\n"; }
void print()
{ cout<
//3.21有关浅拷贝的例子
#include
#include
using namespace std;
class Student{
public:
Student(char *name1,float score1);
~Student();
private:
char *name;//学生姓名
float score;//学生成绩
};
Student::Student(char *name1,float score1)
{
cout<<"构造函数使用中..."<
//3.22有关深拷贝的例子
#include
#include
using namespace std;
class Student{
public:
Student(char *name1,float score1);
Student(Student& stu);
~Student();
private:
char *name;//学生姓名
float score;//学生成绩
};
Student::Student(char *name1,float score1)
{
cout<<"构造函数使用中..."<