C++ Primer Plus 第十章习题答案+收获

本文通过多个示例介绍了C++中的类和对象的使用,包括构造函数、成员函数、对象赋值、静态变量、结构体、栈操作以及枚举。示例涵盖了类的声明、实现和使用,展示了如何在类中处理字符串、计算销售数据、管理高尔夫球手信息、模拟栈操作和定义带有默认参数的结构体。
摘要由CSDN通过智能技术生成

目录

10.1

10.2

10.3

10.4

10.5

10.6

10.7


10.1

//class.h

#pragma once

#ifndef _CLASS_H_
#define _CALSS_H_

#include<string>


class bank
{
private:
	std::string name;
	std::string account;
	double deposit;
public:
	bank(std::string n1, std::string a1, double d1); //自定义构造函数
	bank(); //默认构造函数
	void show();
	void input(double d1);
	void output(double d1);
};


#endif


//fuc.cpp

#include<iostream>
#include<cstring>
#include"class.h"
using namespace std;


bank::bank(std::string n1, std::string a1, double d1)  //此时传递的string参数不能是引用,引用的话传递的参数应当是某个变量的引用,但初始化时很少会有这种情况
{
	name = n1;
	account = a1;
	deposit = d1;
}
bank::bank()
{
	name = "none";
	account = "none";
	deposit = 0;
}
void bank::show()
{
	cout << "name: " << name << endl;
	cout << "account: " << account << endl;
	cout << "deposit: " << deposit << endl;
	cout << "-------------------------" << endl;
}
void bank::input(double d1)
{
	if (d1 < 0)
	{
		cout << "the input number can not under zero" << endl;
	}
	else
	{
		deposit += d1;
	}
}
void bank::output(double d1)
{
	if (d1 < 0)
	{
		cout << "the output number can not under zero" << endl;
	}
	else
	{
		deposit -= d1;
	}
}


//main.cpp

#include<iostream>
#include"class.h"
using namespace std;


int main()
{
	bank mem1 = bank("GodFishhh", "1164219023", 29300);
	mem1.show();
	cout << "input 5000 Yuan" << endl;
	mem1.input(5000);
	mem1.show();
	cout << "output 3000 Yuan" << endl;
	mem1.output(3000);
	mem1.show();



	system("pause");
	return 0;
}

//创建构造函数时,应当避免按引用传递参数:按引用传递时传递的实参应当是某个变量的引用,但给对象初始化时一般会直接进行赋值,而不是调用别的变量的引用.

10.2

//class.h

#pragma once

#include<string>
using namespace std;

class Person
{
private:
	static const int LIMIT = 25;
	string lname;
	char fname[LIMIT];
public:
	Person()
	{
		lname = "";
		fname[0] = '\0';   //字符串数组第一个元素就是空字符,相当于整个数组为空
	}
	Person(const string& ln,const char* fn = "Heyyou");  //只在函数的声明中填写默认参数
	void Show()const;
	void FormalShow()const;
};


// fuc.cpp

#include<iostream>
#include<cstring>
#include"class.h"


Person::Person(const string& ln, const char* fn)
{
	lname = ln;
	strcpy_s(fname, fn);

}
void Person::Show()const
{
	cout << fname << " " << lname << endl;
}
void Person::FormalShow()const
{
	cout << lname << " " << fname;
}


// main.cpp

#include<iostream>
#include"class.h"


int main()
{
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");
	one.Show();
	one.FormalShow();
	cout << endl;
	cout << "------------------" <<endl;
	two.Show();
	two.FormalShow();
	cout << endl;
    cout << "------------------" <<endl;
	three.Show();
	three.FormalShow();
	cout << endl;


	system("pause");
	return 0;

}

10.3

//class.h

#pragma once

#include<string>

class golf
{
private:
	std::string fullname;
	int handicap;
public:
	golf();//默认构造函数
	golf(std::string fname, int hc);
	void setgolf(golf& g1);
	void show();
};


// fuc.cpp

#include"class.h"
#include<iostream>
using namespace std;

golf::golf()
{
	fullname = "";
	handicap = 0;
}

golf::golf(string fname, int hc)
{
	fullname = fname;
	handicap = hc;
}
void golf::setgolf(golf& g1)
{
	*this = g1; //类对象的赋值  (*this是调用函数的对象)
}
void golf::show()
{
	cout << "fullname: " << fullname << endl;
	cout << "handicap: " << handicap << endl;
}

// main.cpp

#include<iostream>
#include"class.h"
using namespace std;


int main()
{
	golf golfone;
	golf golftwo = golf("GodFishhh", 10);
	golfone.setgolf(golftwo); //调用函数的对象是golfone,所以此时的this指针指向的是golfone的地址,所以 *this 就相当于golfone
	cout << "golftwo: " << endl;
	golftwo.show();
	cout << "golfone: " << endl;
	golfone.show();

	system("pause");
	return 0;
}

10.4

// class.h

#pragma once

#ifndef _CLASS_H_
#define _CLASS_H_


namespace SALES
{
	class Sales
	{
	private:
		static const int QUARTERS = 4;
		double sales[QUARTERS];
		double average;
		double max;
		double min;
		double aver;
	public:
		Sales(double s[], int n); //非交互版
		Sales(); //交互版
		void show();
	};
}


#endif


// fuc.cpp

#include<iostream>
#include"class.h"
using namespace std;

namespace SALES
{
	Sales::Sales(double s1[], int n)  //非交互版
	{
		//赋值
		aver = 0, max = 0, min = 999;
		for (int i = 0; i < n; i++)
		{
			sales[i] = s1[i];
		}
		//找出max,min,算出average
		double  total = 0;;
		for (int i = 0; i < n; i++)
		{
			if (sales[i] >max)
			{
				max = sales[i];
			}
			if (sales[i] < min)
			{
				min = sales[i];
			}
			total += sales[i];
		}
		aver = total/ n;
	}
	Sales::Sales()  //交互版
	{
		max = 0, min = 999;
		aver = 0;
		double total = 0;
		cout << "please input for the array" << endl;
		for (int i = 0; i < QUARTERS; i++)
		{
			cin >> sales[i];
		}
		for (int i = 0; i < QUARTERS; i++)
		{
			if (sales[i] > max)
			{
				max = sales[i];
			}
			if (sales[i] < min)
			{
				min = sales[i];
			}
			total += sales[i];
		}
		aver = total / QUARTERS;
	}
	void Sales::show()
	{
		cout << "Sales infor: " << endl;
		
		for (int i = 0; i < QUARTERS; i++)
		{
			cout << "Sale " << i+1 << ":" << sales[i] << endl;
		}
		cout << "max: " << max << endl;;
		cout << "min: " << min << endl;;
		cout << "average: " << aver << endl;;
	}
}

// main.cpp

#include<iostream>
#include"class.h"
using namespace std;


int main()
{
    using namespace SALES;
    double arr1[4] = { 2,4,8,12 };
    cout << "非交互版: " << endl;
    Sales sales1 = Sales(arr1, 4);
    sales1.show();
    cout << "交互版: " << endl;
    Sales sales2 = Sales();
    sales2.show();

    system("pause");
    return 0;
}

//局部变量会覆盖名称空间中的变量以及全局变量(局部变量>名称空间变量,局部变量>全局变量)

10.5

// class.h

#pragma once

#ifndef _CLASS_H_
#define _CLASS_H_

struct customer
{
	char fullname[35];
	double payment;
};

typedef customer Item;  //使得Item相当于customer


class Stack
{
private:
	static const int MAX = 10;
	Item items[MAX];  //结构体数组看作栈区域
	int top; //标记栈目前变量所处的位置
	double total = 0; //标记payment总数(每当customer结构被删除时,其payment的值都呗加入到total中)
public:
	Stack();  //默认构造函数
	bool isempty()const;
	bool isfull()const;
	bool push(const Item& item);   // push过程就是给当前top位置空的数组插入相应的变量
	bool pop(Item& item);   // pop过程就是使得当前top位置向前移一位,并将此位相应的变量移出栈区}
};

#endif


// fuc.cpp


#include<iostream>
#include"class.h"
using namespace std;


