我真的需要通过查看主程序和示例执行输出来理解如何编写类(声明和定义)
I really need help understanding how to write a class (declaration and definition) just by looking at a main program and sample execution output
我看过一个例子,我正试图通过查看主文件和输出来了解如何编写.h和.cpp文件
我有一门课叫Flex:
#include <iostream>
#include "flex.h"
using namespace std;
int main()
{
Flex a, b("Merry"), c("Christmas");
cout << a << ',' << b << ',' << c << endl;
b.cat(a);
cout << b << endl;
b.cat(c);
cout << b << endl;
c.cat(c);
c.cat(c);
cout << c << endl;
return 0;
}
执行输出为:
* *,*Merry*,*Christmas*
*Merry *
*Merry Christmas*
*ChristmasChristmasChristmasChristmas*
然后,声明/定义文件应该如下所示:
#include <iostream>
using namespace std;
class Flex
{
friend ostream& operator<< (ostream& s, const Flex& f);
public:
Flex(); // default constructor
Flex(const char *); // constructor with parameters
~Flex(); // destructor (not specifically required)
void cat(const Flex & f); // cat function -- concatenation
private:
char * str; // variable length string
int size;
};
和
#include <iostream>
#include <cstring>
#include "flex.h"
using namespace std;
ostream& operator<< (ostream& s, const Flex& f)
{
s << '*' << f.str << '*';
return s;
}
Flex::Flex()
{
size = 1; // size doesn't include null char
str = new char[size+1]; // allocate +1 for null char
strcpy(str," ");
}
Flex::Flex(const char * s)
{
size = strlen(s);
str = new char[size+1];
strcpy(str,s);
}
Flex::~Flex()
// not specifically required by the specs, but a good idea to have this
{
delete [] str;
}
void Flex::cat(const Flex & f)
// this function can also be made easier through the use of the
// strcat library function for concatenating strings.
// dyanamic reallocation still required, though.
{
int newsize = size + strlen(f.str);
char * temp = new char[newsize+1]; // allocate with room for ' '
strcpy(temp,str); // copy this string to temp
for (int i = 0; i <= f.size; i++)
temp[size+i] = f.str[i]; // concatenate f.str to temp,
// including ' '
delete [] str; // delete old array
str = temp; // set str to new one
size = newsize; // update size tracker
}
这个问题可能很难解释,但一个人如何看待主程序并立即知道他要写什么课?
我需要为一个涉及统计数据的类做这件事。我还没有一个主程序,但如果我不再使用字符,我们会有什么不同?如何通过查看主文件和输出来表示统计类?
运算符<lt;发送'*' << f.str << '*'
(其中f.str是您的,Flex对象,str值)您有Flex 类的构造函数
Flex::Flex()
{
size = 1; // size doesn't include null char
str = new char[size+1]; // allocate +1 for null char
strcpy(str," ");
}
Flex::Flex(const char * s)
{
size = strlen(s);
str = new char[size+1];
strcpy(str,s);
}
当您初始化像Flex这样的对象时("测试")。您的a.str=="测试"。当Flex为a()时,则a.str=="。函数cat是concatanation 2值。如果b.cat(a),则意味着b.str=b.str+a.str
相关文章:
- 编写代码时C++出现错误:错误 1 错误 C2601:'circle':本地函数定义是非法的
- 为 std::vector 编写自定义插入函数
- 如何编写将展开以定义具有模板参数的对象的宏
- Tbb 库:错误:编写自定义类函数而不是 lambda 表达式时,对函数的调用不匹配
- 如何通过 C 在用户定义的结构中编写双数组并使用 CTYPE 传递给 Python
- 如何编写一个接受如下断言消息的自定义断言函数:assert(false) << "assertio
- 编写具有 2 个通道的自定义 QIODevice
- 如何在不使用 vector::erase() 的情况下编写自定义 Vector 方法来删除元素?
- C++,当函子不是一个选项时,我如何编写带有自定义函数调用的模板化 RAII 包装器?
- 如何使用自定义对象的序列化在 c++ 中编写自定义二进制文件处理程序
- 尝试编写进程内存并获取用户定义的文字运算符未找到
- 如何在不取消定义类型的情况下为 C++ 中的类型编写 getter?
- 在Python中编写和注册自定义TensorFlow OP
- 快捷方式在C 中编写自定义编译命令时
- 如何为数据类型 vector<glm::vec3> (opengl 数据类型) 编写 std::minmax 函数的自定义比较器
- 编写安全包装类以管理用户定义对象的指针
- 致命错误LNK1169:在C++Visual Studio中编写战舰游戏时发现的一个或多个多重定义的符号
- 尝试编写自定义Allocate_Shared分配器并将其变为thread_local时崩溃
- 在C++为矢量编写自定义虚拟分配器时遇到问题
- 如何为 Product* getProductFromID(std::string)编写方法定义;.