1. 对于下面的类声明:

class Cow {
private:
    char name[20];
    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;  
};

给这个类提供实现,并编写一个使用所有成员函数的小程序。

#pragma once
#ifndef COW_H_
#define COW_H_
class Cow {
private:char name[20];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;
};
#endif // !COW_H_
// cow.cpp
#define _CRT_SECURE_NO_WARNINGS#include <cstring>
#include <iostream>
#include "cow.h"
using std::strncpy;
using std::strcpy;// 默认构造函数
Cow::Cow() {hobby = nullptr;weight = 0.0;
}// 构造函数
Cow::Cow(const char* nm, const char* ho, double wt) {strncpy(name, nm, 20);hobby = new char[strlen(ho) + 1];strcpy(hobby, ho);weight = wt;
}// 复制构造函数
Cow::Cow(const Cow& c) {strncpy(name, c.name, 20);hobby = new char[strlen(c.hobby) + 1];strcpy(hobby, c.hobby);weight = c.weight;
}// 析构函数,delete[]
// 最后将hobby置为空指针很重要
Cow::~Cow() {delete[] hobby;hobby = nullptr;
}Cow& Cow::operator=(const Cow& c) {strncpy(name, c.name, 20);hobby = new char[strlen(c.hobby) + 1];strcpy(hobby, c.hobby);weight = c.weight;return *this;
}void Cow::ShowCow() const {std::cout << "name: " << name<< ", hobby: " << hobby << ", weight: " << weight;
}
// 12-1
#include <iostream>
#include "cow.h"int main() {Cow cow1;Cow cow2 = Cow("XiaoHua", "Run", 23.5);Cow cow3 = Cow("XiaoHong", "Eat", 45.7);Cow cow4 = cow2;cow1 = cow3;std::cout << "cow1: ";cow1.ShowCow();std::cout << "\ncow2: ";cow2.ShowCow();std::cout << "\ncow3: ";cow3.ShowCow();std::cout << "\ncow4: ";cow4.ShowCow();return 0;
}

2. 通过完成下面的工作来改进String类声明(即将String1.h升级String2.h)。

a. 对+运算符进行重载,使之可将两个字符串合并成1个。

b. 提供一个Stringlow()成员函数,将字符串中所有的字母字符转换为小写(别忘了cctype系列字符函数)。

c. 提供String()成员函数,将字符串中所有字母字符转换成大写。

d. 提供一个这样的成员函数,它接受一个char参数,返回该字符在字符串中出现的次数。

使用下面的程序测试:

// pe12-2
#include <iostream>
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[3] = { String(s1), String("green"), String("blue") };
    cout << "Enter the name of a primary color of mixing light: ";
    String ans;
    bool success = false;
    while (cin >> ans) {
        ans.Stringlow();
        for (int i = 0; i < 3; i++) {
            if (ans == rgb[i]) {
                cout << "That's right!\n";
                success = true;
                break;
            }
        }
        if (success)
            break;
        else
            cout << "Try again!\n";
    }
    cout << "Bye\n";
    return 0;
}

解答:

#pragma once
// string2.h
#ifndef STRING2_H_
#define STRING2_H_#include <iostream>
using std::ostream;
using std::istream;class String {
private:char* str;int len;static int num_strings;static const int CINLIM = 80;
public:String(const char* s);String();                    // 默认构造函数String(const String& s);     // 复制构造函数~String();int length() const { return len; }String& Stringlow();String& Stringup();int has(char c);       // 返回c在字符串中出现的次数// 重载运算符String& operator=(const String& s);String& operator=(const char* s);char& operator[](int i);const char& operator[](int i) const;// 友元函数friend bool operator<(const String& s1, const String& s2);friend bool operator>(const String& s1, const String& s2);friend bool operator==(const String& s1, const String& s2);friend ostream& operator<<(ostream& os, const String& s);friend istream& operator>>(istream& is, String& s);friend String operator+(const String& s1, const Str

C++ Primer Plus 第6版 第12章 编程练习答案相关推荐

  1. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  2. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  3. C Primer Plus第六版第四章编程题目与参考答案⭐

    1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印出来. #include <stdio.h>int main() {char firstname[20] ...

  4. c++ primer plus第六版第六章编程练习

    编写程序读取键盘输入,回显除数字外字符,同时大写转小写,小写转大写,遇'@'就停止. //练习6.1 读取键盘输入,回显输出(除数字),另外大写字母和小写字母互转,遇"@"则退出程 ...

  5. C Primer Plus第六版第七章编程题目与参考答案⭐

    1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数.换行符数和所有其他字符的数量. #include <stdio.h> #define STOP '#' #define SP ...

  6. C++primer plus第六版第四章编程题代码

    1. #include "stdafx.h" #include <iostream> #include <string> #include <vect ...

  7. C Primer Plus(第6版)第二章编程练习答案

    第二章  C语言概述(P37-P38) 5 编写一个程序,生成以下输出: Brazil, Russia, India, China India, China, Brazil, Russia 除了mai ...

  8. 《C Primer Plus》学习笔记—第12章

    目录 <C Primer Plus>学习笔记 第12章 存储类别.链接和内存管理 1.存储类别 1.作用域 2.链接 3.存储期 4.自动变量 1.程序hiding.c 2.没有花括号的块 ...

  9. java第二版课后题答案_Java语言程序设计第2版第16章 课后习题答案

    <Java语言程序设计第2版第16章 课后习题答案>由会员分享,可在线阅读,更多相关<Java语言程序设计第2版第16章 课后习题答案(62页珍藏版)>请在人人文库网上搜索. ...

  10. Python程序设计与算法基础教程(第二版)微课版第四章上机实践答案

    Python程序设计与算法基础教程(第二版)微课版第四章上机实践答案 2. def generate(L) : #生成杨辉三角的 一行List = [1]for x in range(1,len(L) ...

最新文章

  1. js获取浏览器活跃页面,切换tab页状态
  2. 截屏工具Snipaste使用指南
  3. XMPP协议简单介绍
  4. 手工成本维护不可以将成本改为零
  5. 前端那些年----Webstream快捷键备忘(mac)
  6. SystemTray.cpp
  7. 线程协作-CountDownLatch
  8. TRF7970A 天线
  9. linux命令存放 bash: xxx command not found
  10. ubuntu的web服务器_如何在Ubuntu上安装OpenLiteSpeed Web服务器?
  11. oracle单行函数 之 字符函数
  12. Typora最后的免费版本
  13. android虚拟机固定横屏幕竖屏,用VBox虚拟机安装Android 屏幕90度翻转竖屏设置
  14. 一文了解 TKG 如何使用 GPU 资源池
  15. 迅雷新财报背后:下载一哥到艰难求生
  16. 芯片供应最难的居然是TI,交期拉长
  17. TabIndex的问题
  18. 联想hx系列服务器,联想ThinkAgile HX系列 融合
  19. Unity3D粒子系统实现落叶效果
  20. 苹果id被锁定恢复方法(appleid被锁定怎么解除)

热门文章

  1. FOXBORO FCP270 P0917YZ 控制模块
  2. 计算机一级及wps试题,全国计算机一级WPS office选择题试题及答案
  3. 网站去除plugin.php小尾巴,清除hao123浏览器劫持小尾巴病毒
  4. 洛谷5249加特林轮盘赌
  5. Python天天美味(7) - 连接字符串(join %)
  6. 云端笔记系统-自动化测试
  7. jar包调用java -jar报错“Cannot run program“
  8. 虚拟机VMware下载及安装(Windows环境)
  9. 用Python在某乎爬取了5W+张表情包,问一句:斗图谁与争锋?
  10. WireShark抓包TCP三次握手和四次挥手