1.
#include<iostream>
#include<cstring>
class BankAccount
{
private:
char name[40];
char acctnum[25];
double balance;
public:
BankAccount(const char * client, const char * num, double bal = 0.0);
void show()const;
void deposit(double cash);
void withdraw(double cash);
};
int main()
{
BankAccount Account("jack", "123", 1000);
Account.show();
Account.deposit(100);
Account.show();
Account.withdraw(100);
Account.show();
return 0;
}
BankAccount::BankAccount(const char * client, const char * num, double bal)
{
strcpy_s(name, client);
strcpy_s(acctnum, num);
balance = bal;
}
void BankAccount::show() const
{
using std::cout;
using std::endl;
cout << "name: " << name << endl;
cout << "acctnum: " << acctnum << endl;
cout << "balance: " << balance << endl;
}
void BankAccount::deposit(double cash)
{
using std::cout;
if (cash >= 0)
balance += cash;
else
cout << "请输入一个正数\n";
}
void BankAccount::withdraw(double cash)
{
using std::cout;
if (balance == 0)
cout << "The deposit is 0\n";
else if ((balance - cash) < 0)
cout << "You don't have enough savings\n";
else
balance -= cash;
}
2.
#include<iostream>
#include<cstring>
#include<string>
using std::string;
class Person
{
private:
static const int LIMIT = 25;
string lname;
char fname[25];
public:
Person() {
lname = ""; fname[0] = '\0'; }
Person(const string & ln, const char * fn = "Heyyou");
void Show() const;
void FormalShow() const;
};
int main()
{
using namespace std;
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
cout << endl;
one.FormalShow();
two.Show();
cout << endl;
two.FormalShow();
three.Show();
cout << endl