实验名称 |
实验二 类与对象的特性 |
实验目的和要求 |
|
(1) 了解静态对象的定义和使用方法; (2) 掌握静态数据成员和静态成员函数的定义和使用方法; (3) 理解类的作用域、对象的作用域及生存周期; (4) 掌握函数调用中参数的传递; (5) 掌握常量类型; (6) 掌握友元函数和友元类的定义及使用。 |
|
实验内容 |
|
1.定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。 2.输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。 3.设计一个程序,其中3个类CBabk、BBabk、GBank分别为中国银行类、工商银行类和农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另一个友元函数total用于计算储户在这3家银行中的总存款数。 |
|
主要仪器设备 |
台式或笔记本电脑 |
实验记录 |
|
1. #include<iostream> //#include"Rectangle.h" #include<cmath> using namespace std; class Rectangle { private: float length; protected: int tx, ty, bx, by; public: Rectangle(int = 10, int = 10, int = 20, int = 20); /* Rectangle(Rectangle &r)//拷贝构造 { tx = r.tx; ty = r.ty; bx = r.bx; by = r.by; }*/ void Diagonal(); void show(); ~Rectangle(){} }; Rectangle::Rectangle(int a, int b, int c, int d) { tx = a; ty = c; bx = d; by = b; } void Rectangle::Diagonal() { length = (double)sqrt((ty-by)*(ty-by) + (bx-tx)*(bx-tx)); } void Rectangle::show() { cout << "(" << tx << "," << ty << ")" << endl; cout << "(" << bx << "," << by << ")" << endl; cout << length << endl; } int main() { Rectangle *p = new Rectangle(10, 10, 20, 20); p -> Diagonal(); p -> show(); delete p; return 0; } 2. #include<iostream>
using namespace std;
class Student { string name, num;//姓名 学号 float score;//成绩 // float min = 0, max = 0; static float sum;//总成绩 static int count;//总人数
public: Student(string na, string nu, float g):name(na), num(nu), score(g){} void total(); // void M(); static float average(); void show();
};
//float Student::average = 0; void Student::total() { sum += score; count++; // averge = sum/count; }
float Student::average() { return (sum/count); }
void Student::show() { cout << sum << " " << count << " " << Student::average(); } int Student::count = 0; float Student::sum = 0; int main() { string na, nu, u, a; float g, g1, min, max; cin >> a >> u >> g1; Student stu(a, u, g1); stu.total(); min = max = g1;
while(cin >> na >> nu >> g) { if(max < g) max = g; if(min > g) min = g;
Student stu(na, nu, g); stu.total(); // stu.show();
} cout << max << " " << min << " ";
stu.show();
return 0; } 3. #include <iostream> using namespace std; class BBank; class GBank; class CBank { private: double balance; public: CBank(double i):balance(i){} friend void total(CBank &c, BBank &b, GBank &g); void show(){cout << balance;}; }; class BBank { private: double balance; public: BBank(double i):balance(i){} friend void total(CBank &c, BBank &b, GBank &g); }; class GBank { private: double balance; public: GBank(double i):balance(i){} friend void total(CBank &c, BBank &b, GBank &g); }; void total(CBank &c, BBank &b, GBank &g) //void total(CBank c, BBank b, GBank g)//感觉这里面的引用没必要,但引用了以后对象里的值不变? { cout << c.balance+b.balance+g.balance << endl; } int main() { double c,b,g; cin>>c>>b>>g; CBank c1(c); BBank b1(b); GBank g1(g); total(c1,b1,g1); return 0; } |
|
遇到的问题和解决方法 |
|
1. 静态成员数据和静态函数成员用的不熟 解决方法:看了看书,根据书上的代码变换了下 2.第三个友元函数的运用不太行 解决方法:目前还是有点晕,就关于引用不引用的问题 |
|
心得体会 |
|
光看书是不行的,还得实践,看懂了不一定会用。 友元函数还得查查。 |
#include <iostream>
using namespace std;
class BBank;
class GBank;
class CBank
{
private:
double balance;
public:
CBank(double i):balance(i){}
friend void total(CBank c, BBank b, GBank g);
void show(){cout << balance;};
};
class BBank
{
private:
double balance;
public:
BBank(double i):balance(i){}
friend void total(CBank c, BBank b, GBank g);
};
class GBank
{
private:
double balance;
public:
GBank(double i):balance(i){}
friend void total(CBank c, BBank b, GBank g);
};
//void total(CBank &c, BBank &b, GBank &g)
void total(CBank c, BBank b, GBank g)//有无&都可以
{
cout << c.balance+b.balance+g.balance << endl;
}
int main()
{
double c,b,g;
cin>>c>>b>>g;
CBank c1(c);
BBank b1(b);
GBank g1(g);
total(c1,b1,g1);
return 0;
}