第十章 对象和类
1. 为复习题5描述的类提供方法定义,并编写一个小程序来演示所有的特性
复习题5如下:
定义一个类来表示银行账户。数据成员包括储户姓名、账号(使用字符串)和存款。成员函数执行如下操作:
·创建一个对象并将其初始化
·显示储户姓名、账号和存款
·存入参数指定的存款
·取出参数指定的款项
1.1 使用string类存储姓名及账号
//bank.h
#ifndef bank_H_
#define bank_H_
#include
using namespace std;
class Bank
{
private:
string m_name;
string m_account_number;
double m_deposit;
public:
Bank(); //默认构造函数
Bank(const string name, const string account_number, double deposit); //构造函数
~Bank(); //析构函数
void show() const;
void save(double money);
void expend(double money);
};
#endif
//bank.cpp
#include
#include
#include "bank.h"
using namespace std;
Bank::Bank()
{
m_name = "None";
m_account_number = "None";
m_deposit = 0.0;
}
Bank::Bank(const string name, const string account_number, double deposit)
{
m_name = name;
m_account_number = account_number;
m_deposit = deposit;
}
Bank::~Bank()
{
}
void Bank::show() const
{
cout << "当前账户状态是\n";
cout << "姓名:\t" << m_name << endl;
cout << "账号:\t" << m_account_number << endl;
cout << "存款金额:\t" << m_deposit << endl;
}
void Bank::save(double money)
{
m_deposit += money;
}
void Bank::expend(double money)
{
m_deposit -= money;
}
//usebank.cpp
#include
#include "bank.h"
int main()
{
using namespace std;
string name;
string account_number;
double deposit;
double add;
double minus;
//使用默认构造函数创建一个账户bank1
Bank bank1 = Bank(); //这里显式使用默认构造函数 //也可以隐式使用默认构造函数 Bank bank1;
bank1.show();
//将用户输入的值传递给构造函数,改变bank1的成员的值
cout << "请输入您的姓名:\n";
getline(cin, name);
cout << "请输入您的账号:\n";
getline(cin, account_number);
cout << "请输入您的存款金额:\n";
cin >> deposit;
cin.get();
bank1 = Bank(name, account_number, deposit); //这里构造函数创建一个新的临时的对象,然后将内容复制给bank1
bank1.show();
//直接使用构造函数创建一个账户bank2
Bank bank2 = Bank(name, account_number, deposit); //这里显示使用用构造函数 //也可以隐式使用构造函数 Bank bank2(name, account_number, deposit);
//使用save()存
cout << "请输入您要存储的金额:";
cin >> add;
cin.get();
bank2.save(add);
bank2.show();
//使用expend()取
cout << "请输入您要取款的金额:";
cin >> minus;
cin.get();
bank2.expend(minus);
bank2.show();
return 0;
}
1.2 使用C-风格字符串存储姓名及账号
//bank.h
#ifndef bank_H_
#define bank_H_
class Bank
{
private:
char m_name[20];
char m_account_number[20];
double m_deposit;
public:
Bank(); //默认构造函数
Bank(const char name[], const char account_number[], double deposit); //构造函数
~Bank(); //析构函数
void show() const;
void save(double money);
void expend(double money);
};
#endif
//bank.cpp
#include
#include
#include "bank.h"
using namespace std;
Bank::Bank()
{
strcpy(m_name, "None");
strcpy(m_account_number, "None");
m_deposit = 0.0;
}
Bank::Bank(const char name[], const char account_number[], double deposit)
{
strcpy(m_name, name);
strcpy(m_account_number, account_number);
m_deposit = deposit;
}
Bank::~Bank()
{
}
void Bank::show() const
{
cout << "当前账户状态是\n";
cout << "姓名:\t" << m_name << endl;
cout << "账号:\t" << m_account_number << endl;
cout << "存款金额:\t" << m_deposit << endl;
}
void Bank::save(double money)
{
m_deposit += money;
}
void Bank::expend(double money)
{
m_deposit -= money;
}
//usebank.cpp
#include
#include "bank.h"
int main()
{
using namespace std;
char name[20];
char account_number[20];
double deposit;
double add;
double minus;
//使用默认构造函数创建一个账户bank1
Bank bank1 = Bank(); //这里显式使用默认构造函数 //也可以隐式使用默认构造函数 Bank bank1;
bank1.show();
//将用户输入的值传递给构造函数,改变bank1的成员的值
cout << "请输入您的姓名:\n";
cin.getline(name, 20);
cout << "请输入您的账号:\n";
cin.getline(account_number, 20);
cout << "请输入您的存款金额:\n";
cin >> deposit;
cin.get();
bank1 = Bank(name, account_number, deposit); //这里构造函数创建一个新的临时的对象,然后将内容复制给bank1
bank1.show();
//直接使用构造函数创建一个账户bank2
Bank bank2 = Bank(name, account_number, deposit); //这里显示使用用构造函数 //也可以隐式使用构造函数 Bank bank2(name, account_number, deposit);
//使用save()存
cout << "请输入您要存储的金额:";
cin >> add;
cin.get();
bank2.save(add);
bank2.show();
//使用expend()取
cout << "请输入您要取款的金额:";
cin >> minus;
cin.get();
bank2.expend(minus);
bank2.show();
return 0;
}
1.3 使用string类存储姓名及账号,使用另一种默认构造函数
//bank.h
#ifndef bank_H_
#define bank_H_
#include
using namespace std;
class Bank
{
private:
string m_name;
string m_account_number;
double m_deposit;
public:
Bank(const string name = "None", const string account_number = "None", double deposit = 0.0); //给构造函数提供默认值以实现默认构造函数
~Bank(); //析构函数
void show() const;
void save(double money);
void expend(double money);
};
#endif
//bank.cpp
#include
#include
#include "bank.h"
using namespace std;
Bank::Bank(const string name, const string account_number, double deposit)
{
m_name = name;
m_account_number = account_number;
m_deposit = deposit;
}
Bank::~Bank()
{
}
void Bank::show() const
{
cout << "当前账户状态是\n";
cout << "姓名:\t" << m_name << endl;
cout << "账号:\t" << m_account_number << endl;
cout << "存款金额:\t" << m_deposit << endl;
}
void Bank::save(double money)
{
m_deposit += money;
}
void Bank::expend(double money)
{
m_deposit -= money;
}
//usebank.cpp 说明接受string类的函数也可以接受C-风格字符串
#include
#include "bank.h"
int main()
{
using namespace std;
char name[20];
char account_number[20];
double deposit;
double add;
double minus;
//使用默认构造函数创建一个账户bank1
Bank bank1 = Bank(); //这里显式使用默认构造函数 //也可以隐式使用默认构造函数 Bank bank1;
bank1.show();
//将用户输入的值传递给构造函数,改变bank1的成员的值
cout << "请输入您的姓名:\n";
cin.getline(name, 20);
cout << "请输入您的账号:\n";
cin.getline(account_number, 20);
cout << "请输入您的存款金额:\n";
cin >> deposit;
cin.get();
bank1 = Bank(name, account_number, deposit); //这里构造函数创建一个新的临时的对象,然后将内容复制给bank1
bank1.show();
//直接使用构造函数创建一个账户bank2
Bank bank2 = Bank(name, account_number, deposit); //这里显示使用用构造函数 //也可以隐式使用构造函数 Bank bank2(name, account_number, deposit);
//使用save()存
cout << "请输入您要存储的金额:";
cin >> add;
cin.get();
bank2.save(add);
bank2.show();
//使用expend()取
cout << "请输入您要取款的金额:";
cin >> minus;
cin.get();
bank2.expend(minus);
bank2.show();
return 0;
}
2. 下面是一个非常简单的类定义
class Person
{
private:
static const int LIMIT = 25;
string lname; // Person’s last name
char fname[LIMIT]; // Person’s first name
public:
Person() {lname = ""; fname[0] = '\0';} // #1
Person(const string & ln, const char * fn = "Heyyou"); // #2
// the following methods display lname and fname
void Show() const; // firstname lastname format
void FormalShow() const; // lastname, firstname format
};
它使用了一个string对象和一个字符数组,让您能够比较它们的用法。请提供未定义的方法的代码,以完成这个类的实现。
再编写一个使用这个类的程序,它使用了三种可能的构造函数的调用(没有参数、一个参数和两个参数)以及两种显示方法。
下面是一个使用这些构造函数和方法的例子:
Person one; // use default constructor
Person two("Smythecraft"); // use #2 with one default argument
Person three("Dimwiddy", "Sam"); // use #2, no defaults one.Show();
cout << endl;
one.FormalShow();
// etc. for two and three
//person.h
#ifndef person_H_
#define person_H_
#include
using namespace std;
class Person
{
private:
static const int LIMIT = 25;
string lname; // Person’s last name
char fname[LIMIT]; // Person’s first name
public:
Person() {lname = ""; fname[0] = '\0';} // #1
Person(const string & ln, const char * fn = "Heyyou"); // #2
// the following methods display lname and fname
void Show() const; // firstname lastname format
void FormalShow() const; // lastname, firstname format
};
#endif;
//person.cpp
#include
#include "person.h"
using namespace std;
Person::Person(const string & ln, const char * fn)
{
lname = ln;
strcpy(fname, fn);
}
void Person::Show() const
{
cout << fname << " " << lname << endl;
}
void Person::FormalShow() const
{
cout << lname << "," << fname << endl;
}
//useperson.cpp
#include
#include "person.h"
using namespace std;
int main()
{
Person one; // use default constructor
Person two("Smythecraft"); // use #2 with one default argument 显式Person two = Person("Smythecraft");
Person three("Dimwiddy", "Sam"); // use #2, no defaults one.Show(); 显式Person three = Person("Dimwiddy", "Sam");
cout << endl;
one.FormalShow();
one.Show();
cout << endl;
two.FormalShow();
two.Show();
cout << endl;
three.FormalShow();
three.Show();
return 0;
}
3. 完成第9章的编程练习1,但要用正确的golf类声明替换那里的代码。用带合适参数的构造函数替换setgolf(golf&, const char*, int),以提供初始值。
保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造函数来创建一个临时对象,并将其赋给调用对象,即*this)
第9章的编程练习1,链接:https://blog.csdn.net/weixin_42411044/article/details/86128627
//golf.h
#ifndef golf_H_
#define golf_H_
class Golf
{
private:
static const int Len = 40;
char m_fullname[Len];
int m_handicap;
public:
Golf();
Golf(const char * fullname, int handicap);
int setgolf();
void handicap(int handicap);
void showgolf() const;
};
#endif
//golf.cpp
#include
#include
#include "golf.h"
Golf::Golf()
{
strcpy(m_fullname, "None");
m_handicap = 0;
}
Golf::Golf(const char * fullname, int handicap)
{
strcpy(m_fullname, fullname);
m_handicap = handicap;
}
int Golf::setgolf()
{
using namespace std;
char fullname[Len];
int handicap;
cout << "请输入姓名:";
cin.getline(fullname, Len);
if (strlen(fullname)==0)
return 0;
else
{
cout << "请输入等级:";
cin >> handicap;
cin.get();
*this = Golf(fullname, handicap);
return 1;
}
}
void Golf::handicap(int handicap)
{
m_handicap = handicap;
}
void Golf::showgolf() const
{
using namespace std;
cout << "姓名:\t" << m_fullname << endl;
cout << "等级\t" << m_handicap << endl << endl;
}
//usegolf.cpp
#include
#include
#include "golf.h"
int main()
{
using namespace std;
const int size = 5;
Golf array[size];
int num = 0;
int setgolf_return;
for (int i = 0; i < size; i++)
{
setgolf_return = array[i].setgolf();
if(setgolf_return == 0)
break;
else
num += setgolf_return; //或 num++;
}
for (int i = 0; i < num; i++)
array[i].showgolf();
cout << "修改第1个的handicap后:\n";
array[0].handicap(2);
for (int i = 0; i < num; i++)
array[i].showgolf();
return 0;
}
4. 完成第九章编程练习4,但将Sales结构及相关的函数转换为一个类及其方法。
用构造函数替换setSales(sales&, double[], int)函数。
用构造函数实现setSales(Sales&)方法的交互版本。将类保留在名称空间SALES中。
第9章的编程练习4,链接: https://blog.csdn.net/weixin_42411044/article/details/86128627
//sales.h
#ifndef sales_H_
#define sales_H_
namespace SALES
{
const int QUARTERS = 4;
const double init_ar[4] = {0.0, 0.0, 0.0, 0.0};
class Sales
{
private:
double m_sales[QUARTERS];
double m_average;
double m_max;
double m_min;
public:
Sales(const double ar[] = init_ar, int n = 4);
void setSales();
void showSales() const;
};
}
#endif
//sales.cpp
#include
#include "sales.h"
namespace SALES
{
Sales::Sales(const double ar[], int n)
{
for (int i = 0; i < n; i++)
m_sales[i] = ar[i];
if (n < QUARTERS)
for (int i = n; i < QUARTERS; i++)
m_sales[i] = 0;
double sum = 0;
double max = m_sales[0];
double min = m_sales[0];
for (int i =0; i < QUARTERS; i++)
{
sum += m_sales[i];
if (max < m_sales[i])
max = m_sales[i];
if (min > m_sales[i])
min = m_sales[i];
}
m_average = sum/QUARTERS;
m_max = max;
m_min = min;
}
void Sales::setSales()
{
int num;
double input[QUARTERS];
for (int i = 0; i < QUARTERS; i++)
{
using namespace std;
cout << "请输入第" << i+1 << "季度的销售额:(输入非数字提前结束)";
if(cin >> input[i])
{
cin.get();
continue;
}
num = i;
break;
}
*this = Sales(input, num);
}
void Sales::showSales() const
{
using namespace std;
cout << "四个季度的销售额为:\n";
for (int i =0; i < QUARTERS; i++)
cout << "第" << i+1 << "季度的销售额:\t" << m_sales[i] << endl;
cout << endl;
cout << "平均值为:\t" << m_average << endl;
cout << "最大值为:\t" << m_max << endl;
cout << "最小值为:\t" << m_min << endl;
cout << endl << endl;
}
}
//usesales.cpp
#include
#include "sales.h"
using namespace SALES;
int main()
{
double ar1[4] = {1,2,3,4};
double ar2[3] = {1,2,3};
Sales object1; //隐式调用默认构造函数
Sales object2 = Sales(ar1, 4); //显式调用构造函数
Sales object3(ar2, 3); //隐式调用构造函数
object1.showSales();
object2.showSales();
object3.showSales();
object1.setSales();
object1.showSales();
return 0;
}
5. 考虑下面的结构声明:
struct customer{
char fullname[35];
double payment;
};
编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被删除时,其payment的值都将被加入到总数中,并报告总数。
注意:应该可以直接使用Stack类而不作修改;只需修改typedef声明,使Item的类型为customer,而不是unsigned long即可。
// stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer{
char fullname[35];
double payment;
};
typedef customer Item;
class Stack
{
private:
enum {MAX = 10}; // constant specific to class
Item items[MAX]; // holds stack items
int top; // index for top stack item
public:
Stack();
bool isempty() const;
bool isfull() const;
// push() returns false if stack already is full, true otherwise
bool push(const Item & item); // add item to stack
// pop() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop top into item
};
#endif
// stack.cpp
#include "stack.h"
Stack::Stack() // create an empty 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;
}
// stacker.cpp
#include
#include // or ctype.h
#include "stack.h"
int main()
{
using namespace std;
Stack st; // create an empty stack
char ch;
customer po;
double sum = 0;
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
while (cin >> ch && toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << '\a';
continue;
}
switch(ch)
{
case 'A':
case 'a': cout << "Enter a PO to add: \n";
cout << "Enter the fullname of PO: ";
cin.getline(po.fullname, 35);
cout << "Enter the payment of PO: ";
cin >> po.payment;
cin.get();
if (st.isfull())
cout << "stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p': if (st.isempty())
cout << "stack already empty\n";
else {
st.pop(po);
cout << "PO #" << po.fullname << " popped\n";
sum += po.payment;
cout << "Now, Sum is " << sum << endl;
}
break;
}
cout << "Please enter A to add a purchase order,\n"
<< "P to process a PO, or Q to quit.\n";
}
cout << "Bye\n";
return 0;
}
6. 下面是一个类声明:
class Move
{
private:
double x;
double y;
public:
Move(double a = 0, double b = 0); // sets x, y to a, b
showmove() const; // shows current x,y values
Move add(const Move & m) const;
// this function adds x of m to x of invoking object to get new x,
// adds y of m to y of invoking object to get new y, creates a new
// move object initialized to new x, y values and returns it
//此函数将m的x加到调用对象的x以获取新的x,将m的y添加到调用对象的y中以获得新的y,
//创建一个新的对象,并将对象初始化为新的x,y值并返回它
reset(double a = 0, double b = 0); // resets x,y to a, b
};
请提供成员函数的定义和测试这个类的程序。
//move.h
#ifndef move_H_
#define 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);
};
#endif
//move.cpp
#include
#include "move.h"
using namespace std;
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove() const
{
cout << "x = " << x << "\t" << "y = " << y << endl;
}
Move Move::add(const Move & m) const
{
Move newone;
newone.x = x + m.x;
newone.y = y + m.y;
return newone;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
//usemove.cpp
#include
#include "move.h"
using namespace std;
int main()
{
Move move1; //使用隐式使用默认构造函数 显式: Move move1 = Move();
move1.showmove();
move1.reset(1, 2);
move1.showmove();
Move move2(2, 3); //使用隐式构造函数 显式: Move move2 = Move(2, 3);
move2.showmove();
Move move3 = move1.add(move2);
move3.showmove();
return 0;
}
7. Betelgeusean plorg有这些特征。
数据:
·plorg的名称不超过19个字符
·plorg的满意指数(CI),这是一个整数
操作:
·新的plorg将有名称,其CI值为50
·plorg的CI可以修改
·plorg可以报告其名称和CI
请编写一个Plorg类声明(包括数据成员和成员函数原型)来表示plorg,并编写成员函数的函数定义。
然后编写一个小程序,以演示Plorg类的所有特性。
//plorg.h
#ifndef plorg_H_
#define plorg_H_
class Plorg
{
private:
static const int size = 20;
char m_name[size];
int m_ci;
public:
Plorg(); //默认构造函数
Plorg(const char name[], int ci); //构造函数
void setCI(int ci);
void show();
};
#endif
//plorg.cpp
#include
#include
#include "plorg.h"
using namespace std;
Plorg::Plorg()
{
strcpy(m_name, "None");
m_ci = 50;
}
Plorg::Plorg(const char name[], int ci)
{
strcpy(m_name, name);
m_ci = ci;
}
void Plorg::setCI(int ci)
{
m_ci = ci;
}
void Plorg::show()
{
cout << "Name: " << m_name << "\tCI: " << m_ci << endl;
}
//useplorg.cpp
#include
#include
#include "plorg.h"
using namespace std;
int main()
{
Plorg plorg1; //隐式使用默认构造函数 显式:Plorg plorg1 = Plorg();
plorg1.show();
plorg1 = Plorg("name1", 300);
plorg1.show();
Plorg plorg2("name2", 100); //隐式使用构造函数 显式: Plorg plorg2 = Plorg("namename", 100);
plorg2.show();
plorg1.setCI(200);
plorg1.show();
plorg2.setCI(250);
plorg2.show();
return 0;
}
8. 可以将简单列表描述成下面这样
·可存储0或多个某种类型的列表
·可创建空列表
·可在列表中添加数据项
·可确定列表是否为空
·可确定列表是否为满
·可访问列表中的每一个数据项,并对它执行某种操作
可以看到,这个列表确实很简单,例如,它不允许插入或删除数据项
请设计一个List类来表示这种抽象类型。您应提供头文件list.h和实现文件list.cpp,前者包含类定义,后者包含类方法的实现。您还应该创建一个简短的程序来使用这个类
该列表的规范很简单,这个主要旨在简化这个编程练习。可以选择使用数组或链表来实现该列表,但公有接口不应依赖于所做的选择。也就是说,公有接口不应有数组索引、节点指针等。
应使用通用概念来表达创建列表、在列表中添加数据项等操作。对于访问数据项以及执行操作,通常应使用将函数指针作为参数的函数来处理:void visit(void (*pf)(Item &));
其中。pf指向一个将Item引用作为参数的函数(而不是成员函数),Item是列表中数据项的类型。visit()函数将该函数用于列表中的每个数据项。
//list.h
#ifndef list_H_
#define list_H_
typedef unsigned long Item;
class List
{
private:
enum {MAX = 10}; // constant specific to class
Item items[MAX]; // holds list items
int top; // index for top list item
public:
List();
bool isempty() const;
bool isfull() const;
bool push(const Item & item); // add item to list
void visit(void (*pf)(Item &));
};
#endif
// list.cpp
#include "list.h"
List::List() // create an empty list
{
top = 0;
}
bool List::isempty() const
{
return top == 0;
}
bool List::isfull() const
{
return top == MAX;
}
bool List::push(const 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]);
}
//uselist.cpp
#include
#include "list.h"
using namespace std;
void function(Item &);
int main()
{
List list; //隐式使用默认构造函数
cout << "当前list的状态:\n";
if (list.isempty())
cout << "空\n";
else if (list.isfull())
cout << "满\n";
else
cout << "非空非满\n";
list.push(1);
list.push(2);
list.push(3);
cout << "当前list的状态:\n";
if (list.isempty())
cout << "空\n";
else if (list.isfull())
cout << "满\n";
else
cout << "非空非满\n";
cout << "当前列表内容为:\n";
list.visit(function);
list.push(4);
list.push(5);
list.push(6);
list.push(7);
list.push(8);
list.push(9);
list.push(10);
cout << "当前list的状态:\n";
if (list.isempty())
cout << "空\n";
else if (list.isfull())
cout << "满\n";
else
cout << "非空非满\n";
cout << "当前列表内容为:\n";
list.visit(function);
}
void function(Item & item)
{
cout << item << endl;
}