目录
一、需要求3个长方体的体积,请编一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求同成员函数实现以下功能。
一、需要求3个长方体的体积,请编一个基于对象的程序。数据成员包括length(长)、width(宽)、height(高)。要求同成员函数实现以下功能。
(1)由键盘分别输入3个长方体的长、宽、高;
(2)计算长方体的体积;
(3)输出3个长方体的体积。
思路
该题目中长方体类只有一个计算体积的成员函数,于是使用内联函数的方式实现。分别获取三个长方体的长、宽、高,用于初始化长方体对象,然后调用函数输出长方体体积。
参考代码
#include <iostream>
using namespace std;
class Rectangle{
public:
Rectangle(float l,float w,float h){ //构造函数
length = l; width = w; height = h;
}
float volume(){ //求体积函数
return length*width*height;
}
private:
float length,width,height;
};
int main(){
float al,aw,ah,bl,bw,bh,cl,cw,ch,av,bv,cv;
//获取长方体a的长宽高
cout << "------- a -------" << endl;
cout << "Input length:";
cin >> al;
cout << "Input width:";
cin >> aw;
cout << "Input height:";
cin >> ah;
//获取长方体b的长宽高
cout << "------- b -------" << endl;
cout << "Input length:";
cin >> bl;
cout << "Input width:";
cin >> bw;
cout << "Input height:";
cin >> bh;
//获取长方体c的长宽高
cout << "------- c -------" << endl;
cout << "Input length:";
cin >> cl;
cout << "Input width:";
cin >> cw;
cout << "Input height:";
cin >> ch;
Rectangle a(al,aw,ah);
Rectangle b(bl,bw,bh);
Rectangle c(cl,cw,ch);
cout << "\n ---- volume ----" <<endl;
cout << "a: " << a.volume() << endl; //输出长方体a的体积
cout << "b: " << b.volume() << endl; //输出长方体b的体积
cout << "c: " << c.volume() << endl; //输出长方体c的体积
return 0;
}
结果
二、建立一个对象数组,内放5个学生的数据(学号、成绩)。
(1)用指针指向数组首元素,输出第1、3、5个学生的数据;
(2)设立一个函数max,用指向对象的指针作函数参数。在max函数中找出5个学生中学生成绩最高者,并输出其学号。
思路
建立一个对象数组,用指针指向其首地址,移动指针输出对象;函数Max以对象指针为参数,比较成绩找到最高者并输出。使用了参数初始化表初始化;主要需要注意对象数组建立,实际上是同时实例化一组对象。
参考代码
#include <iostream>
using namespace std;
class Student{
public:
Student(string num,float s):number(num),score(s){}; //使用参数初始化表初始化数据成员
void display(); //输出学生信息函数
friend void max(Student *p);
private:
string number; //学号
float score; //成绩
};
void Student::display(){
cout << "学号:" << number << endl;
cout << "成绩:" << score << endl;
cout << "------------"<< endl;
}
void max(Student *p){ //指向对象的指针作参数
Student *temp = p;
for(int i=0;i<5;i++)
if(temp->score<(temp+i)->score)
temp+=i;
cout << "成绩最高者为:"<< temp->number << endl; //输出成绩最高者学号
}
int main(){
Student Stu[]={Student("2018215112",96),Student("2018215110",98),Student("2019214120",87),Student("2019211124",95),Student("2019222111",66)};
Student *p = Stu;
for(p=Stu;p<Stu+5;p+=2)
p->display();
max(Stu);
}
结果
三、商店销售某一商品,商店每天公布统一的折扣(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(输出结果))
思路
使用对象数组实例化三个对象,在类中定义了静态成员变量和静态成员函数让数据为所有对象共享。每个对象调用total函数计算总销售额和总件数;求平均售价。要注意静态成员函数必须调用静态成员变量。
参考代码
#include <iostream>
using namespace std;
class Commodity{
public:
Commodity(int n,int q,float p):num(n),quantity(q),price(p){} //参数初始化表初始化
void total(); //求总销售额和总销售件数函数
static float average(); //求平均售价的静态成员函数
static void display(); //输出总销售额和平均售价的静态成员函数
private:
int num;
int quantity;
float price;
static int n;
static float discount,sum;
};
int Commodity::n = 0;
float Commodity::discount = 0.95;
float Commodity::sum = 0; //静态成员必须赋初始值
void Commodity::total(){
float rate = 1.0;
if(quantity>10)
rate = 0.98;
sum = sum + price*quantity*rate*discount; //求总销售额
n = n+quantity; //求总销售件数
}
float Commodity::average(){
return (sum/n); //求平均售价
}
void Commodity::display(){ //输出总销售额和平均售价
cout << "总销售额:" << sum << endl;
cout << "平均售价:" << average() << endl;
}
int main(){
Commodity Com[3] = {Commodity(101,5,23.5),Commodity(102,12,24.56),Commodity(1.3,100,21.5)}; //使用对象数组存放三个销售员情况
for(int i=0;i<3;i++)
Com[i].total(); //对于每个对象调用total函数,统计三个销售员的情况
Commodity::display(); //输出最终结果
return 0;
}
结果
四、定义一个复数类Complex,重载运算符“+”使之能用于复数之间的加法运算。参加运算的两个运算量都可以是类对象,也可以其中一个是整数,顺序任意。例如:c1+c2,i+c1,c1+i均合法(设i为整数,c1、c2为复数)。
编程:分别求两个复数之和,整数和复数之和。
思路
分别使用成员函数和友元函数的方式实现了复数类的“+”运算符重载,同时重载输出输出流。其中使用成员函数完成整数与复数相加重载时使用成员函数不易实现,使用友元函数的形式。通过本题目可以对比两种运算符重载方式在形式上的不同。
参考代码
//第一种实现
//以友元函数的形式重载复数加法运算
#include <iostream>
using namespace std;
class Complex{
public:
Complex(double r=0,double i=0){ //带默认参数构造函数
real = r;
image = i;
}
friend Complex operator +(Complex x,Complex y); //重载两个复数加法运算
friend Complex operator +(int k,Complex c); //重载整数与复数的加法运算
friend Complex operator +(Complex c,int k); //重载复数与整数的加法运算
friend ostream& operator <<(ostream&,const Complex&); //重载输出流
friend istream& operator >>(istream&,Complex&); //重载输入流
private:
double real,image;
};
Complex operator +(Complex x,Complex y){
return Complex(x.real+y.real,x.image+y.image);
}
Complex operator +(Complex c,int k) {
return Complex(c.real+k,c.image);
}
Complex operator +(int k,Complex c){
return Complex(c.real+k,c.image);
}
ostream& operator << (ostream& os,const Complex& c){
if(c.real != 0)
os << c.real;
if(c.image > 0)
os << '+' << c.image << 'i';
if(c.image < 0)
os << c.image << 'i';
return os;
}
istream& operator >>(istream& is,Complex& c){
cout << "输入实部:";
is >> c.real;
cout << "输入虚部:";
is >>c.image;
return is;
}
int main(){
Complex c1,c2;
int i;
cout << "输入c1:"<< endl;
cin >> c1;
cout << "c1 = " << c1 << endl;
cout << "------------" << endl; //展示复数c1结果
cout << "输入c2:" << endl;
cin >> c2;
cout << "c2 = " << c2 << endl;
cout << "------------" << endl; //展示复数c2结果
cout << "输入i:" << endl;
cin >> i;
cout << "i = " << i << endl;
cout << "------------" << endl; //展示整数i结果
Complex c3 = c1 + c2;
Complex c4 = c1 + i;
Complex c5 = i + c1;
cout << "------------" << endl;
cout << "c1 + c2 = " << c3 << endl; //复数相加
cout << "c1 + i = " << c4 << endl; //复数与整数相加
cout << "i + c1 = " << c5 << endl; //整数与复数相加
return 0;
}
//第二种实现
//以成员函数的形式重载复数加法运算
#include <iostream>
using namespace std;
class Complex{
public:
Complex(double r=0,double i=0){ //带默认参数构造函数
real = r;
image = i;
}
Complex operator +(Complex &c); //重载加法运算
Complex operator +(int k); //重载复数与整数的加法运算
friend Complex operator +(int k,Complex c); //重载整数与复数的加法运算
friend ostream& operator <<(ostream&,const Complex&); //重载输出流
friend istream& operator >>(istream&,Complex&); //重载输入流
private:
double real,image;
};
Complex Complex::operator +(Complex &c){
return Complex(real+c.real,image+c.image);
}
Complex Complex::operator +(int k) {
return Complex(real+k,image);
}
Complex operator +(int k,Complex c){
return Complex(c.real+k,c.image);
}
ostream& operator << (ostream& os,const Complex& c){
if(c.real != 0)
os << c.real;
if(c.image > 0)
os << '+' << c.image << 'i';
if(c.image < 0)
os << c.image << 'i';
return os;
}
istream& operator >>(istream& is,Complex& c){
cout << "输入实部:";
is >> c.real;
cout << "输入虚部:";
is >>c.image;
return is;
}
int main(){
Complex c1,c2;
int i;
cout << "输入c1:"<< endl;
cin >> c1;
cout << "c1 = " << c1 << endl;
cout << "------------" << endl; //展示复数c1结果
cout << "输入c2:" << endl;
cin >> c2;
cout << "c2 = " << c2 << endl;
cout << "------------" << endl; //展示复数c2结果
cout << "输入i:" << endl;
cin >> i;
cout << "i = " << i << endl;
cout << "------------" << endl; //展示整数i结果
Complex c3 = c1 + c2;
Complex c4 = c1 + i;
Complex c5 = i + c1;
cout << "c1 + c2 = " << c3 << endl; //复数相加
cout << "c1 + i = " << c4 << endl; //复数与整数相加
cout << "i + c1 = " << c5 << endl; //整数与复数相加
return 0;
}