一* 字符串与整型
//account.h --
#include <string>
class Account {
private:
std::string username;
std::string accnum;
int secret;
double balance;
double overdr;
void success() { std::cout << "Successful!\n"; }
public:
Account();
~Account();
int login();
void showInfo();
void deposit();
void withdraw();
void overdraw();
};
//account00.cpp --
#include <string>
#include <iostream>
#include "account.h"
int main() {
std::cout << "****** ATMs ******\n";
Account user;
if (user.login()) {
std::cout << "Choose Service: 1 to Deposit, 2 to Withdraw, 3 to Overdraw, 4 to Balance, 0 to quit\nYour choice: ";
enum choice { quit, deposit, withdraw, overdraw,balance };
short choice;
std::cin >> choice;
while (choice != quit) {
switch (choice) {
case deposit: {
user.deposit();
break;
}
case withdraw: {
user.withdraw();
break;
}
case overdraw: {
user.overdraw();
break;
}
case balance: {
user.showInfo();
break;
}
}
std::cout << "\nChoose Service: 1 to Deposit, 2 to Withdraw, 3 to Overdraw, 4 to Balance, 0 to quit\nYour choice: ";
std::cin >> choice;
}
}
std::cout << "\nThank you for using it and look forward to coming back next time.\n";
return 0;
}
//account01.cpp --
#include <string>
#include <iostream>
#include "account.h"
Account::Account() {
username = "Jusibolder";
accnum = "1000200030004000";
secret = 102030;
balance = 0.0;
overdr = 0;
}
Account::~Account() {}
int Account::login() {
using namespace std;
string acnum;
int sec;
short i = 0;
cout << "****** Login ******\n";
do {
cout << "Account: ";
cin >> acnum;
cout << "Secret: ";
cin >> sec;
if (acnum == accnum && sec == secret) {
success();
cout << endl;
return 1;
}
else {
++i;
cout << "\nThe account number or password is incorrect, You have " << 3 - i << " more times.\n";
acnum = "0";
sec = 0;
}
} while (i < 3);
cout << "\nYour account is locked! Plaease come to the Bank to unlock with your ID Card.\n";
return 0;
}
void Account::deposit() {
std::cout << "****** Deposit ******\n";
double dep;
std::cout << "Please put the cash in the Deposit Slot: ";
std::cin >> dep;
balance += dep;
success();
}
void Account::withdraw() {
std::cout << "****** Withdraw ******\n";
double dep;
std::cout << "Please enter the Withdraw Amount: ";
std::cin >> dep;
if (dep < 0)
std::cout << "You Balence is not enough!";
else {
balance -= dep;
success();
}
}
void Account::overdraw() {
double ove;
std::cout << "****** Overdraw ********\nPlease enter the Overdraw amount: ";
std::cin >> ove;
overdr += ove;
success();
}
void Account::showInfo() {
std::cout << "****** Personal Information ******\n"
"Username: " << username <<
"\nAccount: " << accnum <<
"\nBalance: " << balance <<
"\nOverdraw: " << overdr;
}
二* string与char*
//person.h --
#ifndef PERSON_H_
#define PERSON_H_
#include<iostream>
class Person {
private:
static const short LIMIT = 25;
std::string lname;
char fname[LIMIT];
public:
Person() { lname = ""; fname[0] = '\0';}
Person(const char* fn);
Person(const std::string& ln, const char* fn = "Heyyou");
~Person(){}
void Show()const;
void FormalShow()const;
};
#endif // !PERSON_H_
//person01.cpp --
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "person.h"
int main() {
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.Show();
std::cout << std::endl;
one.FormalShow();
std::cout << std::endl;
two.Show();
std::cout << std::endl;
two.FormalShow();
std::cout << std::endl;
three.Show();
std::cout << std::endl;
three.FormalShow();
std::cout << std::endl;
return 0;
}
//person02.cpp --
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "person.h"
Person::Person(const std::string& ln, const char* fn) {
lname = ln;
strcpy(fname, fn);
}
Person::Person(const char* fn) {
strcpy(fname, fn);
}
void Person::Show()const{
std::cout << "Here is your first name and last name: " << fname << " " << lname;
}
void Person::FormalShow()const {
std::cout << "Here is your last name and first name: " << lname<<" "<<fname;
}
三
四
五
六
七