第10章 对象和类

一* 字符串与整型

在这里插入图片描述

//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;
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孤月小酌

o(* ̄▽ ̄*)ブ谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值