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. Http 请求头中的 Proxy-Connection

    平时用 Chrome 开发者工具抓包时,经常会见到 Proxy-Connection 这个请求头.之前一直没去了解什么情况下会产生它,也没去了解它有什么含义.最近看完<HTTP 权威指南> ...

  2. git创建标签

    创建标签 在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branc ...

  3. hdu--1800--字典树&&其他

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 根据题意可知:意思是有若干个飞行员,需要在扫帚上练习飞行,每个飞行员具有不同的等级,且等级高的飞 ...

  4. Handlebarsjs学习笔记

    handlebarsjs官网(http://handlebarsjs.com/) 1.引入模板      在html页面中添加 <script id="entry-template&q ...

  5. 网易云课堂_C语言程序设计进阶_第四周:ACL图形库

    创建ACLLib程序 #include"acllib.h" #include<stdio.h> int Setup1() { initWindow(, );//初始化窗 ...

  6. append与after

    apend与apendTo就如同after与insertAfter,表达意思相同,表达不同.A.after(B)=B.insertAfter(A).apend在元素里面添加,after在元素外面添加. ...

  7. 自己写的操作sql的公共类

    /* /' `\/ `. . .' : `. `. \\.' , `.` `. `. ,___/|\. `. : . \, .'./ ' '\ , ' .\ . \_.~ _; ; \/'. `\ . ...

  8. Android入门——UI(2)

    介绍SeekBar拖动条控件.ProgressBar进度条控件.DatePicker日历控件.TimePicker时间控件 <?xml version="1.0" encod ...

  9. VS2010 打包问题汇总

    问题1:打包时:应将msadox28.tlb排除 ,受到windows系统文件保护 解决方案:文件选项上,选择排除即可. 问题2:路径有文件,生成报错,提示找不到文件或文件被锁定. 解决方案:文件被锁 ...

  10. wp8.1开发系列之安装包URI方案

    应用文件使用"ms-appdata:///"开头的URI地址,安装包使用的是"ms-appx:///"开头的URI地址. 比如:读取安装包Image文件夹下的t ...