C++ Primer Plus 第十章_对象与类_答案(答案自己编写,测试稳定)

program 1

//bank.h
#include<string>
#ifndef  __BANK_H__
#define __BANK_H__
using namespace std;
class BankAccount {
    
    
private:
	string name;
	string acctnum;
	double balance;
public :
	BankAccount(const string& client, const string& num, double bal = 0.0);
	void show(void)const;
	void deposit(double cash);
	void withdraw(double cash);
};
#endif // ! __BANK_H__
//bank.cpp
#include<string>
#include<iostream>
#include "bank.h"
using namespace std;
BankAccount::BankAccount(const string& client, const string& num, double bal) {
    
    
	name = client;
	acctnum = num;
	balance = bal;
}
void BankAccount::show() const {
    
    
	cout << "Name: " << name << ", AccountNumber: " << acctnum << " , Balance: " << balance << endl;
}
void BankAccount::deposit(double cash) {
    
    
	balance += cash;
}
void BankAccount::withdraw(double cash) {
    
    
	if (balance< cash) {
    
    
		cout << "Not enough money , $" << balance << " is left " << endl;
		return;
	}
	else {
    
    
		balance -= cash;
	}
	
}
//main.cpp
#include<string>
#include<iostream>
#include "bank.h"
using namespace std;
int main() {
    
    
	BankAccount ba("RogerLee", "AC123456",10000.00);
	ba.show();
	ba.deposit(5000.00);
	ba.show();
	ba.withdraw(15000.00);
	ba.show();
	ba.withdraw(15000.00);
	ba.show();
	return 0;
}

program 2

//person.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Person {
    
    
private :
	static const int  LIMIT = 25;
	string last_name;
	char first_name[LIMIT];
public:
	Person() {
    
    
		last_name = "";
		first_name[0] = '\0';
	}
	Person(const string& ln, const char* fn = "HeyYou");
	void Cozy_Show() const;
	void Formal_Show() const;
};
//person.cpp
#include "person.h"
using namespace std;
Person::Person(const string& ln, const char* fn ) {
    
    
	last_name = ln;
	int i;
	for (i = 0; *(fn + i) != '\0'; i++) {
    
    
		first_name[i] = *(fn + i);
	}
	first_name[i] = '\0';
}
void Person::Cozy_Show() const {
    
    
	cout << last_name << " " << first_name << endl;
}
void Person::Formal_Show() const {
    
    
	cout << first_name << " " << last_name << endl;
}
//main.cpp
#include<iostream>
#include<string>
#include "person.h"
int main() {
    
    
	Person one;
	Person two("Roger");
	Person three("Roger", "Lee");
	one.Cozy_Show();
	one.Formal_Show();
	two.Cozy_Show();
	two.Formal_Show();
	three.Cozy_Show();
	three.Formal_Show();
	return 0;
}

program 5

//stack.h
#ifndef  __STACK_H__
#define  __STACK_H__
//typedef unsigned long Item;
struct customer {
    
    
	char fullname[35];
	double payment;
};
typedef customer Item;
class Stack {
    
    
private:
	enum {
    
    MAX =10};
	Item items[MAX];
	int top;
	double total;
public:
	Stack();
	bool isEmpty() const;
	bool isFull() const;
	bool push(const Item& item);
	bool pop();
	void show();
};
#endif // ! __STACK_H__

//stack.cpp
#include<iostream>
#include "stack.h"
Stack::Stack() {
    
    
	top = 0;
	total = 0.0;
}
bool Stack::isEmpty() const {
    
    
	return top == 0;
}
bool Stack::isFull() const {
    
    
	return top == MAX;
}
bool Stack::push(const Item& item) {
    
    
	if (top < MAX) {
    
    
		items[top++] = item;
		return true;
	}
	else {
    
    
		return false;
	}
}
bool Stack::pop() {
    
    
	if (top > 0) {
    
    
		total += items[--top].payment;
		std::cout << "Latest total payment : "<<total << std:: endl;
		return true;
	}
	else {
    
    
		return false;
	}
}
void Stack::show() {
    
    
	if (isEmpty()) {
    
    
		std::cout << "栈是空的"<< std::endl;
		return;
	}
	std::cout << "栈列表: " << std::endl;
	for (int i = 0; i < top; i++) {
    
    
		std::cout << items[i].fullname<<" "<< items[i].payment<<std::endl;
	}
}
//main.cpp
#include<iostream>
#include<cctype>
#include "stack.h"
using namespace std;
int main() {
    
    
	Item i1;
	//strncpy_s
	strcpy_s(i1.fullname, "Roger Lee");
	//(i1.fullname) = { 'R',oger Lee };
	i1.payment = 123.01;
	Item i2;
	strcpy_s(i2.fullname, "Rose Marry");
	i2.payment = 123.01;
	Item i3;
	strcpy_s(i3.fullname, "Jack Doson");
	i3.payment = 123.01;
	Stack st;
	st.show();
	st.push(i1);
	st.show();
	st.push(i2);
	st.show();
	st.push(i3);
	st.show();
	st.pop();
	st.show();
	st.pop();
	st.show();
	st.pop();
	st.show();
	st.pop();
	st.show();
	return 0;
}

