ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

第3章 【例题】(完整版)

2026/8/15 9:28:35 拓冰建站 浏览量
第3章 【例题】(完整版)

目录

前言

【例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】有关成绩结构体的例子 

//3.1有关成绩结构体的例子
#include <iostream>
using namespace std;
struct Score            //声明了一个名为Score的结构体
{   int mid_exam;int fin_exam;
};
int main()
{Score score1;score1.mid_exam=80;//可以在结构体外直接访问数据mid_examscore1.fin_exam=88;//可以在结构体外直接访问数据fin_examcout<<"期中成绩:"<<score1.mid_exam<<"\n期末成绩:"<<score1.fin_exam<<"\n总评成绩:"<<(int)(0.3*score1.mid_exam+0.7*score1.fin_exam)<<endl;return 0;
}

【例3.2】使用Score类的完整程序

//3.2使用Score类的完整程序
#include <iostream>
using namespace std;
class Score{public:void setScore(int m,int f) {mid_exam=m;fin_exam=f;}void showScore() {cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;}private:int mid_exam;       //私有数据成员int fin_exam;       //私有数据成员
};
int main()
{Score op1,op2;          //定义对象op1和op2;op1.setScore(80,88);//调用op1的成员函数setScore(),给op1的数据成员赋值op2.setScore(90,92);//调用对象op2的成员函数setScore(),给op2的数据成员赋值op1.showScore();//调用op1的成员函数showScore()op2.showScore();//调用op的成员函数showScore()return 0;
}

【例 3.3】一个存在错误的程序

//3.3一个存在错误的程序
#include <iostream>
using namespace std;
class Score{public:void setScore(int m,int f){mid_exam=m;fin_exam=f;}void showScore(){cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;}private:int mid_exam;       //私有数据成员int fin_exam;       //私有数据成员
};
int main()
{Score op1,op2;          //定义对象op1和op2;op1.setScore(80,88);//调用op1的成员函数setScore(),给op1的数据成员赋值op2.setScore(90,92);//调用对象op2的成员函数setScore(),给op2的数据成员赋值
//	cout<<"\n期中成绩:"<<op1.mid_exam<<"\n期末成绩"<<op1.fin_exam<<endl;
//从类的外部访问对象的私有成员是错误的,除非公有数据成员op1.showScore();//调用op1的成员函数showScore()op2.showScore();//调用op的成员函数showScore()Score op3,*ptr;ptr=&op3;(*ptr).setScore(90,98);ptr->showScore();return 0;
}

 

【例3.4】用对象赋值语句的例子 

//3.4用对象赋值语句的例子#include <iostream>
using namespace std;
class Score{public:void setScore(int m,int f) {mid_exam=m;fin_exam=f;}void showScore() {cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;}private:int mid_exam;       //私有数据成员int fin_exam;       //私有数据成员
};
int main()
{Score op1,op2;          //定义对象op1和op2;op1.setScore(80,88);//调用op1的成员函数setScore(),给op1的数据成员赋值op2=op1;//将对象op1数据成员的值赋给对象op2op2.showScore();return 0;
}

 

【例3.5】为类Score定义一个构造函数

//3.5为类Score定义一个构造函数
#include <iostream>
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<<"构造函数使用中..."<<endl;mid_exam=m;fin_exam=f;
}
int main()
{}

【例3.6】建立对象的同时,用构造函数给数据成员赋初值

