用友元函數(shù)的方法重新設計“引用<題8>”中的類Point,并求兩個點之間的距離。
解:
將原來求兩個點的距離的普通函數(shù)distance()改寫為友元函數(shù)即可,可以看到采用友元函數(shù)方法使得代碼更簡潔。
本題程序如下:
#include
#include
class Point
{
int x,y;
public:
Point(int i,int j){x=i;y=j;}
friend float distance(Point &p1,Point &p2);
void disp()
{
cout<<"("< }
};
float distance(Point &p1,Point &p2) // 友元函數(shù)的實現(xiàn)
{
float d;
d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
return d;
}
void main()
{
Point p1(2,2),p2(5,5);
p1.disp(); cout<<"與"; p2.disp();
cout<<"之間距離="< }
本程序執(zhí)行結果如下:
(2,2)與(5,5)之間距離=4.24264
-------------------------------------------------------
題7.設計一個日期類Date,包括日期的年份、月份和日號,編寫一個友元函數(shù),求兩個日期之間相差的天數(shù)。
解:
該類中設計有3個友元函數(shù);count_day()函數(shù),它有兩個參數(shù),第2個參數(shù)是一個標志,當其值等于1 時,計算一年的開始到某日期的天數(shù);否則計算某日期到年尾的天數(shù)。leap()函數(shù)用于判斷指定的年份是否為閏年。subs()函數(shù)用于計算兩個日期之間的天數(shù)。
本題程序如下:
#include
#include
class Date
{
int year;
int month;
int day;
public:
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
void disp()
{
printf("%d.%d.%d",year,month,day);
}
friend int count_day(Date &d,int);
friend int leap(int year);
friend int subs(Date &d1,Date &d2);
};
int count_day(Date &d,int flag)
{
static int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
// 使用二維數(shù)組存放各月天數(shù),第一行對應非閏年,第二行對應閏年
int p,i,s;
if(leap(d.year))
p=1;
else p=0;
if(flag)
{
s=d.day;
for(i=1;i s+=day_tab[p][i-1];
}
else
{
s=day_tab[p][d.month]-d.day;
for(i=d.month+1; i<=12; i++)
s+=day_tab[p][i-1];
}
return s;
}
int leap(int year)
{
if(year%4==0&&year%100!=0||year%400==0) // 是閏年
return 1;
else // 不是閏年
return 0;
}
int subs(Date &d1,Date &d2)
{
int days,day1,day2,y;
if(d1.year {
days=count_day(d1,0);
for(y=d1.year+1; y if(leap(y))
days+=366L;
else
days+=365L;
days+=count_day(d2,1);
}
else if(d1.year==d2.year)
{
day1=count_day(d1,1);
day2=count_day(d2,1);
days=day2-day1;
}
else
days=-1;
return days;
}
void main()
{
Date d1(2000,1,1),d2(2002,10,1);
int ds=subs(d1,d2);
printf("輸出結果:\n ");
if(ds>=0)
{
d1.disp(); printf("與");
d2.disp(); printf("之間有%d天\n\n",ds);
}
else
printf("時間錯誤!\n");
}
本程序的執(zhí)行結果如下:
輸出結果:
2000.1.1與2002.10.1之間有1002天
-------------------------------------------------------
題8.編寫一個程序,設計一個Point類,包括學號、姓名和成績等私有數(shù)據(jù)成員,不含任何成員函數(shù),只將main()設置為該類的友元函數(shù)。
解:
main()函數(shù)與其它的函數(shù)一樣可以設置為類的友元函數(shù),這樣就可以在其中使用類對象的私有數(shù)據(jù)成員。
本題的程序如下:
#include
class Person
{
int no;
char name[10];
int deg;
public:
friend void main();
};
void main()
{
Person obj;
cout<<"輸入學號:";
cin>>obj.no;
cout<<"姓名:";
cin>>obj.name;
cout<<"成績:";
cin>>obj.deg;
cout<<"輸出結果"< cout<<"學生"< }
本程序執(zhí)行結果如下:
輸入學號: 10
姓名: Zhengming
成績:88
輸出結果
學生Zhengming(學號10)成績?yōu)?8
-------------------------------------------------------
題9.采用友元類的方式重新編寫“友元第04題“的程序。
解:
將原student類中的disp()成員函數(shù)和trans()友元函數(shù)作為友元類process的成員函數(shù)。其執(zhí)行結果與第4題的結果完全相同。
本題程序如下:
#include
#include
#include
class student
{
char name[10];
int deg;
char level[7];
friend class process; // 說明友元類
public:
student(char na[],int d)
{
strcpy(name,na);
deg=d;
}
};
class process
{
public:
void trans(student &s)
{
if(s.deg>=90)
strcpy(s.level,"優(yōu)");
else if(s.deg>=80)
strcpy(s.level,"良");
else if(s.deg>=70)
strcpy(s.level,"中");
else if(s.deg>=60)
解:
將原來求兩個點的距離的普通函數(shù)distance()改寫為友元函數(shù)即可,可以看到采用友元函數(shù)方法使得代碼更簡潔。
本題程序如下:
#include
#include
class Point
{
int x,y;
public:
Point(int i,int j){x=i;y=j;}
friend float distance(Point &p1,Point &p2);
void disp()
{
cout<<"("<
};
float distance(Point &p1,Point &p2) // 友元函數(shù)的實現(xiàn)
{
float d;
d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
return d;
}
void main()
{
Point p1(2,2),p2(5,5);
p1.disp(); cout<<"與"; p2.disp();
cout<<"之間距離="<
本程序執(zhí)行結果如下:
(2,2)與(5,5)之間距離=4.24264
-------------------------------------------------------
題7.設計一個日期類Date,包括日期的年份、月份和日號,編寫一個友元函數(shù),求兩個日期之間相差的天數(shù)。
解:
該類中設計有3個友元函數(shù);count_day()函數(shù),它有兩個參數(shù),第2個參數(shù)是一個標志,當其值等于1 時,計算一年的開始到某日期的天數(shù);否則計算某日期到年尾的天數(shù)。leap()函數(shù)用于判斷指定的年份是否為閏年。subs()函數(shù)用于計算兩個日期之間的天數(shù)。
本題程序如下:
#include
#include
class Date
{
int year;
int month;
int day;
public:
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
void disp()
{
printf("%d.%d.%d",year,month,day);
}
friend int count_day(Date &d,int);
friend int leap(int year);
friend int subs(Date &d1,Date &d2);
};
int count_day(Date &d,int flag)
{
static int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
// 使用二維數(shù)組存放各月天數(shù),第一行對應非閏年,第二行對應閏年
int p,i,s;
if(leap(d.year))
p=1;
else p=0;
if(flag)
{
s=d.day;
for(i=1;i
}
else
{
s=day_tab[p][d.month]-d.day;
for(i=d.month+1; i<=12; i++)
s+=day_tab[p][i-1];
}
return s;
}
int leap(int year)
{
if(year%4==0&&year%100!=0||year%400==0) // 是閏年
return 1;
else // 不是閏年
return 0;
}
int subs(Date &d1,Date &d2)
{
int days,day1,day2,y;
if(d1.year
days=count_day(d1,0);
for(y=d1.year+1; y
days+=366L;
else
days+=365L;
days+=count_day(d2,1);
}
else if(d1.year==d2.year)
{
day1=count_day(d1,1);
day2=count_day(d2,1);
days=day2-day1;
}
else
days=-1;
return days;
}
void main()
{
Date d1(2000,1,1),d2(2002,10,1);
int ds=subs(d1,d2);
printf("輸出結果:\n ");
if(ds>=0)
{
d1.disp(); printf("與");
d2.disp(); printf("之間有%d天\n\n",ds);
}
else
printf("時間錯誤!\n");
}
本程序的執(zhí)行結果如下:
輸出結果:
2000.1.1與2002.10.1之間有1002天
-------------------------------------------------------
題8.編寫一個程序,設計一個Point類,包括學號、姓名和成績等私有數(shù)據(jù)成員,不含任何成員函數(shù),只將main()設置為該類的友元函數(shù)。
解:
main()函數(shù)與其它的函數(shù)一樣可以設置為類的友元函數(shù),這樣就可以在其中使用類對象的私有數(shù)據(jù)成員。
本題的程序如下:
#include
class Person
{
int no;
char name[10];
int deg;
public:
friend void main();
};
void main()
{
Person obj;
cout<<"輸入學號:";
cin>>obj.no;
cout<<"姓名:";
cin>>obj.name;
cout<<"成績:";
cin>>obj.deg;
cout<<"輸出結果"<
本程序執(zhí)行結果如下:
輸入學號: 10
姓名: Zhengming
成績:88
輸出結果
學生Zhengming(學號10)成績?yōu)?8
-------------------------------------------------------
題9.采用友元類的方式重新編寫“友元第04題“的程序。
解:
將原student類中的disp()成員函數(shù)和trans()友元函數(shù)作為友元類process的成員函數(shù)。其執(zhí)行結果與第4題的結果完全相同。
本題程序如下:
#include
#include
#include
class student
{
char name[10];
int deg;
char level[7];
friend class process; // 說明友元類
public:
student(char na[],int d)
{
strcpy(name,na);
deg=d;
}
};
class process
{
public:
void trans(student &s)
{
if(s.deg>=90)
strcpy(s.level,"優(yōu)");
else if(s.deg>=80)
strcpy(s.level,"良");
else if(s.deg>=70)
strcpy(s.level,"中");
else if(s.deg>=60)

