
p425.1
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std; class Cow{
char name[];
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;
}; Cow::Cow(){
strcpy(name, "no body");
hobby = "nothing";
weight = 0.0;
} Cow::Cow(const char *nm, const char *ho, double wt){
std::strncpy(name, nm, );
hobby = new char[strlen(ho) + ];
strcpy(hobby, ho);
weight = wt;
} Cow::Cow(const Cow &c){
hobby = new char[strlen(c.hobby) + ];
strcpy(hobby, c.hobby);
strcpy(name, c.name);
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) + ];
strcpy(hobby, c.hobby);
strcpy(name, c.name);
weight = c.weight;
cout << "hobby " << hobby << endl
<< "name " << name << endl
<< "weight " << weight << endl;
return *this;
} void Cow::ShowCow()const{
std::cout << "name is " << name << std::endl
<< "hobby is " << hobby << std::endl
<< "weight is " << weight << std::endl;
} int main(){
Cow test1, test2("Max", "soccer", 6.7);
test1.ShowCow();
test2.ShowCow();
Cow test3("Stack", "vollyball", 3.45);
cin.get();
test1 = test3;
test1.ShowCow(); system("pause");
return ;
}
p426.3
//头文件:
#include<iostream>
#include<string>
using std::istream;
using std::ostream; #ifndef STRING2_H_
#define STRING2_H_
class String{
private:
char *str;
int len;
static int num_strings;
static const int CINIM = ;
public:
String(const char *s);
String();
String(const String &);
~String();
int length()const { return len; }
String & operator=(const String &);
String &operator=(const char*);
char&operator[](int i);
const char &operator[](int i)const;
String & Stringlow();
char * Stringup();
int has(char);
friend char * 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 ostream &operator<<(ostream &os, const String &st);
friend istream &operator>>(istream &is, String &st);
static int howmany();
}; #endif //方法:
#include<iostream>
#include<cctype>
#include<cstring>
#include<string>
#include"String2.h" using std::cin;
using std::cout;
using std::endl; int String::num_strings = ; String::String(const char *s){
num_strings++;
len = strlen(s);
str = new char[len+];
strcpy(str, s);
cout << "num_strings " << num_strings << endl;
} String::String(){
num_strings++;
len = ;
str = NULL;
cout << "num_strings " << num_strings << endl;
} String::String(const String &st){
num_strings++;
len = st.len;
str = new char[len + ];
strcpy(str, st.str);
cout << "num_strings " << num_strings << endl;
} String::~String(){
num_strings--;
delete[]str;
cout << "num_strings " << num_strings << endl;
} String & String::operator=(const String &st){
if (&st == this)
return *this;
delete[]str;
len = st.len;
str = new char[len + ];
strcpy(str, st.str);
return *this;
} String & String::operator=(const char*s){
delete[]str;
len = strlen(s);
str = new char[len + ];
strcpy(str, s);
return *this;
} char & String::operator[](int i){
return str[i];
} const char & String::operator[](int i)const{
return str[i];
} String & String::Stringlow(){
for (int i = ; i < len; i++)
str[i] = tolower(str[i]);
return *this;
} char * String::Stringup(){
for (int i = ; i < len; i++)
str[i] = toupper(str[i]);
return str;
} int String::has(char ch){
int count = ;
for (int i = ; i < len; i++)
if (str[i] == ch)
count++;
return count;
} char * operator+(const String &st1, const String &st2){
char *st3 = new char[st1.len + st2.len+];
for (int i = ; i < st1.len; i++)
st3[i] = st1[i];
for (int j = ; j < st2.len; j++)
st3[st1.len ++ j] = st2[j];
st3[st1.len] = ' ';
st3[st1.len + st2.len + ] = '\0';
return st3;
} bool operator<(const String &st1, const String &st2){
if (strcmp(st1.str, st2.str))
return false;
else return true;
} bool operator==(const String &st1, const String &st2){
if (strcmp(st1.str, st2.str) == )
return true;
else return false;
} ostream &operator<<(ostream &os, const String &st){
os << "str: " << st.str << endl;
return os;
} istream &operator>>(istream &is, String &st){
char temp[String::CINIM];
is.get(temp, String::CINIM);
if (is)
st = temp;
while (is&&is.get() != '\n')
continue;
return is;
} int String::howmany(){
return num_strings;
} //驱动:
#include<iostream>
#include<cstdlib>
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;
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[] = { 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 = ; i < ; 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 ;
}