Stack::Stack()  //默认构造函数
{
	top = 0; // 标记从栈的最底端开始
}
bool Stack::isempty()const
{
	if (top == 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Stack::isfull()const
{
	if (top == MAX)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Stack::push(const Item& item) // push过程就是给当前top位置空的数组插入相应的变量
{
	if (top < MAX)
	{
		items[top++] = item;   //此处的top指向的栈区域为空,所以 top++ 后增即可
		return true;
	}
	else
	{
		return false;
	}
}
bool Stack::pop(Item& item) // pop过程就是使得当前top位置向前移一位,并将此位相应的变量移出栈区}
{
	if (top > 0)
	{
		item = items[--top];  //此处的top为空,所以要 --top 前减移到下一位,然后将此位置的值赋给item结构体
		total += items[top].payment;  //此时应为 top,不能再前减了,因为上一行代码已经做了前减处理
		cout << "The total payment is:" << total << "$"<<endl;
		return true;
	}
	else
	{
		return false;
	}
}


 // main.cpp

#include<iostream>
#include<cctype>   //用到了toupper()  -- 将小写字母改为大写字母  和 isalpha() -- 判断是否为字母 两个函数
#include"class.h"
using namespace std;

int main()
{
	Stack st;  //创建类对象时自动调用的默认构造函数,将top赋值为0
	Item tempstruct;
	char ch;
	cout << "Please enter A to add a purchase order" << endl;
	cout << "P to process a tempstruct or Q to quit" << endl;
	while (cin >> ch && toupper(ch) != 'Q')
	{
		while (cin.get() != '\n')
		{
			continue;
		}
		if (!isalpha(ch))
		{
			cout << '\a';
			continue;
		}
		switch (ch)
		{
		case 'a':
		case 'A':
			cout << "Enter a tempstruct" << endl;
			cout << "fullname: " << endl;
			cin.get(tempstruct.fullname, 35);
			cout << "payment: " << endl;
			cin >> tempstruct.payment;
			if (st.isfull())
			{
				cout << "Stack is already full" << endl;
			}
			else
			{
				st.push(tempstruct);
			}
			break;

		case 'p':
		case 'P':
			if (st.isempty())
			{
				cout << "Stack is already empty" << endl;
			}
			else
			{
				st.pop(tempstruct);
				cout << tempstruct.fullname << " has been popped " << endl;
				cout << tempstruct.payment << " has benn popped " << endl;
			}
			break;

		case 'q':
		case 'Q':break;

		default:
			cout << "wrong input,please input by following the instruction" << endl;
		}
		cout << "Please enter A to add a purchase order" << endl;
		cout << "P to process a tempstruct or Q to quit" << endl;
	}
	cout << "Bye~" << endl;

	system("pause");
	return 0;

}

//本道题和之前模拟栈的例题唯一的区别就是该题中栈中储存的是结构体变量

//注意点: 

1.因为栈储存的是结构体变量,所以栈要模拟成结构体数组,同时函数传递的参数为结构体,给结构体赋值时也要注意相关的语法。

2.题目中附加了一个每次pop后都要将payment的值累加到total中,而这个total被保存在类的private空间中,所以只能在public的函数中对total值进行改变以及相关的输出显示

10.6

// class.h

#pragma once

#ifndef _CLASS_H_
#define _CLASS_H_

class Move
{
private:
	double x;
	double y;
public:
	Move(double a = 0, double b = 0);
	void showmove()const;
	Move add(const Move& m)const; //将m中的x和y的值赋给调用此函数的对象,返回值为一个Move对象
	void reset(double a = 0, double b = 0);
};

#endif


// fuc.cpp

#include<iostream>
#include"class.h"
using namespace std;

Move::Move(double a, double b)
{
	x = a;
	y = b;
}
void Move::showmove()const
{
	cout << "x = " << x << endl;
	cout << "y = " << y << endl;
}
Move Move::add(const Move& m)const  //创建一个Move对象,并将参数中的x,y和调用该函数的对象的x,y全都赋值给这个Move对象,并返回他
{
	Move temp;
	temp.x = x + m.x;
	temp.y = y + m.y;
	return temp;
}
void Move::reset(double a, double b)
{
	x = a;
	y = b;
}


// main.cpp

#include<iostream>
#include"class.h"
using namespace std;


int main()
{
	Move m1 = Move(2, 3);
	Move m2 = Move(10, 20);
	cout << "m1:" << endl;
	m1.showmove();
	cout << "m2:" << endl;
	m2.showmove();
	cout << "After adding" << endl;
	Move m3 = m1.add(m2);
	m3.showmove();
	cout << "After resetting" << endl;
	m3.reset();
	m3.showmove();



	system("pause");
	return 0;
}

10.7

// class.h

#pragma once

#ifndef _CLASS_H_
#define _CLASS_H_

class plorg
{
private:
	enum {MAX=20}; // 等同于 static const int MAX =20;
	char name[MAX];
	int CI;
public:
	plorg();//默认构造函数
	plorg(int n,const char arr[]="Plorga");  //默认参数的设置要从右到左,因为默认参数右端的所有参数都必须有默认值 (同时默认参数的设置只在函数的声明中,定义中不需要)
	void resetCI(int n);
	void show();
};

#endif


// fuc.cpp

#include<iostream>
#include<cctype> //用到了strcpy给char字符串赋值
#include"class.h"
using namespace std;


plorg::plorg()//默认构造函数
{
	strcpy_s(name, " ");
	CI = 0;

}
plorg::plorg(int n, const char arr[]) //默认参数的设置要从右到左,因为默认参数右端的所有参数都必须有默认值 (同时默认参数的设置只在函数的声明中,定义中不需要)
{
	CI = n;
	strcpy_s(name, arr);
}
void plorg::resetCI(int n)
{
	CI = n;
}
void plorg::show()
{
	cout << "name: " << name << endl;
	cout << "CI: " << CI << endl;
}


// main.cpp

#include<iostream>
#include"class.h"
using namespace std;


int main()
{
    cout << "p1: " << endl;
    plorg p1(10, "GodFishhh");
    p1.show();

    cout << "p2: " << endl;
    plorg p2;
    p2.show();

    cout << "After resetting the CI of p1: " << endl;
    p1.resetCI(30);
    p1.show();


    system("pause");
    return 0;
}

//整理了下默认参数的注意点

1.默认参数要从右往左来设置,因为默认参数的右边的所有参数都必须有默认值

2.设置默认参数时只在函数的声明处设置,而函数的定义处无需设置(重复设置会出现错误)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值