[英]Classes in separate files in C++?
I have copied this out of a book. 我已经从书本上抄下来了。 I'm just not sure what to add in the
main.cpp
source file to make it run though. 我只是不确定要在
main.cpp
源文件中添加什么来使其运行。
I know that class declarations go in the .h
file and implementations go in the .cpp
file. 我知道类声明在
.h
文件中,实现在.cpp
文件中。 What would I need to write in main.cpp
? 我需要在
main.cpp
写什么?
I've tried lots of different things but I'm just getting so many error messages. 我已经尝试了许多不同的方法,但是却收到了很多错误消息。
// cat.h
#ifndef ____2_cat_implementation__Cat__
#define ____2_cat_implementation__Cat__
#include <iostream>
using namespace std;
class Cat
{
public:
Cat (int initialAge);
~Cat();
int GetAge() { return itsAge;}
void SetAge (int age) { itsAge = age;}
void Meow() { cout << "Meow.\n";}
private: int itsAge;
};
#endif /* defined(____2_cat_implementation__Cat__) */
... ...
// cat.cpp
#include <iostream>
#include "Cat.h"
using namespace std;
Cat::Cat(int initialAge)
{
itsAge = initialAge;
}
Cat::~Cat()
{
}
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat who is ";
cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
cout << "Now Frisky is " ;
cout << Frisky.GetAge() << " years old.\n";
return 0;
}
Look at this part again: 再看这部分:
Cat::Cat(int initialAge);
{
itsAge = initialAge;
Cat::~Cat()
You're missing the closing }
for the constructor, as well as an extra ;
您缺少构造函数的结束符
}
和其他内容;
after the function header. 在函数标题之后。
On an unrelated note, don't use global names starting with underscore (like ____2_cat_implementation__Cat__
), those names a reserved by the specification. 无关紧要的是,不要使用下划线开头的全局名称(例如
____2_cat_implementation__Cat__
),这些名称是规范保留的名称。
You have a missing }
and unneccessary ;
您缺少
}
和不必要的;
//----------------------v
Cat::Cat(int initialAge);
{
itsAge = initialAge;
}
//^
What would I need to write in main.cpp
我需要在main.cpp中写什么
Usially, as you have pointed out, the .h
file contains the declarations and the .cpp
file - the definitions. 通常,正如您所指出的,
.h
文件包含声明和.cpp
文件-定义。 Then, the main.cpp
file should contain the main
function (it's not necessary to name the file, containing the main
function main.cpp
. It could be anything. 然后,
main.cpp
文件应包含main
函数(无需命名文件,包含main
函数main.cpp
。可以是任何东西。
So, in your example, you can create a main.cpp
file with the following content: 因此,在您的示例中,您可以创建具有以下内容的
main.cpp
文件:
// include the declarations file
#include "cat.h"
// include the header for cin/cout/etc
#include <iostream>
using namespace std;
int main()
{
Cat Frisky(5);
Frisky.Meow();
cout << "Frisky is a cat who is ";
cout << Frisky.GetAge() << " years old.\n";
Frisky.Meow();
Frisky.SetAge(7);
cout << "Now Frisky is " ;
cout << Frisky.GetAge() << " years old.\n";
return 0;
}
Other notes: 其他说明:
using namespace std;
is bad practice, especially in header files. std::
instead (for example, std::cout
, std::cin
, std::string
, etc) std::
代替(例如, std::cout
, std::cin
, std::string
等) .h
and .cpp
files, don't put half of the implementation in the header and the rest - in the source file. .h
和.cpp
文件,所以请不要将实现的一半放在标头中,而将其余部分放在源文件中。 Put all definitions inside the source files (unless you want to inline
the functions, implemented in the header) inline
函数(在标头中实现)) __
or _
- they are reserved by the standard. __
或_
开头的名称-它们是标准保留的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.