C++程序设计谭浩强 第九章(关于类和对象的进一步讨论)习题答案(部分有改进)

9.1

构造函数的作用:用于新建对象的初始化工作。
 析构函数的作用:用于在撤销对象前,完成一些清理工作,比如:释放内存等。
 每当创建对象时,需要添加初始化代码时,则需要定义自己的构造函数;而对象撤销时,需要自己添加清理工作的代码时,则需要定义自己的析构函数。

9.2运行结果

#include <iostream>
using namespace std;
class Date
{
public:
	Date(int, int, int);
	Date(int, int);
	Date(int);
	Date();
	void display();
private:
	int month;
	int day;
	int year;
};

Date::Date(int m, int d, int y) :month(m), day(d), year(y)
{ }

Date::Date(int m, int d) : month(m), day(d)
{
	year = 2005;
}

Date::Date(int m) : month(m)
{
	day = 1;
	year = 2005;
}

Date::Date()
{
	month = 1;
	day = 1;
	year = 2005;
}

void Date::display()
{
	cout << month << "/" << day << "/" << year << endl;
}

int main()
{
	Date d1(10, 13, 2005);
	Date d2(12, 30);
	Date d3(10);
	Date d4;
	d1.display();
	d2.display();
	d3.display();
	d4.display();
	return 0;
}

结果:

9.3上题程序的第五行改用默认参数,即Date(int = 1,int = 1,int = 2005);       修改程序

#include <iostream>
using namespace std;
class Date
{
public:
	Date(int = 1, int = 1, int = 2005);
	void display();
private:
	int month;
	int day;
	int year;
};

Date::Date(int m, int d, int y) :month(m), day(d), year(y)
{ }

void Date::display()
{
	cout << month << "/" << day << "/" << year << endl;
}

int main()
{
	Date d1(10, 13, 2005);
	Date d2(12, 30);
	Date d3(10);
	Date d4;
	d1.display();
	d2.display();
	d3.display();
	d4.display();
	return 0;
}

9.4 建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出低1,3,5个学生的数据。

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void display();
private:
	int num;
	float score;
};

void Student::display()
{
	cout << num << "    " << score << endl;
}

int main()
{
	Student stud[5] = {
	 Student(101,78.5),
	 Student(102,85.5),
	 Student(103,98.5),
	 Student(104,100.0),
	 Student(105,95.5)   };
	Student* p = stud;
	for (int i = 0; i <= 2; p = p + 2, i++)
		p->display();
	return 0;
}

9.5 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	int num;
	float score;
};

void main()
{
	Student stud[5] = {
	 Student(101,78.5),
	 Student(102,85.5),
	 Student(103,98.5),
	 Student(104,100.0),
	 Student(105,95.5)    };
	void max(Student*);
	Student* p = &stud[0];
	max(p);
}

void max(Student* arr)
{
	float max_score = arr[0].score;
	int k = 0;
	for (int i = 1; i < 5; i++)
		if (arr[i].score > max_score) { max_score = arr[i].score; k = i; }
	cout << arr[k].num << " " << max_score << endl;
}

9.6 输出结果

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void change(int n, float s) { num = n; score = s; }
	void display() { cout << num << " " << score << endl; }
private:
	int num;
	float score;
};

int main()
{
	Student stud(101, 78.5);
	stud.display();
	stud.change(101, 80.5);
	stud.display();
	return 0;
}

结果

9.7 修改(1)将main函数第2行改为const Student stud(101, 78.5);

#include <iostream>
using namespace std;
class Student
{
public:
    Student(int n, float s) :num(n), score(s) {}
    void change(int n, float s) { num = n; score = s; }
    void display() { cout << num << " " << score << endl; }
private:
    int num;
    float score;
};

int main()
{
    const Student stud(101, 78.5);
    stud.display();
    //stud.change(101,80.5);
    stud.display();
    return 0;
}

情况:error C2662: “void Student::display(void)”: 不能将“this”指针从“const Student”转换为“Student &”

 (2)修改。用change函数修改数据成员num和score的值。

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void change(int n, float s) const { num = n; score = s; }
	void display() const { cout << num << " " << score << endl; }
