本章所有编程练习的工程源码可在此处下载(点击此链接下载),供大家参考交流!
1. 对于下面的类声明:
class Cow {
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; // display all new data
};
给这个类提供实现,并编写一个使用所有成员函数的小程序。
对于本题,首先题中已经给出了类声明,所以头文件不需要自己定义,可以直接使。
所以头文件cow.h代码如下:
// cow.h -- head file
#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
在这里,我们之所以在书中提供的类声明的前后加上#ifndef, #define, #endif主要是为了防止该.h文件被重复引用。详情请点击本链接查看。
声明了头文件之后,我们首先要考虑去定义头文件中的函数声明。
头文件的私有成员变量中有字符串数组和字符串指针,因此需要用到strcpy_s函数,所以头文件中需要包含<cstring>;在构造函数中,唯一需要注意的就是字符串指针变量需要使用new来动态分配内存,然后再使用strcpy_s函数进行赋值,而相应的析构函数一定要记得将构造函数中使用new动态分配的内存delete掉。
另外,在重载运算符=时,需要在一开始delete一下,以防止此时this指针的hobby变量储存的地址是被分配使用过的。
所以实现文件cow.cpp代码如下:
// cow.cpp -- containing the functions' definition
#include "stdafx.h"
#include <iostream>
#include <cstring>
#include "cow.h"
using std::cout;
using std::endl;
Cow::Cow()
{
strcpy_s(name, 20, "new cow");
hobby = new char[4];
strcpy_s(hobby, 4, "cow");
weight = 0.0;
}
Cow::Cow(const char * nm, const char * ho, double wt)
{
weight = wt;
hobby = new char[strlen(ho) + 1];
strcpy_s(hobby, strlen(ho)+1, ho);
strcpy_s(name, 20, nm);
}
Cow::Cow(const Cow & c)
{
strcpy_s(name, 20, c.name);
hobby = new char[strlen(c.hobby) + 1];
strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby);
weight = c.weight;
}
Cow::~Cow()
{
delete[] hobby;
}
Cow & Cow::operator=(const Cow & c)
{
delete[] hobby;
hobby = new char[strlen(c.hobby) + 1];
strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby);
weight = c.weight;
strcpy_s(name, 20, c.name);
return *this;
}
void Cow::ShowCow() const
{
cout << "Now a new cow!\n";
cout << "The name is " << name << endl;
cout << "The hobby is " << hobby << endl;
cout << "The weight is " << weight << endl;
}
接下来我们来考虑如何使用这些成员函数组成检验文件。
其实本题的文件的主要功能只有3个,各种方式的函数初始化,使用重载的运算符=,使用ShowCow函数进行显示。
检验文件checkcow.cpp代码如下:
// checkcow.cpp -- check all the functions
#include "stdafx.h"
#include <iostream>
#include "cow.h"
using namespace std;
int main()
{
cout << "Let's start to see our cows!\n";
Cow c1;
c1.ShowCow();
Cow c2("yellow", "eat grass", 123.456);
c2.ShowCow();
Cow c3("black", "drink water", 222.333);
c3.ShowCow();
c3 = c1;
Cow c4(c2);
c3.ShowCow();
c4.ShowCow();
system("pause");
return 0;
}
运行结果如下图所示:
2. 通过完成下面的工作来改进String类声明(即将String1.h升级为String2.h)。
a. 对+运算符进行重载,使之可将两个字符串合并成1个。
b. 提供一个Stringlow()成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。
c. 提供String()成员函数,将字符串中所有字母字符转换成大写。
d. 提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的次数。
使用下面的程序来测试您的工作:
(……代码忽略……)
输出应与下面相似:
(……代码忽略……)
本题是在String1.h的基础上进行升级,添加几项功能再测试。
首先,需要重载+运算符,将两个字符串合并成1个,这里看起来只需要添加一个成员函数,其实不然。由于检验文件中既有将string对象相加,又有将字符串指针相加,还存在将string对象和字符串指针相加的情况,所以我们需要对应的定义3个函数来分别重载这三种情况下的+运算符。
其次,提供两个函数将字符串中的所有字母字符转换为小写或大写,这个功能使用<cctype>中的函数将非常轻松。
最后,提供一个成员函数,接受一个char参数,返回该参数在字符串中出现的次数,该函数将需要遍历整个字符串进行计数。
对于头文件,我们只需要添加以上6个函数即可,所以头文件string2.h代码如下:
// string2.h -- fixed and augmented string class definition
#ifndef STRING2_H_
#define STRING2_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String &);
~String();
int length() const { return len; }
void stringlow();
void stringup();
int has(char x);
String & operator=(const String &);
String & operator=(const char *);
char & operator[] (int i);
const char & operator[] (int i) const;
friend bool operator<(const String &st1, 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 int HowMany();
String operator+(const String &) const;
String operator+(const char *) const;
friend String operator+(const char *, const String &);
};
对于实现文件,其他的功能可以沿用string1.cpp,新添加的几个函数需要自定义。
首先,对于重载+运算符,我们分为三个函数。第一个是两个string对象之间的加法,在这里呢,我们可以使用strcat_s函数,直接将两个string对象进行拼接,但是由于默认情况下string对象的最后一个字符都是'\0',所以处理起来比较麻烦,我选择依然使用strcpy_s函数,只是复制的地址不一样,比如说str1的长度为10,str2的长度为10,我们新定义一个str3,要想把str1的内容和str2的内容加起来放进str3中,就把str1的内容从str3的第一个字符处开始赋值,把str2的内容从str3的第11个字符处开始赋值,这样就可以了。对于字符串指针变量的情况,可以完全类似处理。对于字符串指针变量和string对象相加的情况,我们可以人为地进行强制类型转换,将字符串指针变量转换为string对象,再加,那么此时相加就会调用前面定义的两个string对象相加的函数,即可以完成任务。
其次,我们在实现文件中包含头文件<cstring>,然后定义stringow()函数和stringup()函数,分别来对字符串的字母字符进行小写或大写的转换。在转换的时候,我们需要首先对整个字符串进行遍历,然后使用tolower()函数和toupper()函数来进行大小写转换,注意这里this是指针,指向某一个变量时要使用->符号。
最后,我们需要定义has()函数。该函数遍历整个字符串,然后识别输入的特定char类型变量,然后计数。我们在for循环内部嵌套一个if条件判断即可。
实现文