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 cow data
};给这个类提供实现,并编写一个使用所有成员函数的小程序。
//cow.h
#ifndef COW_H_
#define COW_H_
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
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"cow.h"
Cow::Cow()
{
name[0] = '\0';
hobby = new char[1];
hobby[0] = '\0';
weight = 0;
}
Cow::Cow(const char *nm, const char *ho, double wt)
{
strcpy_s(name, 20, nm);
hobby = new char[strlen(ho) + 1];
strcpy_s(hobby, strlen(ho) + 1, ho);
weight = wt;
}
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)
{
if (this == &c)
return *this;
delete[]hobby;
hobby = new char[strlen(c.hobby) + 1];
strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby);
strcpy_s(name, 20, c.name);
weight = c.weight;
return *this;
}
void Cow::ShowCow()const
{
cout << "Cow name: " << name << endl;
cout << "Cow hobby: " << hobby << endl;
cout << "Cow weight: " << weight << endl;
}
//main.cpp
#include"cow.h"
int main()
{
Cow co1;
Cow co2("cow1", "sport", 123);
Cow co3(co2);
co1 = co2;
co1.ShowCow();
co2.ShowCow();
co3.ShowCow();
system("pause");
return 0;
}
*题目核心:
(1)strcpy_s函数的使用:
该函数是VS2005之后的VS提供的,并非C标准函数
原型:strcpy_s( char *dst, size_t num, const char *src )
功能:同strcpy()函数功能相同,不同之处在于参数中多了个size_t类型的参数,该参数为字符串dst的长度,当存在缓存区溢出的问题时(即src的长度大于dst的长度),strcpy_s()会抛出异常;而strcpy()结果则未定,因为它错误地改变了程序中其他部分的内存的数据,可能不会抛出异常但导致程序数据错误,也可能由于非法内存访问抛出异常。
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;
}
(2) 重载赋值运算符:
Cow &Cow::operator=(const Cow &c)
{
if (this == &c) //如果对象赋值给自身
return *this;
delete[]hobby; //释放旧字符串
hobby = new char[strlen(c.hobby) + 1]; //为新字符串分配内存
strcpy_s(hobby, strlen(c.hobby) + 1, c.hobby); //复制字符串
strcpy_s(name, 20, c.name);
weight = c.weight;
return *this; //返回一个指向调用对象的引用
}
2.通过完成下面的工作来改进String类声明(即将String1.h升级为String2.h)。
a。对+运算符进行重载,使之可将两个字符串合并成1个。
b。提供一个Stringlow()成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。
c。提供String()成员函数,将字符串中所有字母字符转换成大写。
d。提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的字数。
使用下面的程序来测试您的工作:
//pe12_2.cpp
#include<iostream>
using namespace std;
#include"string2.h"
int main()
{
String s1(" and I am a C++ student.");
String s2 = "Please enter your name: ";
String s3;
cout << s2; //overload <<operator
cin >> s3; //overload >>operator
s2 = "My name is " + s3; //overload = , + operators
cout << s2 << ".\n";
s2 = s2 + s1;
s2.stringup(); //converts string to uppercase
cout << "The string\n" << s2 << "\ncontains " << s2.has('A') << " 'A' characters in it.\n";
s1 = "red"; //String (const char *),
//then String & operator= (const String&)
String rgb[3] = { String(s1), String("green"), String("blue")};
cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (cin >> ans)
{
ans.stringlow(); //converts string to lowercase
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i]) //overload == operator
{
cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
return ;
}
输出应与下面相似:
Please enter your name: Fretts Farbo
My name is Fretta Farbo.
The strign
MY NAME ISFRETTA FARBO AND I AM A C++ STUDENT.
contains 6 'A' characters in it.
Enter the name of a primary color for mixing light: yellow
Try again!
BLUE
Tha's right!
Bye
//String.h
#ifndef STRING_H_
#define STRING_H_
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string.h>
using namespace std;
class String
{
public:
String(const char *s);
String();
String(const String &);
~String();
int length()const { return len; }
String &operator=(const String &st); //重载赋值运算符
String &operator=(const char *);
void stringlow();
void stringup();
int has(const char ch);
friend String operator+(const char *s, const String &st);
friend bool operator<(const String &st1, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st1, const String &st2);
friend String operator+(const String &st1, const String &st2);
friend ostream &operator<<(ostream &os, const String &st);
friend istream &operator>>(istream &is, String &st);
static int HowMany();
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
};
#endif
//String.cpp
#include "String.h"
int String::num_strings = 0;
int String::HowMany()
{
return num_strings;
}
String::String(const char *s)
{
len = strlen(s);
str = new char[len + 1];
strcpy_s(str, len + 1, s);
num_strings++;
}
String::String()
{
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;
}
String::String(const String &st)
{
num_strings++;
len = st.len;
str = new char[len + 1];
strcpy_s(str, len + 1, st.str);
}
String::~String()
{
--num_strings;
delete[]str;
}
String &String::operator=(const String &st)
{
if (this == &st)
return *this;
delete[]str;
len = st.len;
str = new char[len + 1];
strcpy_s(str, len + 1, st.str);
return *this;
}
String &String::operator=(const char *s)
{
delete[]str;
len = strlen(s);
str = new char[len + 1];
strcpy_s(str, len + 1, s);
return *this;
}
void String::stringlow()
{
for (int i = 0; i < len; i++)
{
if (isupper(str[i]))
str[i] = tolower(str[i]);
}
}
void String::stringup()
{
for (int i = 0; i < len; i++)
{
if (islower(str[i]))
str[i] = toupper(str[i]);
}
}
int String::has(const char ch)
{
int counts = 0;
for (int i = 0; i < len; i++)
{
if (str[i] == ch)
counts++;
}
return counts;
}
bool operator<(const String &st1, const String &st2)
{
return (strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2 < st1;
}
bool operator==(const String &st1, const String &st2)
{
return (strcmp(st1.str, st2.str) == 0);
}
String operator+(const char *s, const String &st) //友元:常量字符串+对象
{
int lens = strlen(s) + st.len;
char *ps = new char[lens + 1];
strcpy_s(ps, lens + 1, s);
strcat_s(ps, lens + 1, st.str);
return String(ps);
}
String operator+(const String &st1, const String &st2) //友元:对象+对象
{
int lens = st1.len + st2.len;
char *ps = new char[lens + 1];
strcpy_s(ps, lens + 1, st1.str);
strcat_s(ps, lens + 1, st2.str);
return String(ps);
}
ostream &operator<<(ostream &os, const String &st)
{
os << st.str;
return os;
}
istream &operator>>(istream &is, String &st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
st = temp;
while (is && is.get() != '\n')
continue;
return is;
}
//main.cpp
#include"String.h"
int main()
{
String s1(" and I am a C++ student.");
String s2 = "Please enter your name: ";
String s3;
cout << s2; //重载<<运算符
cin >> s3; //重载>>运算符
s2 = "My name is " + s3; //重载+,=运算符
cout << s2 << ".\n";
s2 = s2 + s1;
s2.stringup(); //将字符串转化为大写
cout << "The string\n" << s2 << "\ncontains " << s2.has('A')
<< " 'A' characters in it.\n";
s1 = "red"; //调用String(const char *),
//然后调用 String & operator=(const String &)
String rgb[3] = { String(s1), String("green"), String("blue") };
cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (cin >> ans)
{
ans.stringlow(); //将字符串转换为小写
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i]) //重载==运算符
{
cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
system("pause");
return 0;
}
运行结果:
Please enter your name: Fretta Farbo
My name is Fretta Farbo.
The string
MY NAME IS FRETTA FARBO AND I AM A C++ STUDENT.
contains 6 'A' characters in it.
Enter the name of a primary color for mixing light: yellow
Try again!
BLUE
That's right!
Bye
*题目核心知识点:
(1) 重载赋值运算符
String &operator=(const String &st); //重载赋值运算符
String &operator=(const char *);
(2) 友元重载+运算符
friend String operator+(const char *s, const String &st);
friend String operator+(const String &st1, const String &st2);
完整的编程练习代码可在如下链接下载: