charpter10_homework.cpp

#include<iostream>
#include"bank_account.h"
#include"Person.h"
#include"golf.h"
#include"sales.h"
#include "stack.h"//引入栈
#include "move.h"
#include "plorg.h"
#include "List.h"
using namespace std;
using namespace SALES;
//函数原型
void p10_1(void);
void p10_2(void);
void p10_3(void);
void p10_4(void);
void p10_5(void);
void p10_6(void);
void p10_7(void);
void p10_8(void);
//主函数
int main()
{//p10_1();//p10_2();//p10_3();//p10_4();//p10_5();//p10_6();//p10_7();p10_8();return 0;
}
//------------作业一-------
void p10_1(void)
{//显示调用构造函数BankAccount b1=BankAccount("AAA","123",100);BankAccount b2("BBB","456",200);BankAccount b3;b1.show();b2.show();b3.show();b1.withdraw(20);b1.show();b2.withdraw(500);b2.show();}//------------作业二---------
void p10_2(void)
{Person one;Person two("Smythecraft");Person three("Dimwiddy","Sam");one.Show();two.FormalShow();three.Show();
}//-----------作业三--------------
void p10_3(void)
{Golf g1;g1.showgolf();Golf g2("FIRST",10);g2.showgolf();g2.setgolf();g2.showgolf();g2.hdicap(2500);g2.showgolf();
}//------------作业四------------
void p10_4(void)
{Sales s1;s1.showsales();double arr[4]={1,2,3,4};Sales s2(arr,4);s2.showsales();}//-------------作业五--------void p10_5(void)
{Stack st;//创建一个栈double total=0.0;const customer c1={"AAA",20.0};const customer c2={"BBB",40.0};if(st.push(c1))cout<<"c1 push!\n";elsecout<<"Full\n";if(st.push(c2))cout<<"c2 push!\n";elsecout<<"Full\n";customer c3,c4;if(st.pop(c3)){cout<<"c3 pop!\n";total++;}    elsecout<<"Empty!\n";if(st.pop(c4)){cout<<"c4 pop!\n";total++;}elsecout<<"Empty!\n";cout<<c3.fullname<<" "<<c3.payment<<endl;cout<<c4.fullname<<" "<<c4.payment<<endl;cout<<total<<endl;
}//----------作业六-----------------
void p10_6(void)
{Move m1;m1.showmove();m1.reset(2,2);m1.showmove();Move m2(3,3);m2=m1.add(m2);m2.showmove();
}//--------作业七-----------
void p10_7(void)
{Plorg p1;p1.show();p1.setCI(10);p1.show();char arr[]="apple";Plorg p2(arr);p2.show();
}//-------作业八--------
void p10_8(void)
{List list1;//创建一个空列表unsigned long temp;for(int i=0;i<10;i++){temp=i+1;list1.add(temp);}list1.visit(revise);
}

bank_account.h

#ifndef BANK_ACCOUNT_H_
#define BANK_ACCOUNT_H_#include<string>
using namespace std;class BankAccount
{
private://默认属性static const int MAX=50;char m_name[MAX];string m_account;double m_money;
public://公共属性//自定义构造函数BankAccount(char* name,const string str,const double m);//默认构造函数BankAccount();//析构函数,无生命类型~BankAccount();//显示函数void show()const;//取出参数指定款项void withdraw(double m);
};#endif 

bank_account.cpp

#include<iostream>
#include<string>
#include<cstring>
#include"bank_account.h"
using namespace std;//自定义构造函数
BankAccount::BankAccount(char* name,const string str,const double m)
{strcpy(m_name,name);m_account=str;m_money=m;
}
//默认构造函数
BankAccount::BankAccount()
{strcpy(m_name,"NULL");m_account="0";m_money=0.0;
}
//析构函数,无声明返回类型
BankAccount::~BankAccount()
{cout<<"Bye! "<<m_name<<endl;
}
//显示函数
void BankAccount::show()const
{cout<<"name: "<<m_name<<endl;cout<<"count: "<<m_account<<endl;cout<<"money: "<<m_money<<endl;
}
//取出参数指定款项
void BankAccount::withdraw(double m)
{if(m<0)cout<<"False!\n";else if(m>m_money)cout<<"You don't have these money!\n";else{cout<<"You have withdraw "<<m<<endl;m_money-=m;}
}

Person.h

#ifndef PERSON_H_
#define PERSON_H_#include <string>class Person
{
private:static const int LIMIT=25;std::string lname;char fname[LIMIT];
public://此处为内联函数,原型加定义Person(){lname="";fname[0]='\0';};//构造函数Person(const std::string& ln,const char* fn="Heyyou");//默认构造函数void Show()const;void FormalShow()const;
};#endif

Person.cpp

#include<iostream>
#include "Person.h"
using namespace std;#include<cstring>//自定义构造函数
//!!!!
//C++规定参数的默认值只可以出现在函数声明中,不可以出现在函数的定义中,否则会出现参数重复定义默认参数的错误
Person::Person(const std::string& ln,const char* fn)
{lname=ln;strcpy(fname,fn);
}
void Person::Show()const
{for(int i=0;i<LIMIT && fname[i]!='\0';i++)cout<<fname[i];cout<<" , "<<lname<<endl;
}
void Person::FormalShow()const
{cout<<lname<<" , ";for(int i=0;i<LIMIT && fname[i]!='\0';i++)cout<<fname[i];cout<<endl;
}

golf.h

#ifndef GOLF_H_
#define GOLF_H_#include <cstring>class Golf
{
private:static const int Len=40;//类中声明的符号常量char fullname[Len];int handicap;
public://默认构造函数,没有参数,允许创建一个对象,无参数,无初始化Golf(){fullname[0]='\0';handicap=0;};//自定义构造函数,空或者默认参数,只能在原型中,内联函数,原型+定义Golf(const char* name,int hc){strcpy(fullname,name);handicap=hc;};//setgolf交互版本void setgolf();void hdicap(int hc);void showgolf()const;//析构函数~Golf(){;};
};#endif

golf.cpp

#include<iostream>
#include<string>
#include"golf.h"
using namespace std;//setgolf交互版本
void Golf::setgolf()
{cout<<"Enter your fullname: ";char fname[Len];cin.getline(fname,Len);//读取一行字符cout<<"Enter handicap:";int hdcap;cin>>hdcap;while(cin.get()!='\n')//读取cin>>留在输入缓冲行里continue;//调用构造函数创建临时对象,并将其赋给调用对象*this=Golf(fname,hdcap);
}void Golf::hdicap(int hc)
{handicap=hc;
}
void Golf::showgolf()const
{cout<<"fullname: "<<fullname<<endl;cout<<"handicap: "<<handicap<<endl;
}

sales.h

#ifndef SALES_H_
#define SALES_H_//SALE名称空间
namespace SALES
{const int QUARTERS=4;class Sales{private:double sales[QUARTERS];double average;double max;double min;public://自定义构造函数Sales(const double arr[],int n);//默认构造函数Sales();void showsales()const;//析构函数~Sales(){;};};
}#endif

sales.cpp

#include<iostream>
#include"sales.h"
using namespace std;
using namespace SALES;SALES::Sales::Sales()
{cout<<"Enter the sales: \n";for(int i=0;i<QUARTERS;i++){cout<<"# "<<i+1<<": ";cin>>sales[i];}max=sales[0];min=sales[0];double sum=0;for(int i=0;i<QUARTERS;i++){if(max<sales[i])max=sales[i];if(min>sales[i])min=sales[i];sum+=sales[i];}average=sum/QUARTERS;
}Sales::Sales(const double arr[],int n)
{if(n<=QUARTERS){max=arr[0];min=arr[0];double sum=0;for(int i=0;i<n;i++){sales[i]=arr[i];if(max<arr[i])max=arr[i];if(min>arr[i])min=arr[i];sum+=arr[i];}average=sum/n;}else cout<<"n is too big!\n";
}void Sales::showsales()const
{for(int i=0;i<QUARTERS;i++)cout<<sales[i]<<" ";cout<<endl;cout<<"average: "<<average<<endl;cout<<"max: "<<max<<endl;cout<<"min: "<<min<<endl;
}

stack.h

#ifndef STACK_H_
#define STACK_H_struct customer
{char fullname[35];double payment;
};typedef customer Item;class Stack
{
private://enum {MAX=10};//符号常量,在类中,使用枚举,不算是数据成员static const int Max=10;Item items[Max];//使用数组来形成栈int top;
public:Stack();//构造函数~Stack();//析构函数bool is_empty()const;bool is_full()const;bool push(const Item& item);bool pop(Item & item);
};#endif

stack.cpp

#include<iostream>
#include"stack.h"//默认构造函数
Stack::Stack()
{top=0;
}
//析构函数
Stack::~Stack()
{std::cout<<"Bye!"<<std::endl;
}
bool Stack::is_empty()const
{return top==0;
}
bool Stack::is_full()const
{return top==Max;
}
bool Stack::push(const Item& item)
{if(top<Max){items[top++]=item;return true;}elsereturn false;
}
bool Stack::pop(Item& item)
{if(top>0){item=items[--top];return true;}elsereturn false;
}

move.h

#ifndef MOVE_H_
#define MOVE_H_class Move
{
private:double x;double y;
public://默认构造函数,要么没有参数,要么默认参数Move(double a=0,double b=0);//默认参数只能出现在原型中void showmove()const;Move add(const Move& m)const;void reset(double a=0,double b=0);//默认参数重置
};#endif

move.cpp

#include<iostream>
#include"move.h"using std::cout;
using std::endl;
//默认构造函数,要么没有参数,要么默认参数
Move::Move(double a,double b)//默认参数只能出现在原型中
{x=a;y=b;
}
void Move::showmove()const
{cout<<"x= "<<x<<endl;cout<<"y= "<<x<<endl;
}
Move Move::add(const Move& m)const
{Move m1;m1.x=this->x+m.x;m1.y=this->y+m.y;return m1;
}
void Move::reset(double a,double b)//默认参数重置
{x=a;y=b;
}

plorg.h

#ifndef PLORG_H_
#define PLORG_H_static const int Size=20;
class Plorg
{
private:char m_name[Size];int CI;
public://默认构造函数,默认参数原型中声明Plorg(char name[Size]="Plorga");//修改CIvoid setCI(int n);//展示void show()const;//析构函数~Plorg(){;};
};#endif

plorg.cpp

#ifndef PLORG_H_
#define PLORG_H_static const int Size=20;
class Plorg
{
private:char m_name[Size];int CI;
public://默认构造函数,默认参数原型中声明Plorg(char name[Size]="Plorga");//修改CIvoid setCI(int n);//展示void show()const;//析构函数~Plorg(){;};
};#endif

List.h

#ifndef LIST_H_
#define LIST_H_typedef unsigned long Item1;
class List
{
private:static const int Max1=10;Item1 items[Max1];int top;
public:List();bool isempty()const;bool isfull()const;bool add(Item1& item);//函数指针作为参数void visit(void (*pf)(Item1& item));
};
//非成员函数---数据+100
void revise(Item1& n);#endif

List.cpp

#include<iostream>
#include"List.h"
using namespace std;
List::List()
{top=0;//创建一个空对象
}
bool List::isempty()const
{return top==0;
}
bool List::isfull()const
{return top==Max1;
}
bool List::add(Item1& item)
{if(!isfull()){*(items+top)=item;top++;return true;}elsereturn false;
}
//非成员函数---数据+100
void revise(Item1& n)
{n=n+100;cout<<n<<endl;
}//函数指针作为参数,visit只有一个参数,接收一个函数指针
void List::visit(void (*pf)(Item1& item))
{for(int i=0;i<Max1;i++){cout<<"# "<<i+1<<" : ";(*pf)(items[i]);//或者 pf(items[i])}
}

如有错误,欢迎指正!

C++ Primer Plus(第6版)---编程作业完成(第十章)相关推荐

  1. C Primer Plus 第六版编程练习第五章答案

    1,编写一个程序,把用分钟表示的时间转换成用小时和分钟表示的时间.使用#define或const创建一个表示60的符号常量或const变量.通过while循环让用户重复输入值,直到用户输入小于或等于0 ...

  2. C++ Primer Plus 第六版编程练习——第6章

    ★★★★★备注★★★★★ 使用的编译环境为 Visual Studio 2017 默认省略了如下内容: #include "stdafx.h"                    ...

  3. C Primer Plus 第六版---编程练习2

    1.编写一个程序,调用一次 printf()函数,把你的姓名打印在一行.再调用一次 printf()函数,把你的姓名分别打印在两行.然后,再调用两次printf()函数,把你的姓名打印在一行.输出应如 ...

  4. C Primer Plus 第六版---编程练习4

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. /*输入名和姓打印"名,姓" */ #include<stdio.h> #d ...

  5. C Primer Plus 第六版编程练习第七章答案

    1,编写一个程序读取输入,读到#字符停止,然后报告读取空格数,换行符数目以及所有的其它字符数目. /*7.12*/ #include<stdio.h> #define STOP '#' # ...

  6. C Primer Plus 第六版 编程练习第四章答案 最新出炉

    文章目录 1,编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. 2,编写一个程序,提示用户输入名字,并执行以下操作: 3,编写一个程序,读取一个浮点数,首先以小数 ...

  7. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  8. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  9. C Primer Plus第六版(中文版)编程练习答案(完美修订版)汇总

    //本文是博主编写的C Primer Plus第六版(中文版)编程练习答案的所有链接; //使用超链接汇总于此,若是有用请点赞收藏并分享给他人; C Primer Plus 第六版(中文版)第二章(完 ...

  10. C++ Primer Plus(第6版)Chapter 4 编程题答案

    C++ Primer Plus(第6版)Chapter 4 编程题答案 第1题: // task 1 #include <iostream> #include <string> ...

最新文章

  1. 使用proxy来调用未定义的属性方法
  2. 轻量级数据库Sqlite的使用
  3. toeplitz--生成托普利兹矩阵
  4. 使用 Spring Boot CLI 运行第一个Spring boot程序
  5. [C#] 等待启动的进程执行完毕
  6. warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY, key ID
  7. 2017年秋招二十套前端面试题分享
  8. shell中用bc进行浮点运算(转帖)
  9. MyEclipse中常用的快捷键
  10. SSM整合简单登录案例
  11. python设置单元格宽度_Python xlwt-访问现有单元格内容,自动调整列宽
  12. log4j的使用 20210719091111861
  13. 第一个冲刺周期(第四天)
  14. 【Elasticsearch】ElasticSearch Cluster的一致性问题
  15. DOM操作与引用资源的前后关系
  16. AVG游戏《裂缝》策划案
  17. php laravel手册,Laravel 8.x 简体中文最新手册指南
  18. 质数 素数 合数 因子
  19. QQ一键登录助手_DedeCMS插件_适合FOR V55 V56 V57_GBK.zip
  20. 目标检测经典论文——Fast R-CNN论文翻译(中英文对照版):Fast R-CNN(Ross Girshick, Microsoft Research(微软研究院))

热门文章

  1. Oracle经典sql语句总结@sql-plus重点函数串讲与sql语句案例@中文排序详讲).doc
  2. 技术创新激活行业增长新引擎!腾讯助力互联网企业服务新发展
  3. 使用51单片机的矩阵键盘和LCD1602做一个密码锁
  4. JDBC向数据库中插入BLOB类型数据
  5. 关于c++使用TTS写的朗读软件
  6. 荣耀耳机的实质跃进:Earbuds 3 Pro
  7. 一加到1亿。C语言_钢铁侠主角成一加品牌推广大使 一加7系列全力冲刺高端市场...
  8. bzoj 1218 [HNOI2003]激光炸弹
  9. 机器学习sklearn之特征工程
  10. Excel基础—为什么学习Excel