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;
};
给这个类提供实现,并编写一个使用所有成员函数的小程序。
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
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 // !CLASS_H_
//class.cpp
#include "class.h"
#include <iostream>
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, nm);
int len = strlen(ho)+1; //算上最后的 '\0'
hobby = new char[len];
strcpy_s(hobby, len, ho);
weight = wt;
}
Cow::Cow(const Cow& c)
{
strcpy_s(name, c.name);
int len = strlen(c.hobby)+1;
hobby = new char[len];
strcpy_s(hobby, len, c.hobby);
weight = c.weight;
}
Cow::~Cow()
{
delete[] hobby;
}
Cow& Cow::operator=(const Cow& c)
{
if (this == &c) //检查传入的c的地址与当前对象的地址是否相同
return *this;
delete[] hobby;
strcpy_s(name, c.name);
int len = strlen(c.hobby)+1;
hobby = new char[len];
strcpy_s(hobby, len, c.hobby);
weight = c.weight;
return *this;
}
void Cow::showCow() const
{
using std::cout;
using std::endl;
cout << "Name: " << name << endl;
cout << "Hobby: " << hobby << endl;
cout << "Weight: " << weight << endl;
}
//main.cpp
/* *************************************************
* 文件名:
* 创建人:px
* 创建时间:2020/4/23
* 描述:
************************************************* */
#include <iostream>
#include "class.h"
int main()
{
using namespace std;
Cow c1;
Cow c2;
Cow c3("C3", "aaa", 15);
Cow c4("C4", "bbb", 10);
c1 = c3;
c1.showCow();
c2.showCow();
c3.showCow();
c4.showCow();
return 0;
}
2.通过完成下面的工作来改进String类声明(即将String1.h升级为String2.h)。
a.对+运算符进行重载,使之可将两个字符串合并成1个。
b.提供一个Stringlow( )成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。
c.提供String( )成员函数,将字符串中所有字母字符转换成大写。
d.提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的次数。
使用下面的程序来测试您的工作:
#include <iostream>
#include "class.h"
using namespace std;
using namespace FableGame;
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 rgb[3] = {
String(s1), String ("grean"), 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";
return 0;
//clss.h
#ifndef CLASS_H_
#define CLASS_H_
#include <iostream>
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&st);
~String();
int length()const {
return len; }
String& operator=(const String&st);
String& operator=(const char*st);
char& operator[](int i);
const char& operator[](int i) const;
String operator+(const String& st); //a.对+运算符进行重载,使之可将两个字符串合并成1个。
String& stringlow(); //b.提供一个Stringlow( )成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。
String& stringup(); //c.提供String( )成员函数,将字符串中所有字母字符转换成大写。
int has(const char n) const; //d.提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的次数。
friend bool operator<(const String& st, const String& st2);
friend bool operator>(const String& st, const String& st2);
friend bool operator==(const String& st, const String& st2);
friend std::ostream& operator<<(std::ostream& os, const String& st);
friend std::istream& operator>>(std::istream& is, String& st);
friend String operator+(const char * ar,const String& st); //a.对+运算符进行重载,使之可将两个字符串合并成1个。
static int Howmany();
};
#endif // !CLASS_H_
//class.cpp