题目
1.对于下面的类声明:
(……代码忽略……)
给这个类提供实现,并编写一个使用所有成员函数的小程序。
2.通过完成下面的工作来改进String类声明(即将String1.h升级为String2.h)。
a. 对+运算符进行重载,使之可将两个字符串合并成1个。
b. 提供一个Stringlow()成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。
c. 提供String()成员函数,将字符串中所有字母字符转换成大写。
d. 提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的次数。
使用下面的程序来测试您的工作:
(……代码忽略……)
输出应与下面相似:
(……代码忽略……)
3.新编写程序清单10.7和程序清单10.8描述的Stock类,使之使用动态内存分配的内存,而不是string类对象来存储股票名称。另外,使用重载的operator<<()定义代替show()成员函数。再使用程序清单10.9测试新的定义程序。
4.请看下面程序清单10.10定义的Stack类的变量:
(……代码忽略……)
正如私有成员表明的,这个类使用动态内存分配的数组来保存栈项。请重新编写方法,以适应这种新的表示法,并编写一个程序来演示所有的方法,包括复制构造函数和赋值运算符。
5.Heather银行进行的研究表明,ATM客户不希望排队时间不超过1分钟。使用程序清单12.10中的模拟,找出要使平均等候时间为1分钟,每小时到达的客户数应为多少(试验时间不短于100小时)?
6.Heather银行想知道,如果再开设一台ATM,情况将如何。请对模拟进行修改,以包含两个队列。假设当第一台ATM前的排队人数少于第二台ATM时,客户将排在第一队,否则将排在第二队。然后再找出要使平均等候时间为1分钟,每小时到达的客户数应该为多少(注意,这是一个非线性问题,即将ATM数量加倍,并不能保证每小时处理的客户数量也翻倍,并确保客户等候的时间少于1分钟)?
程序
//cow.h
#ifndef COW_H_
#define COW_H_
class Cow
{
private:
char name[20];
char *hobby;
double weight;
public:
Cow();
Cow(const char *nm,const char *ho,double wt);
Cow(const Cow &c);
~Cow();
Cow &operator=(const Cow &c);
void ShowCow() const;
};
#endif
//cow.cpp
#include <iostream>
#include <cstring>
#include "cow.h"
using namespace std;
Cow::Cow()
{
name[0]='\0';
hobby=new char[1];
hobby[0]='\0';
weight=0.0;
}
Cow::Cow(const char *nm,const char *ho,double wt)
{
strcpy(name,nm);
hobby=new char[strlen(ho)+1];
strcpy(hobby,ho);
weight=wt;
}
Cow::Cow(const Cow &c)
{
strcpy(name,c.name);
hobby=new char[strlen(c.hobby)+1];
strcpy(hobby,c.hobby);
weight=c.weight;
}
Cow::~Cow()
{
delete [] hobby;
}
Cow &Cow::operator=(const Cow &c)
{
delete [] hobby;
strcpy(name,c.name);
hobby=new char[strlen(c.hobby)+1];
strcpy(hobby,c.hobby);
weight=c.weight;
return *this;
}
void Cow::ShowCow() const
{
cout<<"Name: "<<name<<endl;
cout<<"Hobby: "<<hobby<<endl;
cout<<"Weight: "<<weight<<endl;
cout<<endl;
}
//main.cpp
#include <iostream>
#include "cow.h"
using namespace std;
int main()
{
Cow c1;
c1.ShowCow();
Cow c2("Milkjh","eat grass",334.23);
c2.ShowCow();
Cow c3("Yellow big","ball",354.56);
c3.ShowCow();
Cow c4(c2);
c4.ShowCow();
c1=c3;
c1.ShowCow();
c3.ShowCow();
cout<<"Done!\n";
return 0;
}
// string2.h -- fixed and augmented string class definition
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80; // cin input limit
public:
// constructors and other methods
String(const char * s); // constructor
String(); // default constructor
String(const String &); // copy constructor
~String(); // destructor
int length () const {
return len; }
//+
void stringlow();
void stringup();
int has(char ch);
// overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
//+
String operator+(const String &)const;
String operator+(const char *)const;
friend String operator+(const char *,const String &);
// overloaded operator friends
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator>>(istream & is, String & st);
// static function
static int HowMany();
};
#endif
// string2.cpp -- String class methods
#include <cstring> // string.h for some
#include "string2.h" // includes <iostream>
#include <cctype>
using std::cin;
using std::cout;
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;
}
// class methods
String::String(const char * s) // construct String from C string
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
std::strcpy(str, s); // initialize pointer
num_strings++; // set object count
}
String::String() // default constructor
{
len = 4;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
String::String(const String & st)
{
num_strings++; // handle static member update
len = st.len; // same length
str = new char [len + 1]; // allot space
std::strcpy(str, st.str); // copy string to new location
}
String::~String() // necessary destructor
{
--num_strings;