private:
	mutable int num;
	mutable float score;
};

int main()
{
	const Student stud(101, 78.5);
	stud.display();
	stud.change(101, 80.5);
	stud.display();
	return 0;
}

(3)将main函数改为

 int main()
{
    Student stud(101, 78.5);
    Student* p = &stud;
    p->display();
    p->change(101, 80.5);
    p->display();
    return 0;
}

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void change(int n, float s) { num = n; score = s; }
	void display() { cout << num << " " << score << endl; }
private:
	int num;
	float score;
};

int main()
{
	Student stud(101, 78.5);
	Student* p = &stud;
	p->display();
	p->change(101, 80.5);
	p->display();
	return 0;
}

 (4)在(2)的基础上将main函数第3 行改为    const Student* p = &stud;

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void change(int n, float s) { num = n; score = s; }
	void display() const { cout << num << " " << score << endl; }
private:
	int num;
	float score;
};

int main()
{
	Student stud(101, 78.5);
	const Student* p = &stud;
	p->display();
	stud.change(101, 80.5);
	p->display();
	return 0;
}

(5)再把main函数第3行改为Student* const p = &stud;

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void change(int n, float s) { num = n; score = s; }
	void display() { cout << num << " " << score << endl; }
private:
	int num;
	float score;
};

int main()
{
	Student stud(101, 78.5);
	Student* const p = &stud;
	p->display();
	p->change(101, 80.5);
	p->display();
	return 0;
}

9.8  6题增加一个fun函数,改写main函数。在fun中调用change和display。fun中使用对象的引用(Student&)作为形参。

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n, float s) :num(n), score(s) {}
	void change(int n, float s) { num = n; score = s; }
	void display() { cout << num << " " << score << endl; }
private:
	int num;
	float score;
};

int main()
{
	Student stud(101, 78.5);
	void fun(Student&);
	fun(stud);
	return 0;
}

void fun(Student& stu)
{
	stu.display();
	stu.change(101, 80.5);
	stu.display();
}

9.9  商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者,还可以享受9.8折优惠。现已知当天3名销货员的销售情况为:
销货员号(num)销货件数(quantity)销货单价(price)
   101                   5                      23.5
   102                   12                     24.56
   103                   100                    21.5
请编程序,计算出当日此商品的总销售款sum,以及每件商品的平均售价。要求用静态数据成员和静态成员函数。

#include <iostream>
using namespace std;
class Product
{
public:
    Product(int n, int q, float p) :num(n), quantity(q), price(p) {};
    void total();
    static float average();
    static void display();

private:
    int num;
    int quantity;
    float price;
    static float discount;
    static float sum;
    static int n;
};

void Product::total()
{
    float rate = 1.0;
    if (quantity > 10) rate = 0.98 * rate;
    sum = sum + quantity * price * rate * (1 - discount);
    n = n + quantity;
}

void Product::display()
{
    cout << sum << endl;
    cout << average() << endl;
}

float Product::average()
{
    return(sum / n);
}


float Product::discount = 0.05;  //存在擅自打折
float Product::sum = 0;
int Product::n = 0;

int main()
{
    Product Prod[3] = {
      Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)
    };
    for (int i = 0; i < 3; i++)
        Prod[i].total();
    Product::display();
    return 0;
}

9.10 将例9.13程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据,输出年、月、日和时、分、秒。

#include <iostream>
using namespace std;
class Date;
class Time
{
public:
	Time(int, int, int);
	friend void display(const Date&, const Time&);
private:
	int hour;
	int minute;
	int sec;
};

Time::Time(int h, int m, int s)
{
	hour = h;
	minute = m;
	sec = s;
}

class Date
{
public:
	Date(int, int, int);
	friend void display(const Date&, const Time&);
private:
	int month;
	int day;
	int year;
};

Date::Date(int m, int d, int y)
{
	month = m;
	day = d;
	year = y;
}

void display(const Date& d, const Time& t)
{
	cout << d.month << "/" << d.day << "/" << d.year << endl;
	cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}


