C++ Primer Plus课后复习题及编程练习第十章-对象和类

10.3 复习题

1.类:一种用户自定义的数据抽象组合,描述了程序中的数据成员机器成员函数和方法。

2.类通过将复杂的数据结构分解成为基本数据类型,并且定义实现了对复杂数据类型的操作。类中的数据成员以及成员函数通过(private,public,protected)实现了封装和数据隐藏

3.类是对象的抽象类型,对象是类的真实数据单元

4.性质上看,类的数据成员是描述类的基本数据构成,定义其存储空间和类型,而函数成员是类的操作功能的定义。存储上看,类创建的每一个新对象都有自己的存储空间,共享同一组函数成员,每个方法对于所有对象都仅有一个副本。

5.

class Bank
{
public:
	Bank();
	Bank(const string name, const string id, double money);
	~Bank();
	void set_bank(const string name, const string id, double money);
	void print_bank() const;
	void deposit(double money);
	void withdrawal(double money);

private:
	string name;
	string cordId;
	double money;
};

6.类的构造函数在创建对象时被调用,初始化对象;析构函数在对象被回收或者销毁时被调用。

7.

Bank(const string name, const string id, double cash) {
		fullname = name;
		cordId = id;
		money = cash;
	}

8.默认构造函数是没有参数的构造函数,能够创建构造函数而不用初始化对象。如果定义类时没有定义构造函数,系统会直接定义一个默认,而只要用户定义了任意一个构造函数,都需要手动定义一个默认构造函数。

9.

10.10 编程练习

1.

#include<iostream>
#include<string>
using namespace std;

class Bank
{
public:
	Bank();
	Bank(const string name, const string id, double cash) {
		fullname = name;
		cordId = id;
		money = cash;
	}
	~Bank();
	void set_bank(const string name, const string id, double cash) {
		fullname = name;
		cordId = id;
		money = cash;
	}
	void print_bank() const {
		cout << "fullname: " << fullname << endl;
		cout << "cordId: " << cordId << endl;
		cout << "money: " << money << endl;
	}
	void deposit(double cash) {
		money += cash;
	}
	void withdrawal(double cash) {
		money -= cash;
	}

private:
	string fullname;
	string cordId;
	double money;
};

Bank::Bank()
{
	fullname = "";
	cordId = "";
	money = 0;
}

Bank::~Bank()
{
	cout << "Done!" << endl;
}

int main() {

    //1.
	Bank ba = { "china","2024",100 };
	ba.print_bank();
	cout << "....................." << endl;
	Bank bc;
	bc.print_bank();
	cout << "....................." << endl;
	bc.set_bank("china_bc", "2024", 100);
	bc.print_bank();
	cout << "....................." << endl;
	ba.deposit(20);
	bc.withdrawal(50);
	ba.print_bank();
	cout << "....................." << endl;
	bc.print_bank();
	cout << "....................." << endl;

	system("pause");
	return 0;
}

2.

#include<iostream>
#include<string>
#pragma warning(disable:4996)//出现了“strcpy”不安全提示,可以加上这句
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") {
		lname = ln;
		strcpy(fname, fn);
	}
	void Show() const {
		cout << lname << " " << fname << endl;
	}
	void FormalShow() const {
		cout << fname << " " << lname << endl;
	}
};

int main() {

    //2.
	Person one;
	Person two("Smythecraft");
	Person three("Dimwiddy", "Sam");

	two.Show();
	two.FormalShow();
	cout << endl;
	three.FormalShow();


	system("pause");
	return 0;
}

5.

#include<iostream>
#include<stack>
using namespace std;

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

int main() {

    //5.
	stack<customer> s1;

	customer c;
	customer c1 = { "nihao1",20 };
	customer c2 = { "nihao2",30 };
	customer c3 = { "nihao3",40 };
	customer c4 = { "nihao4",50 };

	double count = 0;
	s1.push(c1);
	cout << s1.size() << endl;
	s1.push(c2);
	s1.push(c3);
	s1.push(c4);

	while (!s1.empty()) {
		count += s1.top().payment;
		cout << "Now count = " << count << endl;
		s1.pop();
	}


	system("pause");
	return 0;
}

6.

#include<iostream>
using namespace std;

class Move {
private:
	double x;
	double y;
public:
	Move(double a = 0, double b = 0) {
		x = a;
		y = b;
	}
	void showmove() const {
		cout << "x = " << x << ", y = " << y << endl;
	}
	Move add(const Move& m) const {
		Move count;
		count.x = x + m.x;
		count.y = y + m.y;
		return count;
	}
	void reset(double a = 0, double b = 0) {
		x = a;
		y = b;
	}
};

int main() {

    //6.
	Move a;
	Move b(12.5, 20);
	a.showmove();
	b.showmove();
	a = a.add(b);
	a.showmove();
	a.reset();
	a.showmove();
	a.reset(10, 23.5);
	a.showmove();
	a = a.add(b);
	a.showmove();


	system("pause");
	return 0;
}

7.

#include<iostream>
#include<string>
#pragma warning(disable:4996)
using namespace std;

class plorg {
private:
	static const int Max = 19;
	char p_name[Max];
	int p_CI;
public:
	void new_p(const char* str, const int CI = 50) {
		strcpy(p_name, str);
		p_CI = CI;
	}
	void reset_ci(int CI) {
		p_CI = CI;
	}
	void show_plorg() {
		cout << "The name: " << p_name << endl;
		cout << "The CI: " << p_CI << endl;
		cout << "................." << endl;
	}
	void begin_p() {
		strcpy(p_name, "Plorga");
		p_CI = 0;
	}
};

int main() {

    //7.
	plorg p;
	p.begin_p();
	p.show_plorg();
	p.new_p("lalala");
	p.show_plorg();
	p.new_p("helloworld", 100);
	p.show_plorg();
	p.reset_ci(10);
	p.show_plorg();

	system("pause");
	return 0;
}

8.

//头文件list.h
#pragma once

typedef unsigned long Item;

class List
{
public:
	List();
	~List();
	bool isempty() const;
	bool isfull() const;
	bool insert(const Item& item);
	void visit(const List& l);

private:
	static const int Max = 10;
	Item items[Max];
	int top;
};
//函数定义
#include<iostream>
#include"list.h"
using namespace std;

bool List::isempty() const {
	if (top == 0)
		return true;
}

bool List::isfull() const {
	if (top == Max)
		return true;
}
List::List() {
	top = 0;
}

bool List::insert(const Item& item) {
	if (top >= Max)
		return false;
	else
	{
		items[top++] = item;
		return true;
	}
}

void List::visit(const List& l) {
	cout << "Now top: " << l.top << endl;
	for (int i = 0; i < l.top; i++) {
		cout << l.items[i] << endl;
	}
}

List::~List() {

}
//测试
#include<iostream>
#include"list.h"
using namespace std;

int main() {

	List list;
	cout << "List is empty? " << list.isempty() << endl;
	list.insert(19.2);
	list.insert('a');
	list.insert(100);
	list.insert(true);
	list.visit(list);

	system("pause");
	return 0;
}

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值