C++Primer Plus(第六版) 第十二章 第二题
2017-07-30 14:17
169 查看
本题主要是重载+运算符,使用友元函数,直接返回String。代码如下:
string2.h:
// string2.h
// #pragma once
#ifndef STRING2_H_
#define STRING2_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80; // cin input limit
public:
// constructors and other methods
String(const char * s); // constructor
String(); // default constructor
String(const String &); // copy constructor
~String(); //destructor
int length() const { return len; }
int has(const char ch) const;
void stringlow() const;
void stringup() const;
// overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
// overloaded operator friends
friend String 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 bool operator==(const String &st1, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator >> (istream &is, String & st);
// static function
static int HowMany();
};
#endif // !STRING2_H_
string2.cpp:
// string2.cpp -- String class methods
#include <cstring> // string.h for some
#include <cctype>
#include "string2.h" // include <iostream>
using std::cin;
using std::cout;
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;
}
// class methods
String::String(const char * s) // construct String from C string
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
std::strcpy(str, s); // initialize pointer
num_strings++; // set object count
}
String::String() // default constructor
{
len = 0;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
String::String(const String & st)
{
num_strings++; // handle static member update
len = st.len; // same length
str = new char[len + 1]; // allot space
std::strcpy(str, st.str); // copy string to new location
}
String::~String()
{
--num_strings;
delete[] str;
}
int String::has(const char ch) const
{
int n = 0;
for (int i = 0; i < len; i++)
{
if (ch == str[i])
{
n++;
}
}
return n;
}
void String::stringlow() const
{
for (int i = 0; i < len; i++)
{
str[i] = tolower(str[i]);
}
}
void String::stringup() const
{
for (int i = 0; i < len; i++)
{
str[i] = toupper(str[i]);
}
}
// overloaded operator methods
// assign a String to a String
String & String::operator=(const String & st)
{
if (this == &st)
{
return *this;
}
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
delete[] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
// read-write char access for non-const String
char & String::operator[](int i)
{
return str[i];
}
// read-only char access for const String
const char & String::operator[](int i) const
{
return str[i];
}
// overloaded operator friends
String operator+(const String & st1, const String & st2)
{
char * tmpch = new char[st1.len + st2.len + 1];
strcpy(tmpch, st1.str);
strcat(tmpch, st2.str);
String tmp(tmpch);
delete[] tmpch;
return (tmp);
}
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) > 0);
}
bool operator==(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
// simple String output
ostream & operator<<(ostream &os, const String & st)
{
os << st.str;
return os;
}
// quick and dirty String input
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 <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; // overloaded << operator
cin >> s3; // overloaded >> operator
s2 = "My name is " + s3; // overloaded =, + 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]) // overloaded == operator
{
cout << "Thar's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
cin.get();
return 0;
}
string2.h:
// string2.h
// #pragma once
#ifndef STRING2_H_
#define STRING2_H_
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80; // cin input limit
public:
// constructors and other methods
String(const char * s); // constructor
String(); // default constructor
String(const String &); // copy constructor
~String(); //destructor
int length() const { return len; }
int has(const char ch) const;
void stringlow() const;
void stringup() const;
// overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
// overloaded operator friends
friend String 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 bool operator==(const String &st1, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator >> (istream &is, String & st);
// static function
static int HowMany();
};
#endif // !STRING2_H_
string2.cpp:
// string2.cpp -- String class methods
#include <cstring> // string.h for some
#include <cctype>
#include "string2.h" // include <iostream>
using std::cin;
using std::cout;
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;
}
// class methods
String::String(const char * s) // construct String from C string
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
std::strcpy(str, s); // initialize pointer
num_strings++; // set object count
}
String::String() // default constructor
{
len = 0;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
String::String(const String & st)
{
num_strings++; // handle static member update
len = st.len; // same length
str = new char[len + 1]; // allot space
std::strcpy(str, st.str); // copy string to new location
}
String::~String()
{
--num_strings;
delete[] str;
}
int String::has(const char ch) const
{
int n = 0;
for (int i = 0; i < len; i++)
{
if (ch == str[i])
{
n++;
}
}
return n;
}
void String::stringlow() const
{
for (int i = 0; i < len; i++)
{
str[i] = tolower(str[i]);
}
}
void String::stringup() const
{
for (int i = 0; i < len; i++)
{
str[i] = toupper(str[i]);
}
}
// overloaded operator methods
// assign a String to a String
String & String::operator=(const String & st)
{
if (this == &st)
{
return *this;
}
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
delete[] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
// read-write char access for non-const String
char & String::operator[](int i)
{
return str[i];
}
// read-only char access for const String
const char & String::operator[](int i) const
{
return str[i];
}
// overloaded operator friends
String operator+(const String & st1, const String & st2)
{
char * tmpch = new char[st1.len + st2.len + 1];
strcpy(tmpch, st1.str);
strcat(tmpch, st2.str);
String tmp(tmpch);
delete[] tmpch;
return (tmp);
}
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) > 0);
}
bool operator==(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
// simple String output
ostream & operator<<(ostream &os, const String & st)
{
os << st.str;
return os;
}
// quick and dirty String input
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 <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; // overloaded << operator
cin >> s3; // overloaded >> operator
s2 = "My name is " + s3; // overloaded =, + 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]) // overloaded == operator
{
cout << "Thar's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
cin.get();
return 0;
}
相关文章推荐
- 《C++ Primer Plus(第六版)》(24)(第十二章 类和动态内存分配 编程题和答案)
- C++Primer Plus(第六版) 第八章 第二题
- C++Primer Plus(第六版) 第十三章 第二题
- c++ Primer Plus(第六版)第十二章习题,写代码之路
- 【学习C++】C++ Primer Plus (第六版)第十二章编程练习1-6
- C++Primer Plus(第六版) 第十二章 第一题
- C++ Primer Plus第六版 第十二章 编程练习答案
- 《C++ Primer Plus(第六版)》(22)(第十二章 类和动态内存分配 笔记)
- 《C++ Primer Plus(第六版)》(23)(第十二章 类和动态内存分配 复习题和答案)
- C++primer plus第六版课后编程题答案18.4
- C++primer plus第六版课后编程题答案7.5
- C++ PRIMER PLUS 第六版编程答案(二)
- 《C++ Primer Plus(第六版)》(20)(第十一章 使用类 编程题答案1)
- C++primer plus第六版课后编程题答案 7.8a
- C++primer plus第六版课后编程题答案9.1
- C++ Primer Plus 第六版 所有章节课后编程练习答案
- C++primer plus第六版课后编程题答案9.3
- C++primer plus第六版课后编程题答案10.7
- C++ primer plus 第六版 课后答案 8.6
- C++Primer Plus(第六版) 第十三章 第四题