int main()
{
	Time t1(10, 13, 56);
	Date d1(12, 25, 2004);
	display(d1, t1);
	return 0;
}

9.11  将例9.13中的Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。

#include <iostream>
using namespace std;
class Time;
class Date
{
public:
	Date(int, int, int);
	friend Time;
private:
	int month;
	int day;
	int year;
};

Date::Date(int m, int d, int y) :month(m), day(d), year(y) { }

class Time
{
public:
	Time(int, int, int);
	void display(const Date&);
private:
	int hour;
	int minute;
	int sec;
};

Time::Time(int h, int m, int s) :hour(h), minute(m), sec(s) { }

void Time::display(const Date& d)
{
	cout << d.month << "/" << d.day << "/" << d.year << endl;
	cout << hour << ":" << minute << ":" << sec << endl;
}


int main()
{
	Time t1(10, 13, 56);
	Date d1(12, 25, 2004);
	t1.display(d1);
	return 0;
}

9.12   将例9.14改写为在类模板外定义各成员函数

#include <iostream>
using namespace std;
template<class numtype>
class Compare
 {public:
   Compare(numtype a,numtype b);
   numtype max();
   numtype min();
  private:
   numtype x,y;
 };
template <class numtype>
Compare<numtype>::Compare(numtype a,numtype b)
  {x=a;y=b;}
template <class numtype>
numtype Compare<numtype>::max()
 {return (x>y)?x:y;}
template <class numtype>
numtype Compare<numtype>::min()
  {return (x<y)?x:y;}

int main()
{Compare<int> cmp1(3,7);
 cout<<cmp1.max()<<" is the Maximum of two integer numbers."<<endl;
 cout<<cmp1.min()<<" is the Minimum of two integer numbers."<<endl<<endl;
 Compare<float> cmp2(45.78,93.6);
 cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
 cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;
 Compare<char> cmp3('a','A');
 cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;
 cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;
 return 0;
}


附赠题中提到的例9.13和例9.14

//例9.13
#include <iostream>
using namespace std;
class Date;
class Time
 {public:
   Time(int,int,int);
   void display(const Date&);
  private:
   int hour;
   int minute;
   int sec;
 };

 class Date
 {public:
   Date(int,int,int);
   friend void Time::display(const Date &);
  private:
   int month;
   int day;
   int year;
 };

 Time::Time(int h,int m,int s)
 {hour=h;
  minute=m;
  sec=s;
 }

void Time::display(const Date &da)
 {cout<<da.month<<"/"<<da.day<<"/"<<da.year<<endl;
  cout<<hour<<":"<<minute<<":"<<sec<<endl;
 }
 
 
Date::Date(int m,int d,int y)
 {month=m;
  day=d;
  year=y;
 }
   
int main()
{Time t1(10,13,56);
 Date d1(12,25,2004);
 t1.display(d1);
 return 0;
}
 
//例9.14
#include <iostream>
using namespace std;
template<class numtype>
class Compare
 {public:
   Compare(numtype a,numtype b)
    {x=a;y=b;}
   numtype max()
    {return (x>y)?x:y;}
   numtype min()
    {return (x<y)?x:y;}
  private:
    numtype x,y;
 };