program 6

//movethis.h
#pragma once 
#include<iostream>
using namespace std;
class Move {
    
    
private :
	double x;
	double y;
public:
	Move(double a = 0, double b = 0);
	void showMove() const;
	Move add(const Move& m);
	void reSet(double a = 0, double b = 0);
};
//movethis.cpp
#include<iostream>
#include "movethis.h"
using namespace std;
Move::Move(double a , double b ) {
    
    
	this->x = a;
	this->y = b;
}
void Move::showMove() const {
    
    
	cout << "X: " << x << "  Y: " << y << endl;
} 
Move Move::add(const Move& m) {
    
    
	this->x += m.x;
	this->y += m.y;
	Move temp(this->x, this->y);
	return temp;
}
void Move::reSet(double a, double b ) {
    
    
	this->x = a;
	this->y = b;
}
//main.cpp
#include<iostream>
#include<string>
#include "movethis.h"
using namespace std;
int main() {
    
    
	Move one(2,8.2);
	one.showMove();
	Move two(2,5);
	two.showMove();
	Move three = one.add(two);
	three.showMove();
	three.reSet();
	three.showMove();
	return 0;
}

program 7

//plorg.h
#pragma once 
#include<iostream>
using namespace std;

class Plorg {
    
    
private:
	enum {
    
     MAX = 19 };
	char name[MAX];
	int CI;
public:
	Plorg(const char * temp_name="Plorga", int temp_CI= 50);
	void reSetCI(int number);
	void show();
};
//plorg.cpp
#include<iostream>
#include "plorg.h"
using namespace std;
Plorg::Plorg(const char* temp_name , int temp_CI) {
    
    
	CI = temp_CI;
	int i;
	for (i = 0; *(temp_name + i) != '\0'; i++) {
    
    
		name[i] = *(temp_name + i);
	}
	name[i] = '\0';
}
void Plorg::reSetCI(int number) {
    
    
	CI = number;
}
void Plorg::show()
{
    
    
	cout << "Name: " << name << " CI: " << CI << endl;
}
//main.cpp
#include<iostream>
#include "plorg.h"
using namespace std;
int main() {
    
    
	Plorg p1;
	p1.show();
	p1.reSetCI(250);
	p1.show();
	Plorg p2("Roger Lee",555);
	p2.show();
	p2.reSetCI(250);
	p2.show();
	return 0;
}

program 8

//list.h
#include<iostream>
#include "plorg.h"
using namespace std;
int main() {
    
    
	Plorg p1;
	p1.show();
	p1.reSetCI(250);
	p1.show();
	Plorg p2("Roger Lee",555);
	p2.show();
	p2.reSetCI(250);
	p2.show();
	return 0;
}
//list.cpp
#include<iostream>
#include<string>
#include "list.h"
using namespace std;
List::List() {
    
    
	length = 0;
}
bool List::addItem(const ElementType data) {
    
    
	if (isFull() == true) {
    
    
		cout << "列表满了" << endl;
		return false;
	}
	this->data[this->length++] = data;
	return true;
}
bool List::isEmpty() const {
    
    
	return length == 0;
}
bool List::isFull() const
{
    
    
	return length == SIZE;
}
void List::visit(void(*pf)(ElementType&))
{
    
    
	for (int i = 0; i < length; i++) {
    
    
		(*pf)(this->data[i]);
	}
}
//main.cpp
#include<iostream>
#include<string>
#include "list.h"
using namespace std;
void print(ElementType& data) {
    
    
	cout << data << endl;
}
int main() {
    
    
	List i;
	i.addItem(1);
	i.addItem(2);
	i.addItem(5);
	i.addItem(7);
	cout << i.isFull() << endl;
	cout << i.isEmpty() << endl;
	void (*pf)(ElementType &);
	pf = print;
	i.visit(pf);

	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_47472749/article/details/115265824