一、实验目的和要求
了解成员函数的特性,掌握静态成员、友元等概念。
二、实验内容
1、调试下列程序,写出输出结果,并分析输出结果。
- //sy4_1.cpp
- #include<iostream>
- using namespace std;
- class My
- {
- public:
- My(int aa)
- {
- A=aa;
- B-=aa;
- }
- static void fun(My m);
- private:
- int A;
- static int B;
- };
- void My::fun(My m)
- {
- cout<<"A="<<m.A<<endl;
- cout<<"B="<<B<<endl;
- }
- int My::B=100;
- int main()
- {
- My p(6),Q(8);
- My::fun(p);
- Q.fun(Q);
- return 0;
- }
//sy4_1.cpp
#include<iostream>
using namespace std;
class My
{
public:
My(int aa)
{
A=aa;
B-=aa;
}
static void fun(My m);
private:
int A;
static int B;
};
void My::fun(My m)
{
cout<<"A="<<m.A<<endl;
cout<<"B="<<B<<endl;
}
int My::B=100;
int main()
{
My p(6),Q(8);
My::fun(p);
Q.fun(Q);
return 0;
}
结果分析如下:
创建对象P、Q,执行语句My p(6),Q(8);时,调用构造函数使得A分别等于6和8,
2、分析并调试程序,完成下列问题。
- #include<iostream>
- #include<cmath>
- using namespace std;
- class My
- {
- public:
- My(double i=0){x=y=i;}
- My(double i,double j){x=i;y=j;}
- My(My&m){x=m.x;y=m.y;}
- friend double dist(My&a,My&b);
- private:
- double x,y;
- };
- double dist(My&a,My&b)
- {
- double dx=a.x-b.x;
- double dy=a.y-b.y;
- return sqrt(dx*dx+dy*dy);
- }
- int main()
- {
- My m1,m2(15),m3(13,14);
- My m4(m3);
- cout<<"The distance1:"<<dist(m1,m3)<<endl;
- cout<<"The distance2:"<<dist(m2,m3)<<endl;
- cout<<"The distance3:"<<dist(m3,m4)<<endl;
- cout<<"The distance4:"<<dist(m1,m2)<<endl;
- return 0;
- }
#include<iostream>
#include<cmath>
using namespace std;
class My
{
public:
My(double i=0){x=y=i;}
My(double i,double j){x=i;y=j;}
My(My&m){x=m.x;y=m.y;}
friend double dist(My&a,My&b);
private:
double x,y;
};
double dist(My&a,My&b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
int main()
{
My m1,m2(15),m3(13,14);
My m4(m3);
cout<<"The distance1:"<<dist(m1,m3)<<endl;
cout<<"The distance2:"<<dist(m2,m3)<<endl;
cout<<"The distance3:"<<dist(m3,m4)<<endl;
cout<<"The distance4:"<<dist(m1,m2)<<endl;
return 0;
}
(1)指出所有的构造函数,它们在本程序中分别起什么作用?
构造函数My(double =0)用来对m1,m2 进行初始化,My(double i,double j)对m3 进行初始化。拷贝构造函数My(My&m)用 来对m4 进行初始化。
(2)指出设置默认参数的构造函数。
My(double 1=0)为带默认参数的构造函数。
(3)指出友元函数,将友元函数放在私有部分,观察结果是否有变化。
dist()为友元函数,将其放在私有部分,编译仍是正确的,因为友元函数声明时仍与普通函数一致的,所以编译时不会出错。
(4)写出输出结果,并分析输出结果。
结果分析如下:
3、定义一个Student类,在该类定义中包括一个数据成员score(分数)、两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount(float s)用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平均值。在main()函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。
实验程序如下:
- #include<iostream>
- using namespace std;
- class student
- {
- public:
- void scoretotalcount(float s);
- static float sum();
- static float average();
- private:
- float score;
- static float total;
- static int count;
- };
- float student::total=0;
- int student::count=0;
- void student::scoretotalcount(float s)
- {
- score=s;
- total+=score;
- count++;
- }
- float student::sum(){return total;}
- float student::average(){return total/count;}
- int main()
- {
- float s;
- int n;
- student a[10];
- cout<<"输入学生的人数(10个以内):";
- cin>>n;
- cout<<"输入学生成绩:";
- for(int i(0);i<n;i++)
- {
- cin>>s;
- a[i].scoretotalcount(s);
- }
- cout<<"班级总分为:";
- cout<<student::sum()<<endl;
- cout<<"班级平均分为:";
- cout<<student::average()<<endl;
- return 0;
- }
#include<iostream>
using namespace std;
class student
{
public:
void scoretotalcount(float s);
static float sum();
static float average();
private:
float score;
static float total;
static int count;
};
float student::total=0;
int student::count=0;
void student::scoretotalcount(float s)
{
score=s;
total+=score;
count++;
}
float student::sum(){return total;}
float student::average(){return total/count;}
int main()
{
float s;
int n;
student a[10];
cout<<"输入学生的人数(10个以内):";
cin>>n;
cout<<"输入学生成绩:";
for(int i(0);i<n;i++)
{
cin>>s;
a[i].scoretotalcount(s);
}
cout<<"班级总分为:";
cout<<student::sum()<<endl;
cout<<"班级平均分为:";
cout<<student::average()<<endl;
return 0;
}
实验结果如下:
实验程序如下:
- #include<iostream>
- using namespace std;
- class Ruler;
- class Book
- {
- public:
- Book(int i=0){weight=i;}
- friend float totalWeight(Book&m,Ruler&n);
- private:
- float weight;
- };
- class Ruler
- {
- public:
- Ruler(int j=0){weight=j;}
- friend float totalWeight(Book&m,Ruler&n);
- private:
- float weight;
- };
- float totalWeight(Book&m,Ruler&n)
- {
- return m.weight+n.weight;
- }
- int main()
- {
- int i,j;
- cout<<"Book weight:";
- cin>>i;
- cout<<"Ruler weight:";
- cin>>j;
- Book B(i);
- Ruler R(j);
- cout<<"totalweight:"<<totalWeight(B,R)<<endl;
- return 0;
- }
#include<iostream>
using namespace std;
class Ruler;
class Book
{
public:
Book(int i=0){weight=i;}
friend float totalWeight(Book&m,Ruler&n);
private:
float weight;
};
class Ruler
{
public:
Ruler(int j=0){weight=j;}
friend float totalWeight(Book&m,Ruler&n);
private:
float weight;
};
float totalWeight(Book&m,Ruler&n)
{
return m.weight+n.weight;
}
int main()
{
int i,j;
cout<<"Book weight:";
cin>>i;
cout<<"Ruler weight:";
cin>>j;
Book B(i);
Ruler R(j);
cout<<"totalweight:"<<totalWeight(B,R)<<endl;
return 0;
}
结果如下:
分析与讨论
1、如何定义静态数据成员和成员函数?
答:静态成员函数是一种特殊的类成员,在类的许可范围内所有对象共享该数据。静态成员包括静态数据成员和静态成员函数,其定义格式为:static<静态成员的定义>;
2、如何对静态成员函数初始化?
答:静态数据成员的初始化与一般数据成员不同,它的初始化不能在构造函数中进行。静态数据成员初始化的格式为:
<数据类型><类名>::<静态数据成员名>=<初始值>
这里的作用域运算符“::”用来说明静态数据成员所属类。
3、静态成员函数访问静态成员与非静态成员有何区别?
答:由于静态成员函数没有this针,它只能直接访问该类的静态数据成员、静态成员函数和类以外的函数和数据,访问类中的非静态数据成员必须通过参数传递方式得到对象名,然后通过对象名来访问。
4、如何调用静态成员函数?
答:静态成员函数的调用格式为:
<类名>::<静态成员函数>(<参数表>);
或
<对象名>.<静态成员函数>(<参数表>);
5、如何理解“静态成员不是属于某个对象的,而是属于类的所有对象的。”这句话?
答:从静态成员是指声明为static的类成员,包括静态数据成员和静态成员函数,在类的范围内所有对象共享该数据。
6、比较友元函数与一般函数在定义和调用方面的异同。
答:友元函数的定义与一般函数的定义不同,友元函数的声明在类中,并在前面加上关键字friend;但友元函数的调用形式与一般函数相同。
三、实验心得
通过本次实验我们学会了成员函数的特性,掌握了静态成员与有元的概念,并学会开始初步使用静态成员和友元函数解决问题,把他们带入实验中有利于我们队他们的用法有了除了概念等理论性的东西有了进一步的加深了解,学会实际操作。把理论与实际结合更有利于我们对该成员函数的学习。虽然过程中总是出现许多问题,但是学习就是解决问题的过程,有了问题才会去尝试解决。希望在以后的学习中解决目前的问题。