//3.6建立对象的同时,用构造函数给数据成员赋初值
#include <iostream>
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<<"构造函数使用中..."<<endl;mid_exam=m;fin_exam=f;
}
void Score::setScore(int m,int f)
{mid_exam=m;fin_exam=f;
}
inline void Score::showScore()
{cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
int main()
{Score score1(80,88);//定义类Score的对象score1,自动调用构造函数//给对象score1的数据成员赋初值cout<<endl<<"成绩输出:";score1.showScore();//调用成员函数showScore(),显示score1的数据score1.setScore(90,92);cout<<endl<<"成绩输出:";score1.showScore();return 0;
}

【例3.7】用成员初始化列表对数据成员进行初始化

//3.7用成员初始化列表对数据成员进行初始化
#include <iostream>
using namespace std;
class A{public:A(int x1):x(x1),rx(x),pi(3.14)//用成员初始化列表对引用类型的数据成员赋值{}void print()//rx和const修饰的数据成员pi进行初始化{cout<<"x="<<x<<"  "<<"rx="<<rx<<"  "<<"pi="<<pi<<endl;}private:int x;int& rx;//rx是整形变量的引用const double pi;//pi是用const修饰的常量
};
int main()
{A a(10);a.print();return 0;
}

【例3.8】类成员初始化的顺序

//3.8类成员初始化的顺序
#include <iostream>
using namespace std;
class D{public:D(int i):mem2(i),mem1(mem2+1)//是按照mem1,mem2的顺序(数据成员声明的顺序){                            //进行初始化的。mem1先进行初始化,由于mem2还未被//初始化,所以此时是个随机数cout<<"mem1: "<<mem1<<endl;cout<<"mem2: "<<mem2<<endl;}private:int mem1;int mem2;
};
int main()
{D d(15);return 0;
}

【例3.9】带有默认参数的构造函数

//3.9带有默认参数的构造函数
#include <iostream>
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<<"构造函数使用中..."<<endl;
}
void Score::setScore(int m,int f)
{mid_exam=m;fin_exam=f;
}
inline void Score::showScore()
{cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
int main()
{Score op1(80,88);//传递两个实参Score op2(90);//只传递了一个实参,第二个参数用默认值Score op3;//没有传递实参,全部用默认值op1.showScore();op2.showScore();op3.showScore();return 0;
}

 

【例3.10】含有构造函数和析构函数的Score类

//3.10含有构造函数和析构函数的Score类
#include <iostream>
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<<"构造函数使用中..."<<endl;
}
Score::~Score()//定义析构函数
{cout<<endl<<"析构函数使用中..."<<endl;}
void Score::setScore(int m,int f)
{mid_exam=m;fin_exam=f;
}
inline void Score::showScore()
{cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
int main()
{Score score1(80,88);//定义类Score的对象Score1,自动调用构造函数,给对象Score的数据成员赋初值cout<<endl<<"成绩输出:";score1.showScore();//调用成员函数showScore(),显示score1的数据score1.setScore(90,92);cout<<endl<<"成绩输出:";score1.showScore();//调用成员函数showScore(),显示score1的数据return 0;//这条语句之后,析构函数自动被调用
}

【例3.11】较完整的学生类例子

//3.11较完整的学生类例子
#include <iostream>
#include<string>
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<<endl<<"析构函数使用中..."<<endl;
}
void Student::modify(float score1)
{score=score1;
}
void Student::show()
{cout<<"姓名:"<<name<<endl;cout<<"学号:"<<stu_no<<endl;cout<<"分数:"<<score<<endl;
}
int main()
{Student stu1("黎明","201502021",90);//定义类Student的对象stu1,调用构造函数,初始化对象stu1stu1.show();//调用成员函数show(),显示stu1的数据stu1.modify(88);//调用成员函数modify(),修改stu1的数据cout<<"修改后:-------"<<endl;stu1.show();//调用成员函数show(),显示stu1修改后的数据
}

 

可以正常运行,但编译有警告 

【例3.12】正确对数据成员赋初值

//3.12正确对数据成员赋初值
#include <iostream>
using namespace std;class Myclass{public:int no;
};
int main()
{Myclass a;a.no=2015;cout<<a.no<<endl;return 0;
}

 

如果将程序修改为:

//将3.12程序修改
#include <iostream>
using namespace std;
class Myclass{public:int no;
};
int main()
{Myclass a;cout<<a.no<<endl;return 0;
}

 

也许我用的是新版编译器,没有出现像书上那样的报错

实际上可以理解为调用了默认的构造函数给数据成员赋初值,默认的构造函数一般只负责给对象分配存储空间,而不承担给数据成员赋初值的任务。

但这个代码如果加上一个带参数的的构造函数一定错误。 因为:

在类中一旦定义了带参数的构造函数,系统将不再提供无参的默认的构造函数,需要自己再重新定义一个无参的构造函数以代替默认的构造函数。

