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

c++ primer plus 习题答案(6)的更多相关文章

  1. c++ primer plus 习题答案(1)

    c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...

  2. c++ primer plus 习题答案(8)

    p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...

  3. c++ primer plus 习题答案(7)

    p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...

  4. c++ primer plus 习题答案(5)

    p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...

  5. c++ primer plus 习题答案(4)

    p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...

  6. c++ primer plus 习题答案(3)

    p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...

  7. c++ primer plus 习题答案(2)

    p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...

  8. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  9. 《C++Primer》第五版习题答案--第五章【学习笔记】

    <C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...

随机推荐

  1. Brew install for mac

    安装命令例如以下: curl -LsSf http://github.com/mxcl/homebrew/tarball/master | sudo tar xvz -C/usr/local --st ...

  2. linux经常使用(一)linux 安装配置 jdk之 找不到安装文件文件夹及source /etc/profile 报unexpected end of file 错误 解决

    linux 安装配置 jdk 应该算是一个非常主要的东西.可是我到如今才自己第一次 正式安装.果然出现了问题.. 问题就是 安装之后 找不到 安装路径 ,进而没法配置环境变量. 现象例如以下: 提示 ...

  3. js 使用for循环遍历数组

    今天写个无聊的东西!for循环的使用! 例如以下:定义a数组,b为伪数组! var a = [1,2,3,0,5,4]; var b = document.getElementsByTagName(' ...

  4. (转)130道ASP.NET面试题

    130道ASP.NET面试题 转自http://blog.csdn.net/kingmax54212008/article/details/2021204 1. 简述 private. protect ...

  5. HTML之学习笔记(十)表单元素

    html表单元素的基本格式为(必须包含在form标签中)

  6. Asp.net 获取图片列表并打包下载

    先引用:ICSharpCode.SharpZipLib.dll 后台代码: using System.IO; using ICSharpCode.SharpZipLib.Zip; using ICSh ...

  7. Windows下命令行下启动ORACLE服务

    检查监听器状态:C:\>lsnrctl statusLSNRCTL for 32-bit Windows: Version 9.2.0.1.0 - Production on 30-6月 -20 ...

  8. wing 5.0 注册机

    输入License id 进入下一页获得request key ,输入request key 后点击生成,即可生成激活码,亲测可用 下载链接 密码:adwj

  9. 解决 jQuery-datepicker无法弹出日期的问题

    1.确保 jquery-ui.css.jquery.min.js和jquery-ui.min.js 三个文件的引用 2.如果是下载网站上的html,需要删除时间<input>的hasDat ...

  10. static_cast,const_cast,dynamic_cast,reinterpret_cast

    除非必要,尽量不要对变量进行强制转换.这是因为强制转换是存在风险的,但实际上在某种情况下,转型是必需的. 旧式C转型方式为(type)expression,即由一对小括号加上一个对象名称组成,而这种语 ...