第十章编程练习

本文展示了如何定义和实现不同类,如银行账户、个人、高尔夫球员、销售记录和移动。每个类包括数据成员和成员函数,如构造函数、显示信息、存款/取款、设置/获取属性等。通过示例程序,演示了如何创建对象并使用这些类的方法,展示了面向对象编程的基本概念和应用。
摘要由CSDN通过智能技术生成

1.为复习题5描述的类提供方法定义,并编写一个小程序来演示所有的特性。
定义一个类来表示银行账户。数据成员包括储户姓名、账号(使用字符串)和存款。成员函数执行如下操作:
创建一个对象并将其初始化;
显示储户姓名、账号和存款;
存入参数指定的存款;
取出参数指定的存款;
实现
bankaccount.h

class BankAccount
{
private:
	string name;
	string acctnum;
	double balance;
public:
	BankAccount(string client, string num, double bal = 0.0);
	void Show(void) const;
	void Deposit(double cash);
	void Withdram(double cash);
};

bankaccount.cpp

BankAccount::BankAccount(string client, string num, double bal)
{
	name = client;
	acctnum = num;
	balance = bal;
}

void BankAccount::Show(void) const
{
	cout << "The account information:" << endl;
	cout << "Name:" << name << endl;
	cout << "Account:" << acctnum << endl;
	cout << "Balance:" << balance << endl;
}

void BankAccount::Deposit(double cash)
{
	balance += cash;
}

void BankAccount::Withdram(double cash)
{
	balance -= cash;
}

main.cpp

int main(void)
{
	BankAccount bank("Name1", "0000001", 1000);
	bank.Show();

	cout << endl;

	bank.Deposit(500);
	cout << "After deposit:" << endl;
	bank.Show();

	cout << endl;

	bank.Withdram(1500);
	cout << "After withdraw:" << endl;
	bank.Show();

	return 0;
}

2.下面是一个非常简单的类定义:
class Person
{
private:
static const int LIMIT = 25;
string lname;
char fname[LIMIT];
public:
Person() { lname = “”; fname[0] = ‘\0’; }
Person(const string& ln, const char* fn = “Heyyou”);
void Show() const;
void FormalShow()const;
};
它使用了一个string对象和一个字符数组,让您能够比较它们的用法。请提供未定义的方法的代码,以完成这个类的实现。再编写一个使用这个类的程序,它使用了三种可能的构造函数调用(没有参数、一个参数和两个参数)以及两种显示方法。下面是一个使用这些构造函数和方法的例子:
Person one;
Person two(“Smythecraft”);
Person three(“Dimwiddy”, “Sam”);
one.Show();
cout << endl;
one.FormalShow();
实现:
person.cpp

Person::Person(const string& ln, const char* fn)
{
	lname = ln;
	strcpy(fname, fn);
}

void Person::Show() const
{
	if (lname == "" && fname[0] == '\0')
	{
		cout << "No name!" << endl;
	}
	else
	{
		cout << "Person name:" << fname << " " << lname << endl;
	}
}

void Person::FormalShow() const
{
	if (lname == "" && fname[0] == '\0')
	{
		cout << "No name!" << endl;
	}
	else
	{
		cout << "Person name:" << lname << ", " << fname << endl;
	}
}

main.cpp

	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");

	one.Show();
	one.FormalShow();
	cout << endl;

	two.Show();
	two.FormalShow();
	cout << endl;

	three.Show();
	three.FormalShow();
	cout << endl;

3.完成第9章的编程练习1,但要用正确的golf类声明替换那里的代码。用带合适参数的构造函数替换setgolf(golf&, const char*,int),以提供初始值。保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造函数来创建一个临时对象,并将其赋给调用对象,即*this)。
实现
golf.h

const int Len = 40;
class golf
{
public:
	golf(const char* name, int hc);
	golf();
	void sethandicap(int hc);
	void showgolf() const;
private:
	char fullname[Len];
	int handicap;
};

golf.cpp

#include "golf.h"

golf::golf(const char* name, int hc)
{
	strcpy(this->fullname, name);
	this->handicap = hc;
}

golf::golf()
{
	cout << "The fullname is:";
	cin.getline(fullname, Len);

	cout << "The handicap is:";
	cin >> handicap;

	cin.get();//消耗回车
}

void golf::sethandicap(int hc)
{
	this->handicap = hc;
}

void golf::showgolf() const
{
	cout << "The name of golf is:" << fullname << endl;
	cout << "The handicap of golf is:" << handicap << endl;
}

main.cpp