如下:

#include <iostream>
using namespace std;
class Myclass{public:Myclass(int no1){no=no1;}int no;
};
int main()
{Myclass a;cout<<a.no<<endl;return 0;
}

编译错误提示: 

而加上一个无参的构造函数或者定义对象时给定初值就没问题了。 

【例3.13】用初始值列表初始化公有数据成员

//3.13用初始值列表初始化公有数据成员
#include <iostream>
using namespace std;
class Myclass{public:char name[10];int no;
};
int main()
{Myclass a={"chen",25};cout<<a.name<<"  "<<a.no<<endl;return 0;
}

【例3.14】分析下列程序的运行结果

//3.14分析下列程序的运行结果
#include <iostream>
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<<"构造函数使用中..."<<endl;
}
Score::~Score()//定义析构函数
{   cout<<endl<<"析构函数使用中..."<<endl;  }
void Score::setScore(int m,int f)
{mid_exam=m;fin_exam=f;
}
inline void Score::showScore()
{cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
int main()
{Score score1;//定义类Score的对象score1,自动调用构造函数score1.setScore(80,88);//给类对象score1的数据成员赋初值cout<<endl<<"成绩输出";score1.showScore();return 0;
}
//当类中定义了带有参数的构造函数之后,系统将不再给它提供默认的构造函数
//解决办法:
//1. 再类中添加一个无参数的构造函数
//Score()
//{ }
//2.或在主函数中给对象参数
//int main()
//{
//	Score score(80,88);
//}

 

【例3.15】构造函数的重载

//【3.15】构造函数的重载
#include <iostream>
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<<"构造..."<<endl;mid_exam=m;fin_exam=f;
}
Score::Score()//定义无参数的构造函数
{   cout<<"构造函数使用中..."<<endl;    }
Score::~Score()
{cout<<endl<<"析构函数使用中..."<<endl;
}
void Score::setScore(int m, int f)
{mid_exam=m;fin_exam=f;
}inline void Score::showScore()
{cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}int main()
{Score score1;//定义类Score的对象score1,调用无参构造函数score1.setScore(80,88);//给对象score1的数据成员赋初值cout<<endl<<"成绩输出:";score1.showScore();//显示score1的数据Score score2(90,92);//定义义类Score的对象score2,调用有参构造函数cout<<endl<<"成绩输出:";score2.showScore();//显示score2的数据return 0;
}

【例3.16】计时器的实现

//3.16计时器的实现
#include <iostream>
#include<stdlib.h>
#include<string>
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<<"时间="<<seconds<<"秒"<<endl;	}private:int seconds;
};
int main()
{Timer a,b(10),c("20"),d(1,10);a.showtime();b.showtime();c.showtime();d.showtime();
}

【例3.17】自定义拷贝构造函数