int main()
{Compare<int> cmp1(3,7);
 cout<<cmp1.max()<<" is the Maximum of two inteder numbers."<<endl;
 cout<<cmp1.min()<<" is the Minimum of two inteder numbers."<<endl<<endl;
 Compare<float> cmp2(45.78,93.6);
 cout<<cmp2.max()<<" is the Maximum of two float numbers."<<endl;
 cout<<cmp2.min()<<" is the Minimum of two float numbers."<<endl<<endl;
 Compare<char> cmp3('a','A');
 cout<<cmp3.max()<<" is the Maximum of two characters."<<endl;
 cout<<cmp3.min()<<" is the Minimum of two characters."<<endl;
 return 0;
}

  • 43
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 《C面向对象程序设计第三版答案》是由谭浩强编写的一本与C语言相关的教材辅导答案。C面向对象程序设计是计算机科学中的一门重要课程,谭浩强作为资深教授和编程专家,他撰写的书籍在编程领域拥有很高的权威性。 这本教材答案为学习者提供了对应教材《C面向对象程序设计第三版》的习题答案和思考指导。习题是帮助学生巩固所学知识和提升编程能力的重要方式,通过对答案的学习,学生可以更好地理解并运用相关知识。学习者可以通过对比答案,分析解题思路、吸收优秀的编程风格和技巧,从而提高编程水平。 《C面向对象程序设计第三版答案》按照教材的章节顺序,详细解答了各个章节的习题,包括程序设计题、思考题、应用题等,涵盖了从基础的语法使用到复杂的程序设计技巧,旨在帮助学生全面理解并掌握C语言的面向对象编程思想和方法。 除了提供答案,这本教材还包括了一些习题的思考指导,指导学生如何分析问题、拆解问题、确定解决步骤等。这些思考指导帮助学生培养编程思维和解决问题的能力,使他们能够独立思考和解决实际编程任务。 总之,《C面向对象程序设计第三版答案》是一本为学习C语言面向对象程序设计的学生提供的辅助资料,它不仅提供了习题答案,还包括了思考指导,帮助学生提高编程水平和解决实际问题的能力。 ### 回答2: 《C++面向对象程序设计(第3版)》是计算机科学与技术专业学生的主要教材之一,由谭浩强编写。这本书全面介绍了C++编程语言的面向对象编程思想和相关的概念、原则与技巧。 该教材内容分为15章,首先从C++的基本概念和语法入手,然后逐渐介绍了面向对象编程的思想和实现。每章的结尾都提供了习题答案,帮助学生巩固所学知识。 《C++面向对象程序设计(第3版)》的答案谭浩强根据书中习题所提供的参考答案。这些答案精确明确,清晰易懂,配有详细的解释和示范代码。通过阅读和理解这些答案,学生可以加深对所学知识的理解,提高自己的编程技能。 同时,这本书还提供了大量的示例代码和实践案例,帮助学生将理论知识应用于实际的编程项目中。通过实践,学生可以更好地理解面向对象编程的思想和方法,并培养自己的分析和解决问题的能力。 总之,《C++面向对象程序设计(第3版)》是一本权威性、系统性和实用性并存的教材。通过学习这本书,学生可以全面掌握C++编程语言和面向对象编程的相关知识,提高自己的编程能力,并为将来的实际工作打下坚实的基础。 ### 回答3: 《C++面向对象程序设计》(第三版)是谭浩强所著的一本教材,该教材主要介绍了C++面向对象程序设计的基本概念、语法和技巧。全书共分为10个章节,涵盖了面向对象程序设计的各个方面。 第一章介绍了C++的发展历程以及面向对象程序设计的基本概念和特点。第二章详细讲解了C++的基本语法和常用数据型。第三章重点介绍了C++中的类和对象的概念,并通过具体的示例演示了如何定义和使用。 第四章讲解了C++的继承和派生,介绍了单继承和多继承的概念以及如何定义和使用派生。第五章介绍了C++中的多态性,包括静态多态和动态多态的概念以及如何使用虚函数实现动态绑定。 第六章讲解了C++中的运算符重载和型转换,通过实例说明了如何重载运算符和型转换函数。第七章介绍了C++中的异常处理机制,讲解了异常的概念和处理方法。 第八章讲解了C++中的文件操作,包括输入输出流、文件读写以及文件指针的相关知识。第九章介绍了C++的模板和泛型编程,包括函数模板和模板的定义和使用。 第十章介绍了C++中的标准模板库(STL),包括容器、迭代器、算法和函数对象等的使用方法。 《C++面向对象程序设计》(第三版)通过简明扼要的语言和生动具体的示例,全面而深入地介绍了C++面向对象程序设计的基本概念和技巧,适合初学者学习和参考。同时,该教材也提供了丰富的练习题和案例,供读者巩固和应用所学知识。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国服最强貂蝉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值