int main(void)
{
	golf g1("rick", 100);
	golf g2;
	cout << endl;

	cout << "Now show all the golf players:" << endl;
	g1.showgolf();
	g2.showgolf();
	
	return 0;
}

4.完成第9章的编程练习4,但将Sales结构及相关的函数转换为一个类及其方法。用构造函数替换setSales(Sale&, double[], int)函数。用构造函数实现setSales(Sales&)方法的交互版本。将类保留在名称空间SALES中。
实现:
sales.h

#pragma once

namespace SALES
{
	const int QUARTERS = 4;
	class Sales
	{
	private:
		double sales[QUARTERS];
		double average;
		double max;
		double min;
	public:
		Sales(const double ar[], int n);
		Sales();
		void ShowSales() const;
	};
}

sales.cpp

namespace SALES
{
	Sales::Sales(const double ar[], int n)
	{
		double total = 0.0;
		int i = 0;


		for (; i < n && i < 4; ++i)
		{
			sales[i] = ar[i];
			total += sales[i];
		}

		average = total / i;
		max = sales[0];
		min = sales[0];

		for (int j = 1; j < i; ++j)
		{
			max = (max > sales[j]) ? max : sales[j];
			min = (min < sales[j]) ? min : sales[j];
		}

		if (n < 4)
		{
			for (int k = n; k < 4; k++)
			{
				sales[k] = 0;
			}
		}
	}

	Sales::Sales()
	{
		double total = 0.0;
		cout << "Enter 4 sales quarters:" << endl;
		for (int i = 0; i < QUARTERS; i++)
		{
			cout << "#" << i + 1 << ":";
			cin >> sales[i];
			total += sales[i];

			if (i == 0)
			{
				max = sales[i];
				min = sales[i];
			}
			else
			{
				max = (max > sales[i]) ? max : sales[i];
				min = (min < sales[i]) ? min : sales[i];
			}
		}
		average = total / QUARTERS;
	}

	void Sales::ShowSales() const
	{
		cout << "The 4 sales are:";
		for (int i = 0; i < QUARTERS; i++)
		{
			cout << sales[i] << " ";
		}
		cout << endl;
		cout << "The average is:" << average << endl;
		cout << "The max value is:" << max << endl;
		cout << "The min value is:" << min << endl;
	}
}

main.cpp

int main(void)
{
	double arr[] = { 11.1, 22.2, 33.3, 44.4 };

	Sales s1(arr, QUARTERS);
	Sales s2;
	cout << endl;
	cout << "Now show all the sales:" << endl;
	s1.ShowSales();
	s2.ShowSales();
	
	return 0;
}

5.考虑下面的结构声明:
struct customer
{
char fullname[35];
double payment;
};
编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不作修改:只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。
实现:
stacker.h

struct customer
{
	char fullname[35];
	double payment;
};

typedef customer Item;

class Stack
{
private:
	enum {MAX = 10};
	Item items[MAX];
	int top;
public:
	Stack();
	bool isempty() const;
	bool isfull() const;
	bool push(const Item& item);
	bool pop(Item& item);
};

stacker.cpp

Stack::Stack()
{
	top = 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(Item& item)
{
	if (top > 0)
	{
		item = items[--top];
		return true;
	}
	else
	{
		return false;

	}
}

main.cpp

int main(void)
{
	char ch;
	customer cust;
	Stack st;
	double sum = 0.0;

	cout << "Please enter A to push to stack, \n"
		<< "P to pop from stack, Q to quit.\n";

	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
		{
			continue;
		}

		switch (ch)
		{
		case 'A':
		case 'a':
			cout << "Enter the customer fullname you want to push to stack" << endl;
			cin.getline(cust.fullname, 35);
			cout << "Enter the customer payment you want to push to stack" << endl;
			cin >> cust.payment;
			if (st.isfull())
			{
				cout << "stack already full" << endl;
			}
			else
			{
				st.push(cust);
			}
			break;
		case'P':
		case'p':
			if (st.isempty())
			{
				cout << "stack already empty" << endl;
			}
			else
			{
				st.pop(cust);
				sum += cust.payment;
				cout << cust.fullname << " is popped" << endl;
				cout << cust.payment << " is popped" << endl;
				cout << "The payment sum is " << sum << endl;
			}
			break;
		}
		cout << "Please enter A to add purchase order,\n"
			<< "P to process a PO, or Q to quit.\n";
	}
	cout << "Bye!" << endl;
	return 0;
}

6.下面是一个类声明
class Move
{
private:
double x;
double y;
public:
Move(double a = 0, double b = 0);
showmove() const;
Move add(const Move& m)const;
reset(double a = 0, double b = 0);
};
请提供成员函数的定义和测试这个类的程序。
实现:
move.h

