1. 程式人生 > >C++ Primer Plus(6th edition) 第12章程式設計練習題

C++ Primer Plus(6th edition) 第12章程式設計練習題

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);

完整的程式設計練習程式碼可在如下連結下載: