本文介绍了我们可以将一个类的成员变量分配给另一个类的函数定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include < iostream.h >
#include < conio.h >
class time24;
class time12
{
int 小时,分钟,秒;
char * meridian;
public :
void getdata();
void putdata();
void summation(time24 x);
};
class time24
{
int 小时,分钟,秒;
public :
void getdata();
void conto12();
朋友 void time12 :: summation(time24 x);
};
void time12 :: summation(time24 x)
{
hour = hour + x.hour;
分钟=分钟+ x.minute;
秒=秒+ x.second;
meridian = PM;
if (second> = 60 )
{
minute =分+(秒/ 60);
秒=秒%60;
}
如果(分钟> = 60 )
{
小时=小时+(分钟/ 60);
分钟=分钟%60;
}
x.conto12();
}
void time24 :: conto12()
{
if (小时> 12)
{
小时=小时 - 12 ;
meridian = AM;
}
}
void time12 :: getdata()
{
cout< < \ internter hour:;
cin>>小时;
cout<< 输入分钟:;
cin>>分钟;
cout<< 输入第二个:;
cin>>秒;
}
void time24 :: getdata()
{
cout<< \ internter hour:;
cin>>小时;
cout<< 输入分钟:;
cin>>分钟;
cout<< 输入第二个:;
cin>>秒;
}
void time12 :: putdata()
{
cout<< time:<< hour<< :<< minute<< :<<< second<< << meridian;
}
void main()
{
clrscr();
time12 a;
time24 b;
a.getdata();
b.getdata();
a.summation(b);
a.putdata();
getch();
}
解决方案
#include<iostream.h> #include<conio.h> class time24; class time12 { int hour,minute,second; char *meridian; public: void getdata(); void putdata(); void summation(time24 x); }; class time24 { int hour,minute,second; public: void getdata(); void conto12(); friend void time12::summation(time24 x); }; void time12::summation(time24 x) { hour=hour+x.hour; minute=minute+x.minute; second=second+x.second; meridian="PM"; if(second>=60) { minute=minute+(second/60); second=second%60; } if(minute>=60) { hour=hour+(minute/60); minute=minute%60; } x.conto12(); } void time24::conto12() { if(hour>12) { hour=hour-12; meridian="AM"; } } void time12::getdata() { cout<<"\nenter hour:"; cin>>hour; cout<<"enter minute:"; cin>>minute; cout<<"enter second:"; cin>>second; } void time24::getdata() { cout<<"\nenter hour:"; cin>>hour; cout<<"enter minute:"; cin>>minute; cout<<"enter second:"; cin>>second; } void time12::putdata() { cout<<"time:"<<hour<<":"<<minute<<":"<<second<<" "<<meridian; } void main() { clrscr(); time12 a; time24 b; a.getdata(); b.getdata(); a.summation(b); a.putdata(); getch(); }
解决方案
这篇关于我们可以将一个类的成员变量分配给另一个类的函数定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!