C++面向对象程序设计第三章部分课后习题

三、

我修改的程序如下:

#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;
}

四、

方法一(一般法):

#include<iostream>
using namespace std;

class Student{
	public:
		Student(int s, int g):sno(s), grade(g){}
//		void output const();
//	private:
		int sno;
		float grade;
};

//void Student::output const()
//{
//	cout << "No." << sno << "'s grade is :" << grade << endl; 
//}

void output(const Student *p)
{
	cout << "No." << p->sno << "'s grade is :" << p->grade << endl; 
}

int main()
{
	Student s[5] = {
		Student(1, 85),
		Student(2, 90),
		Student(3, 95),
		Student(4, 99),
		Student(5, 100)
	};
	
	const Student * p;
	p = &s[0];
	output(p);
	p += 2;
	output(p);
	p += 2;
	output(p);
//	p = &s[4];
//	output(p);
	return 0;
}

方法二(使用了const保证类的成员数据的安全性):

#include<iostream>
using namespace std;

class Student{
	public:
		Student(int s, int g):sno(s), grade(g){}
		void output()const;
	private:
		int sno;
		float grade;
};

void Student::output()const
{
	cout << "No." << sno << "'s grade is :" << grade << endl; 
}

int main()
{
	Student s[5] = {
	Student(1, 85),
	Student(2, 90),
	Student(3, 95),
	Student(4, 99),
	Student(5, 100)
 };
 
	const Student * p;
	p = &s[0];
	p->output();
	p += 2;
	p->output(); 
	p += 2;
	p->output(); 
	return 0;
}

运行结果如下:
在这里插入图片描述

五、

#include<iostream>
using namespace std;

class Student{
	public:
		Student(int s, int g):sno(s), grade(g){}
		int getsno();
		float getgrade();
		void getData(); 
	private:
		int sno;
		float grade;
};

void Student::getData()
{
	cout << "No." << sno << '\t';
	cout <<	"grade:" << endl;
}

int Student::getsno()
{
	return sno;
}

float Student::getgrade()
{
	return grade;
}

int max(Student *p)
{
	float max = p->getgrade();
	int num = (*p).getsno();
	for(; p->getgrade() != '\0'; p++)
	{
		if(max < p->getgrade())
		{
			max = p->getgrade();
			num = p->getsno();
		}
		
	}
	return num;
}

int main()
{
	Student *p;
	Student s[5] = {
		Student(1, 89),
		Student(2, 90),
		Student(3, 96),
		Student(4, 92),
		Student(5, 10)
	};
	p = &s[0];
	
//	cout << s[0].getgrade();
//	cout << p->getgrade();
	
	int cnt = max(p);
	cout << "Winner : No." << cnt << "'s grade is :" << s[cnt - 1].getgrade() << endl;
	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);
	stud.display();
	stud.change(101, 80.5);
	stud.display();
	return 0;
}

运行结果如下:
在这里插入图片描述

七、

(1)常对象
(2)我修改后的程序如下:

#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)运行结果如下:
在这里插入图片描述
(4)我修改的C++代码如下:

#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();
	const Student *p = &stud;		//Student类常对象指针
	(*p).change(101, 80.5);
	p->display();
	return 0;
}

运行结果与上题一致

(5)

#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()
{
	Student stud(101, 78.5);
	stud.display();
	Student * const p = &stud;		//Student类常变量指针
	(*p).change(101, 80.5);
	p->display();
	return 0;
}

运行结果与上题一致

八、

详细C++代码如下:

#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;
};

void fun(Student &s, int n, float score)
{
	s.display();
	s.change(n, score);
	s.display();
}

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

九、

在这里插入图片描述
不知道是我没理解正确题目还是题目有问题,那句“对于一次购10以上的客户,还可以享受9.8折优惠”,然后再看所给的销售情况,销售情况里只有各个销货员总的销货件数,并没有涉及到客户是否是购了10件以上的,所以这题我就放飞自我按我自己理解的写了(但不得不说,谭浩强的C系列的书真的好多问题,这里不推荐初学者以这个为教材,我推荐C++ Primer)

详细C++代码如下:

#include<iostream>
using namespace std;

class Sell{
	public:
		static float aver();
		static void display();
		void total();
		Sell(int n, int q, float p): num(n), quantity(q), price(p){}
	private:
		static float discount;	//折扣 
		static float sum;		//总销售款sum 
		static int n;			//销售总件数n 
		int num;
		int quantity;
		float price;
};

float Sell::discount = 1.0;
float Sell::sum = 0;
int Sell::n = 0; 

float Sell::aver()
{
	return sum / n;
}

void Sell::display()
{
	cout << "总销售款:" << sum << endl;
	cout << "每件商品的平均售价:" << aver() << endl; 
}

void Sell::total()
{
	if(quantity > 10)
		discount = 0.98;
	else
		discount = 1.0;
	sum += price * discount * quantity;
	n += quantity;
}

int main()
{
	Sell s[3] = {
		Sell(101, 5, 23.5),
		Sell(102, 12, 24.56),
		Sell(103, 100, 21.5)
	};
	s[0].total();
	s[1].total();
	s[2].total();
	s[2].display();
	return 0;
}

运行结果如下:
在这里插入图片描述

十、

#include<iostream>
using namespace std;

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

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

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

void display(Date &d, Time &t)
{
	cout << d.month << "/" << d.day << "/" << d.year << endl;
	cout << t.hour << ":" << t.minute << ":" << t.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);
	display(d1, t1);
	return 0;
}

运行结果如下:
在这里插入图片描述

十一、

根据题意:将Time类声明为Date类的友元类,代码就是例3.13(严重怀疑谭*强语文不好)

#include<iostream>
using namespace std;

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

class Date{
	public:
		Date(int, int, int);
		friend void Time::display(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(Date &d)
{
	cout << d.month << "/" << d.day << "/" << d.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;
}

如果是将Date类声明为Time的友元类,则C++代码如下:

#include<iostream>
using namespace std;

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

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

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

void Date::display(Time &t)
{
	cout << month << "/" << day << "/" << year << endl;
	cout << t.hour << ":" << t.minute << ":" << t.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);
	d1.display(t1);
	return 0;
}

运行结果同上题

十二、

改写后的C++代码如下:

#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." << endl;
	cout << cmp1.min() << "is the Minimun." << endl;
	Compare<float> cmp2(45.78, 93.6);
	cout << cmp2.max() << "is the Maximum." << endl;
	cout << cmp2.min() << "is the Minimun." << endl;
	Compare<char> cmp3('a', 'A');
	cout << cmp3.max() << "is the Maximum." << endl;
	cout << cmp3.min() << "is the Minimun." << endl;
	return 0;
} 

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值