class Move
{
private:
	double x;
	double y;
public:
	Move(double a = 0, double b = 0);
	void showmove() const;
	Move add(const Move& m)const;
	void reset(double a = 0, double b = 0);
};

move.cpp

Move::Move(double a, double b)
{
	x = a;
	y = b;
}

void Move::showmove() const
{
	cout << "x = " << x << ", y = " << y << endl;
}

Move Move::add(const Move& m) const
{
	Move temp;
	temp.x = x + m.x;
	temp.y = y + m.y;
	return temp;
}

void Move::reset(double a, double b)
{
	x = a;
	y = b;
}

main.cpp

int main(void)
{
	double x, y;
	Move m1(11.1, 22.2);
	Move m2(1.0, 1.0);
	Move m3;

	m1.showmove();
	cout << "Enter new x and y:";
	cin >> x >> y;
	cout << "Reset m1:" << endl;
	m1.reset(x, y);
	m1.showmove();
	cout << "m2 is:" << endl;
	m2.showmove();
	m3 = m2.add(m1);
	cout << "m3 is:" << endl;
	m3.showmove();

	return 0;
}

7.Betelgeuseam plorg有这些特征
数据:
plorg的名称不超过19个字符;
plorg有满意指数(CI),这是一个整数。
操作:
新的plorg将有名称,其CI值为50;
plorg的CI可以修改;
plorg可以报告其名称和CI;
plorg的默认名称为“Plorg”。
请编写一个plorg类声明(包含数据成员和成员函数原型)来表示plorg,并编写成员函数的定义。然后编写一个小程序,以演示plorg类的所有特性。
实现:
plorg.h

const int SIZE = 19;
class Plorg
{
private:
	char name[SIZE];
	int CI;
public:
	Plorg(const char str[] = "Plorga",  int ci = 50);
	void reset(int ci);
	void show() const;
};

plorg.cpp

Plorg::Plorg(const char str[], int ci)
{
	strcpy(name, str);
	CI = ci;
}

void Plorg::reset(int ci)
{
	CI = ci;
}

void Plorg::show() const
{
	cout << "The name is:" << name << ", The CI is:" << CI << endl;
}

main.cpp

int main(void)
{
	Plorg p1;
	p1.show();
	p1.reset(90);
	p1.show();

	return 0;
}

8.可以将简单列表描述成下面这样:
可存储0或多个某种类型的列表;
可创建空列表;
可在列表中添加数据项;
可确定列表是否为空;
可确定列表是否为满;
可访问列表中的每一个数据项,并对它执行某种操作。
可以看到,这个列表确实很简单,例如,它不允许插入或删除数据项。
请设计一个List类来表示这种抽象类型。您应提供文件list.h和文件list.cpp,前者包含定义,后者包含类实现的方法。您应还创建一个简短的程序来使用这个类。
该列表的规范很简单,这主要旨在简化这个编程练习。可以选择使用数组或链表来实现该列表,但公有接口不应依赖于所做的选择。也就是说,公有接口不应有数组索引、节点指针等。应使用通用概念来表达创建列表、在列表中添加数据项等操作。对于访问数据项以及执行操作,通常应使用将函数指针作为参数的函数来处理:
void visit(void (*pf)(Item &));
其中,pf指向一个将Item引用作为参数的函数(不是成员函数),Item是列表中数据项的类型。visit()函数将该函数用于列表中的每个数据项。
实现:
list.h

#pragma once

typedef unsigned long Item;

void visit_item(Item& item);

class List
{
private:
	enum { MAX = 10 };
	Item items[MAX];
	int top;
public:
	List();
	bool isempty() const;
	bool isfull() const;
	bool add(Item& item);
	void visit(void (*pf)(Item&));
};

list.cpp

List::List()
{
	top = 0;
}

bool List::isempty() const
{
	return top == 0;
}

bool List::isfull() const
{
	return top == MAX;
}

bool List::add(Item& item)
{
	if (top < MAX)
	{
		items[top++] = item;
		return true;
	}
	else
	{
		return false;
	}
}

void List::visit(void(*pf)(Item&))
{
	for (int i = 0; i < top; i++)
	{
		pf(items[i]);
	}
}

void visit_item(Item& item)
{
	cout << "item = " << item << endl;
}

main.cpp

int main(void)
{
	List list;
	Item num;
	for (int i = 0; i < 5; i++)
	{
		cout << "Please enter a number:";
		cin >> num;
		list.add(num);
	}
	list.visit(visit_item);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值