实验内容:
(1)有以下程序:
:# include
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 <<” ”<
int num ;
float score ;
} ;
void main ( )
{
Student stud (101, 78.5);
stud. display ( )
stud. change (101, 80.5 ) ;
stud . display( );
}
① 阅读此程序,分析其执行过程,然后上机运行,对比输出结果。
② 修改上面的程序,增加一个fun函数,改写main函数。在main函数中调用fun函数,在fun函数中调用change和display函数。在fun函数中使用对象的引用(Student &)作为形参。
(2)商店销售某一商品,商店每天公布统一的折扣(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(输出结果)。
(3)有以下程序:
#include
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 &da)
{cout<
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;
}
请读者分析和运行此程序,注意友元函数Time : : display 的作用。将程序中的display 函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据,输出年、月、日和时、分、秒。本题是《C++面向对象程序设计》 第3章第10题。
修改后上机调试和运行。
(4)有以下使用类模板程序(这是《C++面向对象程序设计》第3章例3.14的程序):#include
using namespace std;
template
class Compare
{public:
Compare(numtype a,numtype b)
{x=a;y=b;}
numtype max()
{return (x>y)?x:y;}
numtype min()
{return (x
numtype x,y;
};
int main()
{Compare cmp1(3,7);
cout<
cout<
cout<
}
① 运行此程序,体会类模板的作用。
② 将它改写为在类模板外定义各成员函数。
源码
"1.cpp“
# include //1
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&stud1)
{
stud1.display();
stud1.change(101,80.5);
stud1.display();
}
int main()
{
Student stud(101, 78.5);
void fun(Student&);
fun(stud);
"2.cpp"
#include //2
using namespace std;
class shangpin
{
private:
int num;
int quantity;
float price;
static int n;
static float discount;
static float sum;
public:
shangpin(int m, int q, float p) :num(m), quantity(q), price(p){};
void total();
static float average();
static void display();
};
float shangpin::discount = 0.9;
float shangpin::sum = 0;
int shangpin::n = 0;
void shangpin::total()
{
float yh = 1.0;
if (quantity > 10)yh = 0.98*yh;
sum += quantity*price*yh*discount;
n = n + quantity;
}
float shangpin::average()
{
return(sum / n);
}
void shangpin::display()
{
cout <<discount*10<<"折的销售款:"<< sum << endl;
cout << discount * 10 << "折的平均售价:" << average() << endl;
}
int main()
{
shangpin sp[3] = { shangpin(101, 5, 23.5), shangpin(102, 12, 24.56), shangpin(103, 100, 21.5) };
for (int i = 0; i < 3; i++)
sp[i].total();
shangpin::display();
}
"3.cpp"
#include //3
using namespace std;
class Date;
class Time
{
public:
Time(int, int, int);
friend void display(Date &,Time&t);
private:
int hour;
int minute;
int sec;
};
class Date
{
public:
Date(int, int, int);
friend void display(Date &, Time&t);
private:
int month;
int day;
int year;
};
Time::Time(int h, int m, int s)
{
hour = h;
minute = m;
sec = s;
}
void display(Date &da,Time&t)
{
cout << da.month << "/" << da.day << "/" << da.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(16, 30, 56);
Date d1(3, 30, 2019);
display(d1,t1);
return 0;
}
"4.cpp"
#include //4
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>
numtype Compare <numtype>::min()
{
return (x<y) ? x : y;
}
template<class numtype>
numtype Compare <numtype>::max()
{
return (x>y) ? x : y;
}
template<class numtype>
Compare<numtype>::Compare(numtype a, numtype b)
{
x = a; y = b;
}
int main()
{
Compare<int> cmp1(345, 678);
cout << cmp1.max() << "->在这两个整数中最大(整型)" << endl;
cout << cmp1.min() << "->在这两个整数中最小(整型)" << endl << endl;
Compare<double> cmp2(456.78, 9345.6);
cout << cmp2.max() << "->在这两个浮点型中最大(浮点型)" << endl;
cout << cmp2.min() << "->在这两个浮点型中最小(浮点型)" << endl << endl;
Compare<char> cmp3('b', 'B');
cout << cmp3.max() << "->在这两个字符中最大(字符型)" << endl;
cout << cmp3.min() << "->在这两个字符中最小(字符型)" << endl;
return 0;
}