1.为复习题5描述的类提供方法定义,并编写一个小程序来演示所有特性。
复习题5:定义一个类来表示银行账户。数据成员包括储户姓名,账号和存款。成员函数执行如下操作。
创建一个对象并将其初始化;显示储户的姓名,账号,存款。;存入参数指定的存款;取出参数指定的款项;
//bank.h
#ifndef BANK_H_
#define BANK_H_
#include <string>
using namespace std;
class bank
{
private:
string name;
string account;
double money;
public:
bank(const string & name1, const string & account1, double money1 = 0.0);
void show(void) const;
void deposit(double money2);
void withdraw(double money2);
};
#endif
//bank.cpp
#include "bank.h"
#include <iostream>
using namespace std;
bank::bank(const string & name1, const string & account1, double money1 = 0.0)
{
name = name1;
account = account1;
money = money1;
}
void bank::show(void) const
{
cout << "name :" << name << " account :" << account << " money :" << money;
}
void bank::deposit(double money2)
{
if (money2 > 0)
{
money = money + money2;
cout << "yuan: " << money2 << "money2 added" << endl;
}
else
{
cout << "finish" << endl;
}
}
void bank::withdraw(double money2)
{
if (money2 > 0)
{
money = money - money2;
cout << "yuan :" << "money withdraw" << endl;
}
else
{
cout << "finish" << endl;
}
}
//bank_1.cpp
#include <iostream>
#include "bank.h"
#include <string>
using namespace std;
int main()
{
cout << "please enter your information" << endl;
string name;
string account;
double money;
cout << "please enter your name" << endl;
getline(cin, name);
cout << "please enter your accunt" << endl;
getline(cin, account);
cout << "please enter your money" << endl;
cin >> money;
bank qaccount(name, account, money);
char f;
double money2;
cout << "Please choose d is deposit, w is withdraw, q quit: ";
cin >> f;
while (f != 'q')
{
if (f == 'd')
{
cout << "Please enter amount: ";
cin >> money2;
qaccount.deposit(money2);
qaccount.show();
}
else if (f == 'w')
{
cout << "Please enter withdraw amount: ";
cin >> money2;
qaccount.withdraw(money2);
qaccount.show();
}
else
break;
cout << "Please choose d is deposit, w is withdraw, q quit: ";
cin >> f;
}
cout << "finish" << endl;
system("Pause");
return 0;
}
2.下面是一个简单的类定义:
/*****************************************/
它使用了一个string对象和一个字符数组,让您能够比较他们的用法。请提供未定义的方法的代码,以完成这个类的实现。在编写一个使用这个类的程序,它使用三种可能的构造函数(没有参数,一个参数,两个参数)以及两种显示方法。下面是使用这些构造函数的方法和例子:
#ifndef PERSON_H_
#define PERSON_H_
#pragma once
#include <string>
#include <iostream>
//person.h
using namespace std;
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;
};
#endif
//person.cpp
#include "person.h"
#include <cstring>
#include <iostream>
using namespace std;
person::person(const string &ln, const char *fn )
{
lname = ln;
strcpy_s(fname, fn);
}
void person::show()const
{
cout << "lname:" << lname << " fname: " << fname << endl;
}
void person::FormalShow()const
{
cout << "lname1: " << lname << "fname1: " << fname << endl;
}
//person_1.cpp
#include "person.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
person one;
person two("smythecraft");
person three("Dimwiddy", "sam");
one.show();
cout << endl;
one.FormalShow();
cout << endl;
two.show();
cout << endl;
two.FormalShow();
cout << endl;
three.show();
cout << endl;
three.FormalShow();
cout << endl;
system("pause");
return 0;
}
3.完成第九章的编程练习1,但要用正确的golf类声明替换那里的代码。用带合适参数的构造函数替代setgolf(golf &,const char*,int),已提供初始值。保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造函数来创建一个临时对象,并将其赋给调用对象,既*this)
//golf.h
#ifndef GOLF_H_
#define GOLF_H_
#pragma once
class golf
{
private:
char fullname[40];
int handicap;
public:
golf(const char*name, int h);
golf();
void sethandicap(int h);
void showgolf();
};
#endif
//glof1.cpp
#include "golf.h"
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
golf::golf(const char * name, int h)
{
strcpy_s(fullname, name);
handicap = h;
}
golf::golf()
{
char k[20];
int h;
cout << "please enter name";
cin.getline(k, 20);
cout << "please enter handicap";
cin >> h;
cin.ignore();
*this = golf(k, h);
}
void golf::sethandicap(int h)
{
handicap = h;
}
void golf::showgolf()
{
cout << "fullname: " << fullname << endl;
cout << "handicap: " << handicap << endl;
}
//golf_test.cpp
#include "golf.h"
#include <iostream>
using namespace std;
int main()
{
golf k;
k.showgolf();
k.sethandicap(80);
k.showgolf();
golf k1("liu", 30);
k1.showgolf();
k1.sethandicap(80);
k1.showgolf();
system("pause");
return 0;
}
4.完成第九章的编程练习四,但将Sales结构及相关的函数转化为一个类及其方法,用构造函数替代setSales(sales&,double【】,int)函数。用构造函数实现setSales(Sales&)方法的交互版本,将类保留在名称空间SALES中。
//namesp.h
#ifndef NAMESP_H_
#define NAMESP_H_
namespace SALES
{
class Sales
{
private:
double sales[4];
double average;
double max;
double min;
public:
void setSales (const double ar[],int n);
void setSales();
void showSales();
};
}
#endif
// fun.cpp
#include <iostream>
#include "namesp.h"
using namespace std;
namespace SALES
{
void Sales::setSales(const double ar[], int n)
{
if (n < 4)
{
for (int i = 0; i < n; i++)
{
sales[i] = ar[i];
}
for (int j = n; j < 4; j++)
{
sales[j] = 0;
}
}
else
{
for (int i = 0; i < 4; i++)
{
sales[i] = ar[i];
}
}
average = (sales[0] + sales[1] + sales[2] + sales[3]) / 4;
double imax = 0.0;
double imin = 100.0;
for (int i = 0; i < 4; i++)
{
if (sales[i] > imax)
{
imax = sales[i];
}
if (sales[i] < imin)
{
imin = sales[i];
}
max = imax;
min = imin;
}
}
void Sales::setSales()
{
cout << "Please enter 4 ci sales:\n";
for (int i = 0; i < 4; i++)
{
cout << i + 1 << " quarter is: ";
cin >> sales[i];
}
average = (sales[0] + sales[1] + sales[2] + sales[3]) / 4;
double imax = 0.0;
double imin = 100.0;
for (int i = 0; i < 4; i++)
{
if (sales[i] > imax)
{
imax = sales[i];
}
if (sales[i] < imin)
{
imin = sales[i];
}
}
max = imax;
min = imin;
}
void Sales::showSales()
{
cout << "xian shi quan bu " << endl;
cout << "4 ge fen bie wei " << sales[0] << ", " << sales[1] << ", " << sales[2] << ", " << sales[3] << endl;
cout << "The average is " << average << endl;
cout << "The max is " << max << endl;
cout << "The min is " << min << endl;
}
}
// sales.cpp
#include <iostream>
#include "namesp.h"
using namespace std;
using namespace SALES;
int main()
{
Sales s1;
Sales s2;
cout << "di yi ge information:" << endl;
s1.setSales();
s1.showSales();
cout << endl;
cout << "di er ge information:" << endl;
double ar[3] = {
10,20,30 };
s2.setSales(ar, 3);
s2.showSales();
cout << endl;
system("pause");
return 0;
}
5.考虑下面的结构声明:
//
//
编写一个程序,它从栈中添加和删除custimer结构(栈用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:
Item items[5];
int top;
public:
Stack();
bool istopmin()const;
bool istopmax()const;
bool add(const Item &item);
bool out(Item &item);
};
#endif
//stack.cpp
#include "stack.h"
Stack::Stack()
{
top = 0;
}
bool Stack::istopmin()const
{
return top == 0;
}
bool Stack::istopmax()const
{
return top == 5;
}
bool Stack::add(const Item &item)
{
if (top < 5)
{
items[top++] = item;
return true;
}
else
return false;
}
bool Stack::out(Item &item)
{
if (top >0)
{
top--;
item = items[top];
}
else
return false;
}
//stack_test.cpp
#include "stack.h"
#include <iostream>
using namespace std;
int main()
{
Stack s;
char c;
customer t;
double j = 0;
cout << "please enter A is add,enter q is quit" << endl;
while (cin>>c&&toupper(c)!='q')
{
while (cin.get() != '\n')
{
continue;
}
if (c != 'a'&&c != 'A')
{
cout << "please enter a" << endl;
continue;
}
if (c == 'A' || c == 'a')
{
cout << "please enter name: ";
cin.getline(t.fullname, 20);
cout << "please enter payment: ";
cin >> t.payment;
if (s.istopmax())
{
cout << "stack is full" << endl;
}
else
{
s.add(t);
}
}
if (c == 'q' || c == 'Q')
{
if (s.istopmin())
{
cout << "stack is kong" <<endl;
}
else
{
j = j + t.payment;
s.out(t);
cout << "name :" << t.fullname << " delate" << endl;
}
}
cout << "please enter A is add,enter q is quit" << endl;
}
cout << "quan bu payment is" << j << endl;
system("pause");
return 0;
}
6.下面是类的声明:
//
/*****/
请提供成员函数的定义和显示
//move.h
#ifndef PLORG_H_
#define PLORG_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 "move.h"
#include <iostream>
using namespace std;
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 i;
i.x = m.x + this->x;
i.y = m.x + this->y;
return i;
}
void Move::reset(double a , double b )
{
x = a;
y = b;
}
#include "move.h"
#include <iostream>
using namespace std;
int main()
{
Move m1{
10.1, 10.2 };
Move m2{
20.1, 20.2 };
cout << "the first move m1: " << endl;
m1.showmove();
cout << "the second move m2: " << endl;
m2.showmove();
cout << "please enter your move " << endl;
double i, j;
cout << "x: ";
cin >> i;
cout << "y: ";
cin >> j;
Move m3(i, j);
cout << "your move is :" << endl;
m3.showmove();
cout << "the first move add your move" << endl;
m3 = m3.add(m1);
cout << "the second move add your move" << endl;
m3 = m3.add(m2);
cout << "your move is :" << endl;
m3.showmove();
cout << "reset your move:" << endl;
cout << "x: ";
cin >> i;
cout << "y: ";
cin >> j;
system("pause");
return 0;
}
7.Betelgeusean plorg有这些特征。
数据:
plorg的名称不超过19个字符;
plorg有满意指数(CI),这是个整数。
操作:
新的plorg将有名称,其CI值为50;
plorg的CI可以修改;
plorg可以报告其名称和CI;
plorg的默认名称为“Plorga”。
请编写一个PLorg类声明(包括数据成员和成员函数原型)来表示plorg,并编写成员函数的函数定义。然后编写一个小 程序,以演示Plorg类的所有特性。
//Plorg.h
#ifndef PLORG_H_
#define PLORG_H_
class plorg
{
public:
plorg();
void show()const;
void newplorg(const char *name);
void CIplorg(int i);
private:
char plorgname[19];
int CI;
};
#endif
//Plorg.cpp
#include "Plorg.h"
#include <iostream>
using namespace std;
plorg::plorg()
{
strcpy_s(plorgname, "Plorg");
CI = 0;
}
void plorg::show()const
{
cout << "plorg name is :" << plorgname << " the CI is" << CI << endl;
}
void plorg::newplorg(const char *name)
{
strcpy_s(plorgname, name);
CI = 50;
}
void plorg::CIplorg(int i)
{
CI = i;
}
//Plorg_test.cpp
#include <iostream>
#include "Plorg.h"
using namespace std;
int main()
{
plorg k;
cout << "plorg is : " << endl;
k.show();
plorg i;
cout << "please enter new plorg name: " << endl;
char name[19];
cin.getline(name, 19);
i.newplorg(name);
i.show();
cout << "you can change CI,CI is 50" << endl;
int p;
cin >> p;
cin.ignore();
i.CIplorg(p);
i.show();
system("pause");
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 int Item;
class List
{
private:
enum {
MAX = 5 };
Item items[MAX];
int top = 0;
public:
List();
bool ismin()const;
bool ismax()const;
bool add(const Item&item);
void visit(void(*pf)(Item&item));
void show()const;
};
void p(Item&item);
#endif
// list.cpp
#include <iostream>
#include "list.h"
using namespace std;
List::List()
{
top = 0;
}
bool List::ismin() const
{
return top == 0;
}
bool List::ismax() const
{
return top == MAX;
}
bool List::add(const Item & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
void List::visit(void(*pf)(Item & item))
{
for (int i = 0; i < top; i++)
{
pf(items[i]);
}
}
void List::show() const
{
for (int i = 0; i < top; i++)
{
cout << "di " << i + 1 << ": " << items[i] << std::endl;
}
}
void p(Item & item)
{
item += 100;
}
//list_test.cpp
#include <iostream>
#include "list.h"
#include <cctype>
using namespace std;
int main()
{
cout << "Please enter C list, P process the list, S show the items, Q quit:" << endl;
char ch;
List l;
int data;
while (cin >> ch && toupper(ch) != 'Q')
{
while (cin.get() != '\n')
continue;
if (!isalpha(ch))
{
cout << "\a";
continue;
}
switch (ch)
{
case 'C':case 'c':
if (l.ismax())
cout << "The list already full."<<endl;
else
{
cout << "Enter the data: ";
cin >> data;
l.add(data);
}
break;
case 'P':case 'p':
if (l.ismin())
cout << "The list already empty."<<endl;
else
{
l.visit(p);
cout << "Every data has plused 100."<<endl;
}
break;
case 'S':case 's':
l.show();
break;
}
cout << "Please enter C list, P process the list, S show the items, Q quit:" << endl;
}
l.show();
system("pause");
return 0;
}
由于本人的实际实力尚处于初学者。
有些内容参考另一个博主:Leonardo Liu