【发布时间】:2020-10-04 12:12:42
【问题描述】:
#include <iostream>
using namespace std;
class MyClass
{
private :
char str[848];
public :
MyClass()
{
}
MyClass(char a[])
{
str[848] = a[848];
}
MyClass operator () (char a[])
{
str[848] = a[848];
}
void myFunction(MyClass m)
{
}
void display()
{
cout << str[848];
}
};
int main()
{
MyClass m1; //MyClass has just one data member i.e. character array named str of size X
//where X is a constant integer and have value equal to your last 3 digit of arid number
MyClass m2("COVID-19") , m3("Mid2020");
m2.display(); //will display COVID-19
cout<<endl;
m2.myFunction(m3);
m2.display(); //now it will display Mid2020
cout<<endl;
m3.display(); //now it will display COVID-19
//if your array size is even then you will add myEvenFn() in class with empty body else add myOddFn()
return 0;
}
我不能使用string,因为我被告知不要使用,因此,我需要知道如何使它显示所需的输出
【问题讨论】:
-
str[848] = a[848];行没有达到您的意思。 -
是的,这就是为什么我想知道在这里做什么
-
你需要明确任务是什么。即使您必须避免使用
std::string,您也可以将字符串存储为char*或char[],并且每个选项都有其优缺点。更不用说将其存储为std::unique_ptr<char>、std::shared_ptr<char>或std::array<char, 848>... -
为什么是 848?您是否需要精确的 847 个字符?您可能在这里的每种情况下都想要
std::string。如果您出于某种原因需要避免使用std::string,请分配一个您实际需要的大小的缓冲区,而不是一些看似随机的长度。 -
指定我们应该使用长度作为我们的最后3位数字或卷号
标签: c++ arrays constructor