运行时从键盘输入时、分、秒的值
#include<iostream>
using namespace std;
class Time
{
public: //数据成员设置为公有的
int hour;
int minute;
int sec;
};
Time t;
int main()
{
void set_time(void); //函数声明
void show_time(void); //函数声明
set_time(); //函数调用
show_time(); //函数调用
return 0;
}
void set_time(void)
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void show_time(void)
{
cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}
程序分析:
该段程序的特点:
- set_time和show_time为全局函数
- 数据成员为公有的
#include<iostream>
using namespace std;
class Time //声明类
{
public: //成员函数为公有的
void set_time(void) //类体内定义成员函数
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void show_time(void) //类体内定义成员函数
{
cout << hour << ":" << minute << ":" << sec << endl;
}
private: //数据成员为私有的
int hour;
int minute;
int sec;
};
Time t; //定义类对象
int main()
{
t.set_time(); //调用对象的成员函数
t.show_time(); //调用对象的成员函数
return 0;
}
程序分析:
该段程序的特点:
- 数据成员为私有的
- 输入和输出的功能由成员函数实现
- 在类体内定义成员函数
#include<iostream>
using namespace std;
class Time
{
public: //成员函数是公有的
void set_time(void); //公用成员函数原型声明
void show_time(void); //公用成员函数原型声明
private: //数据成员是私有的
int hour;
int minute;
int sec;
};
Time t;
int main()
{
t.set_time(); //调用函数
t.show_time(); //调用函数
return 0;
}
void Time::set_time(void) //类外定义成员函数
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time(void) //类外定义成员函数
{
cout << hour << ":" << minute << ":" << sec << endl;
}
程序分析:
该段程序的特点:
- 在类体内声明成员函数,而在类外定义成员函数
运行结果:
需要求3个长方柱的体积,编写一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求用成员函数实现以下功能:
- 由键盘分别输入3个长方体的长、宽、高
- 计算长方柱的体积
- 输出3个长方柱的体积
#include<iostream>
using namespace std;
class Box //定义Box类
{
public:
float volume(void);
void get_value(void);
void display(void);
private:
float length; //长
float width; //宽
float height; //高
};
float Box::volume(void) //计算体积的函数
{
return length*width*height;
}
void Box::get_value(void) //输入数据的函数
{
cout << "Please input length,width,height:";
cin >> length >> width >> height;
}
void Box::display(void) //输出结果的函数
{
cout << volume() << endl;
}
int main()
{
Box box1, box2, box3; //定义3个Box类的对象
box1.get_value(); //输入box1的数据
cout << "volume of box1 is:";
box1.display(); //输出box1的体积
box2.get_value();
cout << "volume of box2 is:";
box2.display();
box3.get_value();
cout << "volume of box3 is:";
box3.display();
return 0;
}
运行结果:
分析下面的程序,写出其运行时的输出结果
#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); //给定3个实参
Date d2(12, 30); //给定2个实参
Date d3(10); //给定一个实参
Date d4;
d1.display();
d2.display();
d3.display();
d4.display();
return 0;
}
运行结果:
- 如果将第4行改为默认参数,即
Date(int = 1, int = 1, int = 2015);
,分析程序
- 此时编译时会出现错误,因为构造函数使用默认参数后就不能在使用重载的构造函数,否则会出现歧义性。
可修改为:
#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); //给定3个实参
Date d2(12, 30); //给定2个实参
Date d3(10); //给定一个实参
Date d4;
d1.display();
d2.display();
d3.display();
d4.display();
return 0;
}
建立一个对象数组,内放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,92.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;
}
修改上题的程序,增加一个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;
};
int main()
{
Student stud(101, 78.5);
void fun(Student&); //声明fun函数
fun(stud); //调用fun函数,实参为对象stud
return 0;
}
void fun(Student& stu) //定义fun函数,形参为Student类对象的引用
{
stu.display(); //在fun函数中调用change和display函数
stu.change(101, 80.5);
stu.display();
}
阅读下面程序,分析其执行过程,写出输出结果
#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
- 如果把main函数中的第2行改为:
const Student stud(101, 78.5);
,会出现错误
商店销售某一商品,每天公布统一的折扣。同时允许销售人员在销售时灵活掌握售价,在此基础上,一次购10件以上者,还可以享受9.8折优惠。现已知当天3个销售员的销售情况为:
销货员号(num) | 销货件数(quantity) | 销货单价(price) |
---|---|---|
101 | 5 | 23.5 |
102 | 12 | 24.56 |
103 | 100 | 21.5 |
请编写程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。要求用静态数据成员和静态成员函数
#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类对象数组,并给出数据
Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5) };
for (int i = 0; i < 3; i++) Prod[i].total(); //统计3个销货员的销货情况
Product::display(); //输出结果
return 0;
}
运行结果: