C++ Primer Plus第六版 第十二章 编程练习答案

博主分享了自己在C++实验期末考试前的经历,提到由于忙碌于其他课程的实验代码和个人论文,尚未深入完成C++ Primer Plus第六版的编程练习。
摘要由CSDN通过智能技术生成

开学到现在写了一堆代码 然而懒得写博客发2333333

AHU今年最长寒假40多天简直爽cry

话说今晚C++实验期末考试啊= =

最近一直在写各科实验的代码 还有自己论文的东西。。。这本书的题目都没怎么写= = 

//第一题
//main.cpp
#include "Cow.h"

int main()
{
	Cow a("Bob", "Fuck", 100);
	a.ShowCow();
	Cow b;
	b = a;
	b.ShowCow();

	system("PAUSE");
	return 0;
}

//Cow.h
#ifndef COW_H_
#define COW_H_

#include <iostream>
#include <cstring>

class Cow {
private:
	char name[20];
	char *hobby;
	double weight;
public:
	Cow();
	Cow(const char *nm, char *ho, double wt);
	Cow(const Cow &c);
	~Cow();
	Cow &operator=(const Cow &c);
	void ShowCow() const;
};

#endif

//Cow.cpp
#include "Cow.h"

Cow::Cow()
{
	name[0] = '\0';
	hobby = nullptr;
	weight = 0;
}

Cow::Cow(const char * nm, char * ho, double wt)
{
	strcpy_s(name, nm);
	hobby = ho;
	weight = wt;
}

Cow::Cow(const Cow & c)
{
	strcpy_s(name, c.name);
	hobby = c.hobby;
	weight = c.weight;
}

Cow::~Cow()
{
}

Cow & Cow::operator=(const Cow & c)
{
	if (this == &c)
		return *this;
	strcpy_s(name, c.name);
	hobby = c.hobby;
	weight = c.weight;
	return *this;
}

void Cow::ShowCow() const
{
	std::cout << "name: " << name << std::endl;
	std::cout << "hobby: " << hobby << std::endl;
	std::cout << "weight: " << weight << std::endl;
}



//第二题
//main.cpp
#include "string2.h"

int main()
{
	String s1(" and I am a C++ student.");
	String s2 = "Please enter your name: ";
	String s3;
	std::cout << s2;
	std::cin >> s3;
	s2 = "My name is " + s3;
	std::cout << s2 << ".\n";
	s2 = s2 + s1;
	s2.Stringup();
	std::cout << "The string\n" << s2 << "\ncontains " << s2.has('A') << " 'A' characters in it.\n";
	s1 = "read";
	String rgb[3] = { String(s1), String("green"), String("blue") };
	std::cout << "Enter the name of a primary color for mixing light: ";
	String ans;
	bool success = false;
	while (std::cin >> ans)
	{
		ans.Stringlow();
		for (int i = 0; i < 3; ++i)
		{
			if (ans == rgb[i])
			{
				std::cout << "That's right!\n";
				success = true;
				break;
			}
		}
		if (success)
			break;
		else
			std::cout << "Try again!\n";
	}
	std::cout << "Bye\n";
	return 0;
}

//string2.h
#ifndef STRING2_H_
#define STRING2_H_

#include <iostream>

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 &);
	~String();
	int length() const { return len; }
	String &operator=(const String &);
	String &operator=(const char *);
	char &operator[](int i);
	const char &operator[](int i) const;
	friend bool operator<(const String &st1, const String &st2);
	friend bool operator>(const String &st1, const String &st2);
	friend bool operator==(const String &st, const String &st2);
	friend std::ostream &operator<<(std::ostream &os, const String &st);
	friend std::istream &operator>>(std::istream &is, String &st);
	static int HowMany();

	friend String operator+(const String &st1, const String &st2);
	void Stringlow();
	void Stringup();
	int has(char c);
};

#endif

//string2.cpp
#include "string2.h"

#include <cstring>

int String::num_strings = 0;

int String::HowMany()
{
	return num_strings;
}

void String::Stringlow()
{
	const int num = 'a' - 'A';
	for (int i = 0; i < len; ++i)
	{
		if (str[i] >= 'A' && str[i] <= 'Z')
			str[i] += num;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值