用C ++创建一个类 [英] create a class in C++
问题描述
我想用以下属性在C ++中创建类:
1-类的成员是长度为n的字符串,每个字符可以获取0或1
2-描述没有任何参数的构造函数
3-描述接收两个类的成员并返回OR操作的类的函数成员
4-描述接收两个类的成员并返回AND操作的类的函数成员
我写这段代码请说出我的问题谢谢:
I want create class in C++ with these properties:
1- members of class are strings of length n that each character can get 0 or 1
2- describe a constructor without any parameters
3- describe function member of class that receive two members of class and returns OR operation
4- describe function member of class that receive two members of class and returns AND operation
I write this code please say my problem thanks:
class CBoolOperate{
public:
CBoolOperate ()//default constructor
{
a[5]=b[5]={0};
};
~ CBoolOperate (){};//default destructor
void get()
{
cout<<"please enter first 5 binary digit\n";
for(int i=0;i<5;i++)
cin>>a[i];
cout<<"please enter second 5 binary digit\n";
for(int i=0;i<5;i++)
cin>>b[i];
}
void union(bool a[], bool b[])//A member function
{
bool c[5];
for(int i=0;i<5;i++)
{
if (a[i]==1 || b[i]==1)c[i]=1;
else c[i]=0;
cout << c[i];
}
cout<<"\n";
}
void and(bool a[], bool b[])//A member function
{
bool c[5];
for(int i=0;i<5;i++)
{
if (a[i]==1 && b[i]==1)c[i]=1;
else c[i]=0;
cout << c[i];
}
cout<<"\n";
}
bool a[5], b[5]; //Member variables
};
int main()
{
CBoolOperate r;
r.get;
r.union;
r.and;
getch();
return 0;
}
推荐答案
r.get;
r.union;
r.and;
您不在这里调用函数.
You''re not calling the functions here.
r.get();
r.union(argument1, argument2);
r.and(argument1, argument2);
但是,据我了解,您的类已经包含矢量参数a和b.因此,您可以将它们从参数列表中完全跳过.
But your class already contains the vector arguments, a and b, if I understood. So you could skip them from the argument list altogether.
void union()
{
...
}
void and()
{
...
}
真是一团糟.
It''s a bit of a mess as it is.
这篇关于用C ++创建一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!