我们可以将一个类的成员变量分配给另一个类的函数定义吗? [英] can we assign member variable of one class to function definition of another class?
本文介绍了我们可以将一个类的成员变量分配给另一个类的函数定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#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();
}
解决方案
您可以通过所谓的前向声明来实现。这是一个简单的陈述。但是在实现文件中需要是该类的标题。作为参数你最好使用指针。
但我建议你不要在真正的代码中这样做。它依赖于依赖关系及其问题。最好使用接口的原生数据类型。
总结示例:
void time12 :: summation( int h24, int m24, int s24);
它提高了可维护性和稳定性。如果你这样做而不是把它看作单程票。永远不能。 :-O
#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();
}
解决方案
You can do it via so called "forward declaration". It is a simple statement as you done it. But in the implemention file needs to be the header for that class. As parameter you better use pointers.
But I recommand you to do that NOT IN REAL CODE. It leeds to dependencies and its problems. It is better to use native data types for interfaces.
Example for your summation:
void time12::summation(int h24, int m24, int s24);
It improves maintainability and stability. And if you do it than see it as as "One way ticket". Never ever. :-O
这篇关于我们可以将一个类的成员变量分配给另一个类的函数定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文