【解】略。
【解】
//9.2
//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;
}
//10 / 13 / 2005
//12 / 30 / 2005
//10 / 1 / 2005
//1 / 1 / 2005
【解】
//9.3
//3.如果将第2题中程序的第5行改为用默认参数,即Date(int = 1, int = 1, int = 2005);
//分析程序有无问题。上机编译,分析出错信息,修改程序使之能通过编译。
//要求保留上面一行给出的构造函数,同时能输出与第2题程序相同的输出结果。
//已有默认参数构造函数时不再使用重载构造函数,否则会出现歧义,需要删去重载构造函数
#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
//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;
}
//101 78.5
//103 98.5
//105 95.5
【解】
//9.5
//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;
};
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) };
void max(Student*);
Student *p = &stud[0];
max(p);
return 0;
}
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
//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;
}
//101 78.5 (num和score的原值)
//101 80.5 (num和score的新值)
【解】
//7.将第6题程序分别作以下修改,分析所修改部分的含义以及编译和运行的情况。
//(1)将main函数第2行改为
//const Student stud(101, 78.5)
//1.stud被声明为常对象后,不能调用对象中的一般成员函数(除非把该成员函数也声明为const型),因此在main函数中调用stud.display()和stud.change()是非法的。
//2.若将对象stud声明为常对象,其值是不可改变的,而在主程序中,企图用stud.change函数去改变stud中的值,是非法的。
#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:
int num;
float score;
};
int main()
{
const Student stud(101, 78.5);
stud.display();
stud.change(101, 80.5);
stud.display();
return 0;
}
//(2)在(1)的基础上修改程序,使之能正常运行,用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;
//}
//其他部分仍同第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);
Student *p = &stud;
p->display();
p->change(101, 80.5);
p->display();
return 0;
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;
stud.display();
stud.change(101, 80.5);
stud.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() const { cout << num << " " << score << endl; }
private:
int num;
float score;
};
int main()
{
Student stud(101, 78.5);
Student *p = &stud;
stud.display();
stud.change(101, 80.5);
stud.display();
return 0;
}
【解】
//9.8
//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;
};
void fun(Student &st)
{
st.display();
st.change(101, 80.5);
st.display();
}
int main()
{
Student stud(101, 78.5);
fun(stud);
return 0;
}
【解】
//9.9
//9.商店销售某一商品,每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价(price),
// 在此基础上,一次购10件以上者,还可以享受9.8折优惠。现已知当天3个销货员销售情况为
// 销售员号(num) 销售件数(quantity) 销货单价(price)
// 101 5 23.5
// 102 12 24.56
// 103 100 21.5
//请编写程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数。
//(提示:将折扣discount,总销售款sum和商品销售总件数n声明为静态数据成员,
//再定义静态成员函数average(求平均售价)和display(输出结果))。
#include <iostream>
using namespace std;
class Product
{
public:
Product(int m, int q, float p) :num(m), 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) };
//定义Product类对象数组, 并给出数据
for (int i = 0; i < 3; i++) //统计3个销货员的销货情况
Prod[i].total();
Product::display(); //输出结果
return 0;
}
//2387.66 (总销售款)
//20.4073 (平均售价)
【解】
//9.10
//10.将例9.13程序中的display函数不放在Time类中,而作为类外的普通函数,
// 然后分别在Time和Date类中将display声明为友元函数。
// 在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的
// 私有数据,输出年、月、日和时、分、秒。请读者完成并上机调试。
#include <iostream>
using namespace std;
class Date; //对Date的声明, 它是对Date的预引用
class Time
{
public:
Time(int, int, int);
friend void display(const Date &, const Time &); //将普通函数display声明为朋友
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 &); //将普通函数display声明为朋友
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) //是Time和Date两个类的朋友
{
cout << d.month << "/" << d.day << "/" << d.year << endl; //引用Date类对象t1中的数据成员
cout << t.hour << ":" << t.minute << ":" << t.sec << endl; //引用Time类对象t1中的数据成员
}
int main()
{
Time t1(10, 13, 56); //定义Time类对象t1
Date d1(12, 25, 2004); //定义Date类对象d1
display(d1, t1); //调用display函数, 用对象名作实参
return 0;
}
//12 / 25 / 2004
//10:13 : 56
【解】
//9.11
//11.将例9.13中的Time类声明为Date类的友元类,通过Time类中的display函数
// 引用Date类对象的私有数据,输出年、月、日和时、分、秒。
#include <iostream>
using namespace std;
class Time;
class Date
{
public:
Date(int, int, int);
friend Time; //将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; //引用Date类对象d1的数据成员
cout << hour << ":" << minute << ":" << sec << endl; //引用Time类对象d1的数据成员
}
int main()
{
Time t1(10, 13, 56);
Date d1(12, 25, 2004);
t1.display(d1);
return 0;
}
//12 / 25 / 2004
//10:13 : 56
【解】
//9.12
//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;
}
//7 is the Maximum of two integers.
//3 is the Minimum of two integers.
//
//93.6 is the Maximum of two float numbers.
//45.78 is the Minimum of two float numbers.
//
//a is the Maximum of two characters.
//A is the Minimum of two characters .