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
3.如果将上题中程序的第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;
}
?
4.建立一个对象数组,内放5个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5个学生的数据。
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string,int);
void out();
private:
string number;
int score;
};
Student::Student(string n, int s):number(n),score(s)
{
}
void Student::out()
{
cout << number << ":" << score << endl;
}
int main()
{
Student a[] =
{
Student("2020033193",100),
Student("2020033194",61),
Student("2020033195",62),
Student("2020033196",63),
Student("2020033197",60),
};
Student* p = a;
p->out();
(p + 2)->out();
(p + 4)->out();
return 0;
}
5.建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string, int);
friend void max(Student*);
private:
string number;
int score;
};
Student::Student(string n, int s) :number(n), score(s)
{
}
void max(Student* p1)
{
int maxscore = p1[0].score;
for (int i = 0; i < 4; i++) {
if (maxscore < p1[i].score) {
maxscore = p1[i].score;
}
}
cout << maxscore << endl;
}
int main()
{
int b;
Student a[] =
{
Student("2020033193",100),
Student("2020033194",112),
Student("2020033195",62),
Student("2020033196",63),
Student("2020033197",60),
};
Student* p = a;
max(a);
}
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 101 80.5
7.?(简答题)将上题的程序分别作以下修改,分析所修改部分的含义以及编译和运行的情况。
(1) 将main函数第2行改为const Student stud(101,78.5)。
编译错误
(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;
}
其他部分仍同上题的程序。
(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;
}
?8. 修改第6题的程序,增加一个fun函数,改写main函数。在main函数中调用fun函数,在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 &stud) {
stud.display();
stud.change(101, 80.5);
stud.display();
}
int main()
{
Student stud(101, 78.5);
fun(stud);
return 0;
}
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 Discount {
public:
Discount(int, int, double);
static double average();
static void display();
void count();
private:
int num;
double price;
int quantity;
static int add;
static double sum;
static double discount;
};
double Discount::sum = 0;
double Discount::discount = 0.95;
int Discount::add = 0;
Discount::Discount(int n, int q, double p) :num(n), quantity(q), price(p)
{
}
void Discount::count()
{
if (quantity > 10)
{
sum += quantity * price* discount;
add += quantity;
}
else
{
sum += quantity * price;
add += quantity;
}
}
double Discount::average()
{
return sum / add;
}
void Discount::display()
{
cout << "平均售价:" << Discount::average() << endl;
cout << "总销售款:" << sum << endl;
}
int main()
{
Discount a[] =
{
Discount(101,5,23.5),
Discount(102,12,24.56),
Discount(103,100,21.5),
};
Discount* p = a;
for (int i = 0; i < 3; i++)
{
a[i].count();
}
Discount::display();
}
10.?(简答题)?将例3.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);
t1.display(d1);
return 0;
}
11.?(简答题)?将例3.13中的Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。
#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;
};
Time::Time(int h, int m, int s) :hour(h), minute(m), sec(s) {}
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) {}
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;
}
?12.将例3.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;
}
|