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 第五章:语句 ...
随机推荐
- Unix/Linux环境C编程入门教程(8) FreeBSD CCPP开发环境搭建
1. FreeBSD是一种自由类Unix操作系统,是由经过BSD.386BSD和4.4BSD发展而来的类Unix的一个重要分支.FreeBSD拥有超过200名活跃开发者和上千名贡献者.FreeBSD被 ...
- Unix/Linux环境C编程入门教程(4) Debian Linux环境搭建
Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 1.广义的Debian是指一个致力于创建自由操作系统的合作组织及其作品,由于Deb ...
- window.open打开新页面,并将本页数据用过url传递到打开的页面;需要两个页面;
页面1 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 加入收藏夹的js代码(求兼容chrome浏览器的代码)
从网上找了加入收藏夹的js代码,但不兼容chrome,不知道有没有兼容chrome的相关代码,希望有知道的告诉一下,谢谢! 代码如下 $("#id").click(function ...
- NYOJ 7-街区最短路径问题(曼哈顿距离)
街区最短路径问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 一个街区有很多住户,街区的街道只能为东西.南北两种方向. 住户只可以沿着街道行走. 各个街道之间的间 ...
- Android版xx助手之天天酷跑外挂具体分析
Android版xx助手之天天酷跑外挂具体分析 图/文 莫灰灰 背景 近些年来,移动互联网的大肆崛起,潜移默化中影响着人们的生活和工作习惯.当腾讯的微信平台接入手机游戏之后,移动端的游戏也開 ...
- 利用jquery写的一个TAB页切换效果
函数如下 /** *切换效果 */ function switab(tab,con,tab_c_css,tab_n_css,no) { $(tab).each(function(i){ if(i == ...
- English - refer to...和refer to...as
refer to...和refer to...as...本来就是refer的两个固定搭配,这个只能讲讲后两者用法,剩下的就是单独的refer的用法了. 1. refer to sb/sth 指的是/提 ...
- 2014.9.3数据库CRUD
CRUD 增删改查 DCL 数据控制语言:备份,grant DML 数据操作语言: CRUD DDL 数据定义语言:create drop alter 自增长列不能赋值 增: Insert into ...
- VS2013 快捷键乱掉如何修改回来
比如 CTRL+E+C =注释 F6=重新生成解决方案 CTRL+D+Q=运行时快速监视 工具-->选项-->环境-->键盘-->应用以下其他键盘映射方案,下拉选择 Visua ...