天天看點

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

運作結果如下:

C++面向對象程式設計第三章部分課後習題

五、

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

運作結果如下:

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

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

運作結果如下:

C++面向對象程式設計第三章部分課後習題

七、

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

運作結果美麗:

C++面向對象程式設計第三章部分課後習題

(3)運作結果如下:

C++面向對象程式設計第三章部分課後習題

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

九、

C++面向對象程式設計第三章部分課後習題

不知道是我沒了解正确題目還是題目有問題,那句“對于一次購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;
}
           

運作結果如下:

C++面向對象程式設計第三章部分課後習題

十、

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

運作結果如下:

C++面向對象程式設計第三章部分課後習題

十一、

根據題意:将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;
} 
           

之後我會持續更新,如果喜歡我的文章,請記得一鍵三連哦,點贊關注收藏,你的每一個贊每一份關注每一次收藏都将是我前進路上的無限動力 !!!↖(▔▽▔)↗感謝支援!

繼續閱讀