//3.17自定义拷贝构造函数
#include <iostream>
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<<"构造函数使用中..."<<endl;mid_exam=m;fin_exam=f;
}
Score::Score()//定义无参数的构造函数
{}
Score::~Score()//定义析构函数
{	cout<<"析构函数使用中...\n";}
Score::Score(const Score &p)//自定义的拷贝构造函数
{mid_exam=2*p.mid_exam;fin_exam=2*p.fin_exam;cout<<"拷贝构造函数使用中...\n";
}
void Score::setScore(int m,int f)
{mid_exam=m;fin_exam=f;
}inline void Score::showScore()
{cout<<"\n期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
int main()
{Score score1(90,92);//定义类Score的对象,调用有参构造函数Score score2(score1);//拷贝构造函数,拷贝构造函数只在创建对象时被调用
//	Score score2=score1;//作用是一样的cout<<endl<<"成绩输出...";score1.showScore();
//	cout<<endl<<"成绩输出...";
//	score2.showScore();return 0;
}

 

【例3.18】默认拷贝构造函数的调用

//3.18默认拷贝构造函数的调用
#include <iostream>
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期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
int main()
{Score p1(80,88);//定义对象p1,调用普通构造函数初始化对象p1Score p2(p1);//以代入法调用了默认的拷贝函数,用对象p1初始化对象p2Score p3=p1;//以赋值法调用默认的拷贝函数,用对象p1初始化p3p1.showScore();p2.showScore();p3.showScore();cout<<endl;return 0;
}

 

【例3.19】调拷贝构造函数的三种情况用

//3.19调用拷贝构造函数的三种情况
#include <iostream>
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期中成绩:"<<mid_exam<<"\n期末成绩:"<<fin_exam<<"\n";cout<<"总评成绩:"<<(int)(0.3*mid_exam+0.7*fin_exam)<<endl;
}
void fun1(Score p)//形参是对象,在这里调用了拷贝构造函数。
{p.showScore();
}
Score fun2()
{Score p4(80,88);return p4;//renturn时,调用了拷贝构造函数,将p4拷贝倒匿名对象中。
}
int main()
{Score p1(80,88);//定义对象p1,第1次调用普通的构造函数p1.showScore();Score p2(p1);//第一次调用拷贝构造函数,用对象p1初始化对象p2p2.showScore();Score p3=p1;//第二次调用拷贝构造函数p3.showScore();fun1(p1);//对象p1作为fun1()的实参,第三次调用拷贝构造函数p2=fun2();//在哪函数fun()2内部定义对象时,第2次调用普通的构造函数//函数的返回值是对象,第4次调用拷贝构造函数,//但在匿名对象给p2赋值时,不会调用拷贝构造函数,函数结束时,//匿名对象也随之消失p2.showScore();return 0;
}

【例3.20】分析下列程序的运行结果

//3.20分析下列程序的运行结果
#include <iostream>
using namespace std;
class Coord{public:Coord(int a=0,int b=0);//声明构造函数Coord(const Coord &p);//声明拷贝构造函数~Coord(){	cout<<"析构函数使用中...\n";	}void print(){	cout<<x<<" "<<y<<endl;	}int getX(){	return x;	}int getY(){	return y;	}private:int x,y;
};
Coord::Coord(int a,int b)//定义构造函数
{x=a;y=b;cout<<"构造函数使用中...\n";
}
Coord::Coord(const Coord &p)//定义拷贝构造函数
{x=p.x;y=p.y;cout<<"拷贝构造函数使用中...\n";
}
Coord fun(Coord p)//函数fun()的形参是对象
{cout<<"函数使用中...\n";int a,b;a=p.getX()+10;b=p.getY()+20;Coord r(a,b);return r;//函数fun()的返回值是对象
}
int main()
{Coord p1(30,40);Coord p2;Coord p3(p1);p2=fun(p3);p2.print();return 0;
}

 

【例3.21】有关浅拷贝的例子

//3.21有关浅拷贝的例子
#include <iostream>
#include <string>
using namespace std;
class Student{public:Student(char *name1,float score1);~Student();private:char *name;//学生姓名float score;//学生成绩};
Student::Student(char *name1,float score1)
{cout<<"构造函数使用中..."<<name1<<endl;name=new char[strlen(name1)+1];if(name!=0){strcpy(name,name1);score=score1;}
}
Student::~Student()
{cout<<"析构函数使用中..."<<name<<endl;name[0]='\0';delete []name;
}
int main()
{Student stu1("黎明",90);//调用构造函数Student stu2=stu1;//调用默认的拷贝构造函数return 0;
}

 

【例3.22】有关深拷贝的例子

//3.22有关深拷贝的例子
#include <iostream>
#include <string>
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<<"构造函数使用中..."<<name1<<endl;name=new char[strlen(name1)+1];if(name!=0){strcpy(name,name1);score=score1;}
}
Student::Student(Student& stu)
{cout<<"拷贝构造函数使用中..."<<stu.name<<endl;name=new char[strlen(stu.name)+1];if(name!=0){strcpy(name,stu.name);score=stu.score;}
}
Student::~Student()
{cout<<"析构函数使用中..."<<name<<endl;name[0]='\0';delete []name;
}
int main()
{Student stu1("黎明",90);//调用构造函数Student stu2=stu1;//调用默认的拷贝构造函数return 0;
}