要求:
(1) 了解静态对象的定义和使用方法;
(2) 掌握静态数据成员和静态成员函数的定义和使用方法;
(3) 理解类的作用域、对象的作用域及生存周期;
(4) 掌握函数调用中参数的传递;
(5) 掌握常量类型;
(6) 掌握友元函数和友元类的定义及使用。
1.定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。
#include<bits\stdc++.h>
using namespace std;
class Rectangle
{
protected:
double diagonal;
public:
void Diagonal(int left,int top,int right,int bottom)
{
Right=right;
Left=left;
Top=top;
Bottom=bottom;
diagonal=sqrt((Left-Right)*(Left-Right)+(Top-Bottom)*(Top-Bottom));
}
void Show()
{
cout<<"左上角坐标:"<<Left<<","<<Top<<endl;
cout<<"右上角坐标:"<<Right<<","<<Bottom<<endl;
cout<<"对角线长度:"<<diagonal<<endl;
}
protected:
int Left,Top;
int Right,Bottom;
};
int main()
{
Rectangle *r1;
r1=new Rectangle;
r1->Diagonal(10,10,20,20);
r1->Show();
//delete r1;
system("pause");
return 0;
}
2.输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。
#include<bits\stdc++.h>
//输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、
//最高成绩、最低成绩。
using namespace std;
class Student
{
string name;
long long ID;//学号
static int num;//学生总人数
static float avera;//平均成绩
float per;//成绩
//float Eng;//英语成绩
static float scor;//总成绩
static float hig;//最高成绩
static float low;//最低成绩
public:
Student(string a,long long b,float s);//定义构造函数
void chu();
void Show();//输出每个学生的姓名、学号、成绩
static void Num();
static void s();//输出最高成绩,最低成绩
static void score();//输出总成绩
};
int Student::num=0;
float Student::hig=0;
float Student::low=0;
float Student::avera=0;
float Student::scor=0;
Student::Student(string a,long long b,float s)
{
name=a;
ID=b;
per=s;
//Eng=c;
scor+=s;
hig=hig>per?hig:per;
low=low>per?per:low;
}
void Student::chu()
{
scor=per;
low=per;
}
void Student::Show()
{
num++;
cout<<name<<" "<<ID<<" "<<"成绩:"<<per<<" "<<endl;
}
void Student::Num()
{
cout<<"学生总人数是:"<<num<<endl;
}
void Student::s()
{
avera=scor/num;
cout<<"总成绩是:"<<scor<<endl;
cout<<"平均成绩是:"<<avera<<endl;
cout<<"最高成绩是:"<<hig<<endl;
cout<<"最低成绩是:"<<low<<endl;
}
int main()
{
Student s1("陈琳林",2020002360,90);
s1.chu();
s1.Show();
Student s2("魏源昕",2020002361,91);
s2.Show();
Student s3("张云潇",2020002363,92);
s3.Show();
Student::Num();
Student::s();
system("pause");
return 0;
}
3.设计一个程序,其中3个类CBabk、BBabk、GBank分别为中国银行类、工商银行类和农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另一个友元函数total用于计算储户在这3家银行中的总存款数。
#include<bits\stdc++.h>
using namespace std;
//中国银行类
class BBank;
class GBank;
class CBank
{
public:
friend int total(CBank a,BBank b,GBank c);
private:
int balance=100;
};
//工商银行类
class BBank
{
public:
friend int total(CBank a,BBank b,GBank c);
private:
int balance=230;
};
//农业银行类
class GBank
{
public:
friend int total(CBank a,BBank b,GBank c);
private:
int balance=200;
};
int total(CBank a,BBank b,GBank c)
{
return a.balance+b.balance+c.balance;
}
int main()
{
CBank A;
BBank B;
GBank C;
cout<<"三个银行的总存款数是:"<<endl;
cout<<total(A,B,C)<<endl;
system("pause");
return 0;
}
注意
- 注意用new申请空间要用delete来释放;
- 友元函数要正确使用,合理的使用其能访问私有数据成员的功能;
- 注意传常引用不能改变数据原本的值,搭配友元函数实现数据的保护;
- sqrt函数的使用,开根号;
- 用vector定义动态数组,vector 是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器。vector 是C++ STL的一个重要成员