1、银行账户
#ifndef BANK_H_
#define BANK_H_
#include <string>
namespace bank
{
using std::string;
class Account
{
private:
enum{
LEN = 10};
string name_;
char id_[LEN];
mutable double balance_;
public:
Account(string name = "default", const char* id = "000000000", double deposit = 0.0);
~Account();
void Show()const;
void deposit(double)const;
void withdraw(double)const;
};
}
#endif
#include "Bank.h"
#include <iostream>
namespace bank
{
Account::Account(string name, const char * id, double cash)
{
name_ = name;
strncpy_s(id_, id, LEN);
id_[LEN - 1] = '\0';
balance_ = cash;
}
Account::~Account()
{
}
void Account::Show()const
{
using std::cout;
using std::endl;
cout << "id: " << id_ << '\t' << "name: " << name_ << '\t' << "balance: " << balance_ << endl;
}
void Account::deposit(double cash)const
{
balance_ += cash;
}
void Account::withdraw(double cash)const
{
using std::cout;
if (balance_ < cash)
{
cout << "Your balance (" << balance_ << ") is insufficient!\n";
}
else
{
balance_ -= cash;
}
}
}
#include 'Bank.h'
int main()
{
using bank::Account;
const Account jan("mmmmi", "10010");
jan.Show();
jan.deposit(500);
jan.Show();
jan.withdraw(250.5);
jan.Show();
jan.withdraw(1000);
const Account bob;
bob.deposit(100);
bob.Show();
system("pause");
return 0;
}
2、人的全名
#ifndef PERSON_H_H
#define PERSON_H_H
#include <string>
namespace per
{
using std::string;
class Person
{
private:
static const int LIMIT = 25;
string lname_;
char fname_[LIMIT];
public:
Person();
Person(const string & lname, const char * fname = "default");
void Show()const;
void FormalShow()const;
};
}
#endif
#include "Person.h"
#include <iostream>
namespace per
{
Person::Person()
{
lname_ = "";
fname_[0] = '\0';
}
Person::Person(const string & lname, const char * fname)
{
lname_ = lname;
strncpy_s(fname_, fname, LIMIT);
fname_[LIMIT - 1] = '\0';
}
void Person::Show() const
{
using std::cout;
using std::endl;
cout << fname_ << lname_ << endl;
}
void Person::FormalShow() const
{
using std::cout;
using std::endl;
cout << lname_ << "," << fname_