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. Netfilter-packet-flow.svg

    调试网络的方法:(Debugging the kernel using Ftrace)  $ watch -n1 -d sudo cat /proc/net/snmp$ watch -n1 -d su ...

  2. 面向对象程序设计-C++_课时16子类父类关系

    初始化列表 类名::类名(形参1,形参2,...形参n):数据成员1(形参1),数据成员2(形参2),...,数据成员n(形参n) { ... } 规则1,初始化列表进行数据成员的初始化 规则2,初始 ...

  3. Ubuntu启动、停止、重新启动MySQL,查看MySQL错误日志、中文编码错误

    1)启动: sudo /etc/init.d/mysql start 2)停止: sudo /etc/init.d/mysql stop 3)重新启动: sudo /etc/init.d/mysql ...

  4. c++打印环境变量

    直接上代码:cpp版本 #include <stdio.h> #include <stdlib.h> #include <string.h> extern char ...

  5. codefirst初尝试

    Code First 约定 借助 CodeFirst,可通过使用 C# 或Visual Basic .NET 类来描述模型.模型的基本形状可通过约定来检测.约定是规则集,用于在使用 Code Firs ...

  6. jquery新增,删除 ,修改,清空select中的option

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

  7. C#委托的简单剖析

    为什么在Button1的Click事件发生之后,button1_Click方法就会被调用呢? 实际上,在我们双击Button1的时候,IDE自动的添加了一段代码,该段代码位于“Form1.Design ...

  8. 全局通知Notification

    Notification 全局通知 关于全局通知的个人理解: 即有一个发射消息的,在整个应用中任何对象都可以接受这个消息 但是无论是哪个对象接受消息,都要在这个对象结束时移除消息 简单的说 就是给对象 ...

  9. org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Internal Server Error 错误

    Solr报错: { "responseHeader": { "status": 500, "QTime": 11 }, "erro ...

  10. HDU2955-Robberies

    描述: The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usual ...