c++ primer plus 习题答案(6)
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)的更多相关文章
- c++ primer plus 习题答案(1)
c++ primer plus 习题答案用的是第五版,IDE仍然是vs2013.我只标注了题号,具体的题目找下书上对应内容吧. p110.8 #include<iostream> #inc ...
- c++ primer plus 习题答案(8)
p475.2 //头文件: class Cd{ private: char *performers; char *label; int selections; double playtime; pub ...
- c++ primer plus 习题答案(7)
p427.4 //头文件: #include<iostream> #ifndef STACK_H_ #define STACK_H_ typedef unsigned long Item; ...
- c++ primer plus 习题答案(5)
p333.7 #include<iostream> #include<cstring> #include<cstdlib> using namespace std; ...
- c++ primer plus 习题答案(4)
p333.3 #include<iostream> #include<cstdlib> #include<cstring> #include<string&g ...
- c++ primer plus 习题答案(3)
p296.3 #include<iostream> #include<cstdlib> #include<string> #include<cstring&g ...
- c++ primer plus 习题答案(2)
p221.8 #include<iostream> #include<cstdlib> #include<cstring> using namespace std; ...
- C++Primer第五版——习题答案目录
目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...
- 《C++Primer》第五版习题答案--第五章【学习笔记】
<C++Primer>第五版习题答案--第五章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/15 第五章:语句 ...
随机推荐
- java中输出流OutputStream 类应用实例(转)
OutputStream类该类是字节输出流的抽象类,定义了输出流的各种操作方法.这些方法的说明如表1所示.下面通过实例介绍如何使用OutputStream类向控制台输出字符串信息.步骤如下.(1)创建 ...
- js原生forEach、map与jquery的each、$.each的区别
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- ACdream群赛1112(Alice and Bob)
题意:http://acdream.info/problem?pid=1112 Problem Description Here is Alice and Bob again ! Alice and ...
- 【leetcode系列】Valid Parentheses
非常经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也能够直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class Stack ...
- 自己写的操作sql的公共类
/* /' `\/ `. . .' : `. `. \\.' , `.` `. `. ,___/|\. `. : . \, .'./ ' '\ , ' .\ . \_.~ _; ; \/'. `\ . ...
- sql server group by having 之复习篇
where 与 having 之间的差别在于where 是分组前的过滤,而having是分组后的过滤 Group By中Select指定的字段限制 示例3 select 类别, sum(数量) as ...
- hive 学习笔记精简
创建表: drop table t create table if not exists t (t string) partitioned by (log_date string) row forma ...
- ASP.NET MVC View向Controller传值方式总结
1:QueryString传值1)也可以使用new{}来为form的action增加querystring2)在controler里使用Request.QueryString["word&q ...
- vue.js自定义指令入门
Vue.js 允许你注册自定义指令,实质上是让你教 Vue 一些新技巧:怎样将数据的变化映射到 DOM 的行为.你可以使用Vue.directive(id, definition)的方法传入指令id和 ...
- JSTL入门
在页面最上方引入 -------------------- if语句 8}"> b的值大于8 --------------------- foreach语句 i的值是:${i}