實驗目的和要求
了解成員函數的特性,掌握靜态成員、友元等概念。
實驗内容
1.調試下列程式,寫出輸出結果,并分析輸出結果。
<span style="font-size:16px;">//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); //語句1
My::fun(p);
Q.fun(Q);
return 0;
}</span>
//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); //語句1
My::fun(p);
Q.fun(Q);
return 0;
}
輸出結果如下圖所示:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX9klaOJDOxoVds1WYwhHMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DM3UDMxUDNzEDNyQDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
分析:非靜态資料成員從屬于某個類,而靜态資料成員從屬于整個類。執行語句1時調用構造函數,使得對象P、Q的資料成員A的值分别是6和8,是以fun ()中輸出資料成員A的值分别是6和8。資料成員B的初始值為100,執行語句1,建立對象P,B的值改變為94,再建立對象Q時,B的值改變為86,是以fun()中輸出的資料成員B的值都是86,是以輸出結果為A=6 B=86 A=8 B=86。
2.分析并調試程式,完成下列問題。
(1)指出所有的構造函數,它們在本程式中分别起什麼作用?
第一個構造函數My(double i=0)用來初始化對象m1和m2 ,第二個構造函數My(double i,double j)用來初始化對象m3 。拷貝構造函數My(My&m)用來初始化對象m4。
(2)指出設定預設參數的構造函數。
預設參數的構造函數是My(double i=0)。
(3)指出友元函數,将友元函數放在私有部分,觀察結果是否有變化。
友元函數是dist()為,将dist()放到私有部分,編譯程式發現結果仍然是正确的。這是因為友元函數是一個在類裡聲明的普通函數,聲明位置可在類的任何部位,即可在public區也可在private區和protected區,意義完全一樣,是以編譯時不會出錯。
(4)寫出輸出結果,并分析輸出結果。
<span style="font-size:16px;">//sy4_2.cpp
#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;
}</span>
//sy4_2.cpp
#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;
}
輸出結果如下:
分析:友元函數dist()的主要作用是求兩點之間的距離。
3.定義一個Student類,在該類定義中包括一個資料成員score(分數)、兩個靜态資料成員total(總分)和學生人數count;成員函數scoretotalcount(float s)用于設定分數、求總分和累計學生人數;靜态成員函數sum()用于傳回總分;靜态成員函數average()用于求平均值。在main()函數中,輸入某班同學的成績,并調用上述函數求全班學生的總分和平均分。(sy4_3.cpp)
編寫程式如下:
print?
<span style="font-family:SimSun;font-size:16px;">//sy4_3.cpp
#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[8];
cout<<"請輸入學生的人數:";
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;
}</span>
//sy4_3.cpp
#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[8];
cout<<"請輸入學生的人數:";
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;
}
輸出結果如下:
4、聲明Book與Ruler兩個類,二者都有weight屬性,定義二者的一個友元函數totalWeight(),計算二者的重量和。(sy4_4.cpp)
編寫程式如下:
[cpp] view plain copy
print?
<span style="font-family:SimSun;font-size:16px;">//sy4_4.cpp
#include<iostream>
using namespace std;
class Ruler;
class Book
{
public:
Book(int x){weight=x;}
friend int totalWeight(Book&book,Ruler&ruler);
private:
int weight;
};
class Ruler
{
public:
Ruler(int x){weight=x;}
friend int totalWeight(Book&book,Ruler&ruler);
private:
int weight;
};
int totalWeight(Book&book,Ruler&ruler)
{
return book.weight+ruler.weight;
}
int main()
{
float x,y;
cout<<"請輸入“Book weight”:";
cin>>x;
cout<<"請輸入“Ruler weight”:";
cin>>y;
Book B(x);
Ruler R(y);
cout<<"totalweight:"<<totalWeight(B,R)<<endl;
return 0;
}</span>
//sy4_4.cpp
#include<iostream>
using namespace std;
class Ruler;
class Book
{
public:
Book(int x){weight=x;}
friend int totalWeight(Book&book,Ruler&ruler);
private:
int weight;
};
class Ruler
{
public:
Ruler(int x){weight=x;}
friend int totalWeight(Book&book,Ruler&ruler);
private:
int weight;
};
int totalWeight(Book&book,Ruler&ruler)
{
return book.weight+ruler.weight;
}
int main()
{
float x,y;
cout<<"請輸入“Book weight”:";
cin>>x;
cout<<"請輸入“Ruler weight”:";
cin>>y;
Book B(x);
Ruler R(y);
cout<<"totalweight:"<<totalWeight(B,R)<<endl;
return 0;
}
輸出結果如下:
分析與讨論
1.如何定義靜态資料成員和成員函數?
答:靜态成員函數是一種特殊的類成員,在類的許可範圍内所有對象共享該資料。靜态成員包括靜态資料成員和靜态成員函數,其定義格式為:static<靜态成員的定義>;
2.如何對靜态資料成員初始化?
答:靜态資料成員的初始化與一般資料成員不同,它的初始化不能在構造函數中進行。靜态資料成員初始化的格式為:
<資料類型><類名>::<靜态資料成員名>=<初始值>
這裡的作用域運算符“::”用來說明靜态資料成員所屬類。
3.靜态成員函數通路靜态成員與非靜态成員有何差別?
答:由于靜态成員函數沒有this針,它隻能直接通路該類的靜态資料成員、靜态成員函數和類以外的函數和資料,通路類中的非靜态資料成員必須通過參數傳遞方式得到對象名,然後通過對象名來通路。
4.如何調用靜态成員函數?
答:靜态成員函數的調用格式為:
<類名>::<靜态成員函數>(<參數表>);
或
<對象名>.<靜态成員函數>(<參數表>);
5.如何了解“靜态成員不是屬于某個對象的,而是屬于類的所有對象的。”這句話?
答:從靜态成員是指聲明為static的類成員,包括靜态資料成員和靜态成員函數,在類的範圍内所有對象共享該資料。
6.比較友元函數與一般函數在定義和調用方面的異同。
答:友元函數的定義與一般函數的定義不同,友元函數的聲明在類中,并在前面加上關鍵字friend;但友元函數的調用形式與一般函數相同。
實驗小:
這次實驗還是比較順利的,收獲也是比較大,主要還是老師講得就比較清楚,是以在整個實驗過程中還是很完成的比較好。