简体   繁体   English

C ++中单独文件中的类?

[英]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. 这是不好的做法,尤其是在头文件中。 Use std:: instead (for example, std::cout , std::cin , std::string , etc) 使用std::代替(例如, std::coutstd::cinstd::string等)
  • as you have .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函数(在标头中实现))
  • avoid using names, starting with __ or _ - they are reserved by the standard. 避免使用以___开头的名称-它们是标准保留的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM