当前位置: 首页 > news >正文

手机网站设计字体多大大连网站哪家做的好

手机网站设计字体多大,大连网站哪家做的好,网络设计软件有哪些,网站上传目录 前言 【例3.1】有关成绩结构体的例子 【例3.2】使用Score类的完整程序 【例 3.3】一个存在错误的程序 【例3.4】用对象赋值语句的例子 【例3.5】为类Score定义一个构造函数 【例3.6】建立对象的同时#xff0c;用构造函数给数据成员赋初值 【例3.7】用成员初始…目录 前言 【例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_exam80;//可以在结构体外直接访问数据mid_examscore1.fin_exam88;//可以在结构体外直接访问数据fin_examcout期中成绩score1.mid_exam\n期末成绩score1.fin_exam\n总评成绩(int)(0.3*score1.mid_exam0.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_examm;fin_examf;}void showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.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_examm;fin_examf;}void showScore(){cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.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_examendl; //从类的外部访问对象的私有成员是错误的除非公有数据成员op1.showScore();//调用op1的成员函数showScore()op2.showScore();//调用op的成员函数showScore()Score op3,*ptr;ptrop3;(*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_examm;fin_examf;}void showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.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的数据成员赋值op2op1;//将对象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_examm;fin_examf; } 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_examm;fin_examf; } void Score::setScore(int m,int f) {mid_examm;fin_examf; } inline void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.7*fin_exam)endl; } int main() {Score score1(80,88);//定义类Score的对象score1,自动调用构造函数//给对象score1的数据成员赋初值coutendl成绩输出;score1.showScore();//调用成员函数showScore(),显示score1的数据score1.setScore(90,92);coutendl成绩输出;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进行初始化{coutxx rxrx pipiendl;}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(mem21)//是按照mem1,mem2的顺序数据成员声明的顺序){ //进行初始化的。mem1先进行初始化由于mem2还未被//初始化所以此时是个随机数coutmem1: mem1endl;coutmem2: mem2endl;}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 m0,int f0);//声明构造函数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_examm;fin_examf; } inline void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.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()//定义析构函数 {coutendl析构函数使用中...endl;} void Score::setScore(int m,int f) {mid_examm;fin_examf; } inline void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.7*fin_exam)endl; } int main() {Score score1(80,88);//定义类Score的对象Score1,自动调用构造函数给对象Score的数据成员赋初值coutendl成绩输出;score1.showScore();//调用成员函数showScore(),显示score1的数据score1.setScore(90,92);coutendl成绩输出;score1.showScore();//调用成员函数showScore(),显示score1的数据return 0;//这条语句之后析构函数自动被调用 } 【例3.11】较完整的学生类例子 //3.11较完整的学生类例子 #include iostream #includestring 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)//定义构造函数 {namenew char[strlen(name1)1];strcpy(name,name1);stu_nonew char[strlen(stu_no1)1];strcpy(stu_no,stu_no1);scorescore1; } Student::~Student()//定义析构函数 {delete []name;delete []stu_no;coutendl析构函数使用中...endl; } void Student::modify(float score1) {scorescore1; } void Student::show() {cout姓名nameendl;cout学号stu_noendl;cout分数scoreendl; } 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.no2015;couta.noendl;return 0; } 如果将程序修改为 //将3.12程序修改 #include iostream using namespace std; class Myclass{public:int no; }; int main() {Myclass a;couta.noendl;return 0; } 也许我用的是新版编译器没有出现像书上那样的报错 实际上可以理解为调用了默认的构造函数给数据成员赋初值默认的构造函数一般只负责给对象分配存储空间而不承担给数据成员赋初值的任务。 但这个代码如果加上一个带参数的的构造函数一定错误。 因为 当在类中一旦定义了带参数的构造函数系统将不再提供无参的默认的构造函数需要自己再重新定义一个无参的构造函数以代替默认的构造函数。 如下 #include iostream using namespace std; class Myclass{public:Myclass(int no1){nono1;}int no; }; int main() {Myclass a;couta.noendl;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};couta.name a.noendl;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()//定义析构函数 { coutendl析构函数使用中...endl; } void Score::setScore(int m,int f) {mid_examm;fin_examf; } inline void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.7*fin_exam)endl; } int main() {Score score1;//定义类Score的对象score1,自动调用构造函数score1.setScore(80,88);//给类对象score1的数据成员赋初值coutendl成绩输出;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_examm;fin_examf; } Score::Score()//定义无参数的构造函数 { cout构造函数使用中...endl; } Score::~Score() {coutendl析构函数使用中...endl; } void Score::setScore(int m, int f) {mid_examm;fin_examf; }inline void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.7*fin_exam)endl; }int main() {Score score1;//定义类Score的对象score1,调用无参构造函数score1.setScore(80,88);//给对象score1的数据成员赋初值coutendl成绩输出;score1.showScore();//显示score1的数据Score score2(90,92);//定义义类Score的对象score2,调用有参构造函数coutendl成绩输出;score2.showScore();//显示score2的数据return 0; } 【例3.16】计时器的实现 //3.16计时器的实现 #include iostream #includestdlib.h #includestring using namespace std; class Timer{public:Timer()//定义无参数的构造函数将seconds初始化为零{ seconds0; }Timer(char* t)//定义一个含数字串参数的构造函数{ secondsatoi(t); }//atoi函数功能将数字串转为整形Timer(int t)//定义一个含整形参数的构造函数{ secondst; }Timer(int min,int sec)//定义含两个整形参数的构造函数{ secondsmin*60sec; }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_examm;fin_examf; } Score::Score()//定义无参数的构造函数 {} Score::~Score()//定义析构函数 { cout析构函数使用中...\n;} Score::Score(const Score p)//自定义的拷贝构造函数 {mid_exam2*p.mid_exam;fin_exam2*p.fin_exam;cout拷贝构造函数使用中...\n; } void Score::setScore(int m,int f) {mid_examm;fin_examf; }inline void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.7*fin_exam)endl; } int main() {Score score1(90,92);//定义类Score的对象调用有参构造函数Score score2(score1);//拷贝构造函数,拷贝构造函数只在创建对象时被调用 // Score score2score1;//作用是一样的coutendl成绩输出...;score1.showScore(); // coutendl成绩输出...; // 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_examm;fin_examf; } Score::~Score() { cout析构函数使用中...\n; } void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.7*fin_exam)endl; } int main() {Score p1(80,88);//定义对象p1调用普通构造函数初始化对象p1Score p2(p1);//以代入法调用了默认的拷贝函数用对象p1初始化对象p2Score p3p1;//以赋值法调用默认的拷贝函数用对象p1初始化p3p1.showScore();p2.showScore();p3.showScore();coutendl;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_examm;fin_examf; } Score::Score(const Score p)//自定义拷贝构造函数 {mid_examp.mid_exam;fin_examp.fin_exam;cout拷贝构造函数使用中...\n; } void Score::showScore() {cout\n期中成绩mid_exam\n期末成绩fin_exam\n;cout总评成绩(int)(0.3*mid_exam0.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 p3p1;//第二次调用拷贝构造函数p3.showScore();fun1(p1);//对象p1作为fun1的实参第三次调用拷贝构造函数p2fun2();//在哪函数fun()2内部定义对象时第2次调用普通的构造函数//函数的返回值是对象第4次调用拷贝构造函数//但在匿名对象给p2赋值时不会调用拷贝构造函数函数结束时//匿名对象也随之消失p2.showScore();return 0; } 【例3.20】分析下列程序的运行结果 //3.20分析下列程序的运行结果 #include iostream using namespace std; class Coord{public:Coord(int a0,int b0);//声明构造函数Coord(const Coord p);//声明拷贝构造函数~Coord(){ cout析构函数使用中...\n; }void print(){ coutx yendl; }int getX(){ return x; }int getY(){ return y; }private:int x,y; }; Coord::Coord(int a,int b)//定义构造函数 {xa;yb;cout构造函数使用中...\n; } Coord::Coord(const Coord p)//定义拷贝构造函数 {xp.x;yp.y;cout拷贝构造函数使用中...\n; } Coord fun(Coord p)//函数fun()的形参是对象 {cout函数使用中...\n;int a,b;ap.getX()10;bp.getY()20;Coord r(a,b);return r;//函数fun()的返回值是对象 } int main() {Coord p1(30,40);Coord p2;Coord p3(p1);p2fun(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构造函数使用中...name1endl;namenew char[strlen(name1)1];if(name!0){strcpy(name,name1);scorescore1;} } Student::~Student() {cout析构函数使用中...nameendl;name[0]\0;delete []name; } int main() {Student stu1(黎明,90);//调用构造函数Student stu2stu1;//调用默认的拷贝构造函数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构造函数使用中...name1endl;namenew char[strlen(name1)1];if(name!0){strcpy(name,name1);scorescore1;} } Student::Student(Student stu) {cout拷贝构造函数使用中...stu.nameendl;namenew char[strlen(stu.name)1];if(name!0){strcpy(name,stu.name);scorestu.score;} } Student::~Student() {cout析构函数使用中...nameendl;name[0]\0;delete []name; } int main() {Student stu1(黎明,90);//调用构造函数Student stu2stu1;//调用默认的拷贝构造函数return 0; }
http://www.pierceye.com/news/809819/

相关文章:

  • 电商网站建设 解决方案的设计营销策略都有哪些方面
  • 菏泽网站建设兼职凡科网制作网站教程
  • 实验一 电子商务网站建设与维护北京网站设计培训学校
  • 周到的网站建设合肥建筑网站大全
  • 国外互联网资讯网站南宁网站制作费用
  • 建设公司网站要注意哪些蜜雪冰城推广软文
  • 做信息安全的网站博客网站的建设
  • 门户网站建设项目书提升学历是什么意思
  • 上海网站建设极简慕枫塘沽有哪些互联网公司
  • 社区网站如何做官方网站建设哪儿有
  • 做兼职的网站策划书大连中山网站建设
  • 中国摄影网站深圳网站建设龙华
  • 个人网站怎么建立深圳网站建站费用
  • 笔趣阁建站教程网页设计 网站建设啥意思
  • 海门网站开发西安响应式网站建设服务提供商
  • 自适应网站建站哈尔滨市建设安全监察网站
  • nas服务器可以做网站吗电商类网站开发方案
  • 免费的个人的网站网站建设 考虑
  • 医院网站建设的目的高端网站有哪些优势
  • 佛山网站建设首选如何备份wordpress
  • 优化稳定网站排名网站建设需要学什么语言
  • 可以做设计私单的网站硬件开发工程师面试
  • 竞价网站单页网页设计师中级证书有用吗
  • 做网站 简单外包wordpress 插件api
  • 白城网站seo新手怎么建立自己网站
  • 建立用模板建立网站wordpress feed
  • 株洲品牌网站建设优质的杭州网站优化
  • 网站开发在哪个科目核算网站平台怎么做的好处
  • 网站底部模板代码江苏建站系统
  • 写出网站开发的基本流程品牌建设网站