1.
bank.h
#include <string> using namespace std; class BankAccount { private: std::string m_name;//若是不用命名空间的话,就需要加std:: string m_id; double m_money; public: BankAccount(); BankAccount(const string & name,const string & id,double money=0.0); void show() const; void add(double money); void sub(double money); };
bank.cpp
#include <iostream> #include "bank.h" //构造函数 BankAccount::BankAccount(){ m_name = ""; m_id = ""; m_money = 0.0; } BankAccount::BankAccount(const string & name,const string & id,double money){ m_name = name; m_id = id; m_money = money; } void BankAccount::show() const{ ios_base::fmtflags orig=cout.setf(ios_base::fixed,ios_base::floatfield);//设置cout对象的一个标记,命令cout使用定点表示法 streamsize prec=cout.precision(2); cout<< "Name: " << m_name << " ID: " << m_id << " Money: " << m_money << endl; //因为bank.h中已经声明了命名空间,所以不需要加std:: } void BankAccount::add(double money){ m_money += money; } void BankAccount::sub(double money){ m_money -= money; }
main.cpp
#include "bank.h" void main(){ BankAccount a("MR.Zhang","20135908",8888888.88); a.show(); a.add(88888.88); a.show(); a.sub(88888.88); a.show(); system("pause"); }
//person.h #include <string> class Person { private: static const int LIMIT = 25; std::string lname; char fname[LIMIT]; public: Person() { lname = ""; fname[0] = '