IDE 创建 C++ 类方法
🧑💻 User
In this lecture, I want you to know that we have a better ways to create classes through other ideslike Microsoft Visual Studio and basically any other IDE you’re going to be using out there.But Visual Studio Code is special in that it really is not an IDE, it is a glorified text editor andwe have the ability to do cool stuff with C plus plus because of the extension that we have installed.But that extension unfortunately isn’t giving us access to an easier way to create classes directly.But we’re going to show you that you can do that in this lecture by using other ides.So, for example, here I have a code light up and you see here this is the code light ide, and I amgoing to create a junk project just to show you that you can play with this.We’re going to create a project and call it Sandbox.Why not?It’s going to live somewhere on your drive and it’s going to be living in some workspace, but it’sgoing to be a C plus plus 20 project.It’s going to be a console project.Make sure you are using these settings here and we’re going to hit, okay, This is going to createa project.It’s going to have the main CPP file that we have grown to know and love, and we’re going to take outwhat we don’t need and we’re going to just say Stdcout hello.And if we run this by coming to build and run, we should have a window pop up saying Hello here.So this is our program.Now we want to see how we can create a class from this IDE.All you have to do is come to the source folder here and say, New class, you right click.You’re going to get a menu here and it’s going to say New class.You see that you have a nice window in which you can type information about your classes.For example, our class is going to be called cylinder.Let’s put the name here like we did in the last lecture.And you see we have a lot of information here, but we don’t really care about all these guys.So you can put in a few settings.For example, we can use H hp P instead of dot h as our header extension, but I like dot H so we canuse that and we’re going to use lowercase file names because that’s really what I like.If you don’t check this, the header and the CPP file are going to have names that start with uppercaseletters.If you want that, you can uncheck that.But I really like lowercase file names, so that’s what I’m going to choose.We don’t have enough information to understand all of the other options here, but we’re going to useour class as is.For now.Let’s hit okay and see what we get from the IDE.You see, we have a header file which is going to put in our include guard.You already know this and we have a class definition in here, which is pretty good.You see, we have a few things inside.We have the public keyword here and we have a constructor which is not going to take anything.And we have another method from the class that we don’t know about yet.So for now, I’m going to take this out.And if you look down, we have cylinder dot CPP.It’s going to include our cylinder header file and we have the implementations here.Okay.So from this point on, you can start from here and really modify things.So this is helpful if you ask some people, but I personally usually take all this out anyway becauseI really like to have control on how I create my classes.But you have to know that some ides are going to give you this option and create the class for you andgive you a blueprint or a starting point where you can start doing your own things.The only benefit I find with this is that this is going to put together include guards for you becauseI really hate doing this.So the ID is going to take care of this.But other than that, I usually go and type the class definition myself and split things up howeverI want.This is how you can do this.Using the code light ID, it is also possible to do the same in the Microsoft Visual Studio.IDE.Here I have a sample project open.You see we have the main function here and if we run this by going to debug and start without debugging,we should see a window saying Hello world in a minute.Let’s wait.Okay.The build is good, you see.Hello world here.But we want to create a class.What you do.If you want to create a class, you come to the project name here and it is not the solution name inVisual Studio, it is the project name that I have selected here, which is highlighted in blue.You right click, you come to add and it’s going to give you a few options.We’re going to choose class here and it’s going to give us a window, which is strikingly similar towhat we had with code light.In a minute, we’re going to put in our class name cylinder and you see it’s going to give us the nameshere.I don’t like this uppercase names, so I am going to change this to lowercase cylinder for.The names of my files.We’re not going to put any other thing here because we really are using the basics here.So once we have the name of the class, the name for the files in, we’re going to hit.Okay.And the ID is going to create these classes here.You see, it’s not using include guards.It’s using what we call pragma once here.And this is basically to be like letting the compiler generate the include guards for you.So this is another syntax you can use for this.We have the header which is going to have our class definition.We also have a CPP file which is going to have the implementation.And if we want we can go from Visual Studio code and copy the files or we can set up our classes howeverwe want.Once we have the class, we’re going to head over in the main function and include the class.For example, we can say include cylinder and this is going to be available and usable in the main functionhere and you can go on and use your class however you want.This is the facility I wanted you to see if you happen to be not using Visual Studio code, use this.At least it is going to generate the include guards and it’s going to give you a good starting point.It’s going to create these files for you so that you don’t have to create them yourself.It’s useful if you have this feature, make sure you are using it.This is really all I had to share in this lecture.I hope you found it interesting.We are going to stop here in this lecture.In the next one we’re going to show you how you can manage class objects through pointers.Go ahead and finish up here and meet me there.
🤖 Assistant
好的,同学你好!很高兴能成为你的C++启蒙老师。今天我们要学习的是如何利用集成开发环境(IDE [Integrated Development Environment])来更便捷地创建C++类(class [class])。这会比我们之前在Visual Studio Code这样的文本编辑器(text editor [text editor])中手动创建文件要方便一些。
1. 标题与概要
标题: IDE助力:轻松创建C++类文件
概要: 本节课我们将学习如何在像CodeLite(或Code::Blocks)和Microsoft Visual Studio这样的集成开发环境 (IDE [Integrated Development Environment]) 中使用向导功能来创建C++类 (class [class])。这可以帮助我们自动生成头文件 (header file [header file]) 和源文件 (source file [source file]) 的基本结构,特别是烦人的包含保护 (include guard [include guard]),从而提高我们的开发效率。
2. 详细解释
在之前的学习中,你可能已经习惯了在Visual Studio Code (VS Code) 这样的代码编辑器中手动创建 .h
或 .hpp
头文件和 .cpp
源文件来定义和实现你的类。VS Code 本身是一个非常强大的文本编辑器,通过安装C++扩展 (extension [extension]),它具备了很多IDE的功能。但对于创建类这种结构化的任务,许多成熟的IDE提供了更直接的图形化界面支持。
让我们看看两种常见的IDE是如何帮助我们创建类的:
A. 使用 CodeLite (或类似的IDE,如 Code::Blocks)
CodeLite是一个轻量级的C++ IDE。它的操作步骤通常如下:
创建项目 (Project):
- 首先,你需要一个项目。比如,我们可以创建一个名为 “Sandbox” 的C++20控制台应用程序项目。IDE会为你生成一个包含
main.cpp
的基本项目结构。 - 通常
main.cpp
会有一个简单的 “Hello World” 示例,你可以修改它或清空它。
- 首先,你需要一个项目。比如,我们可以创建一个名为 “Sandbox” 的C++20控制台应用程序项目。IDE会为你生成一个包含
添加新类:
- 在项目文件浏览器中,找到通常名为 “src” 或 “Sources” 的文件夹(存放源代码的地方)。
- 右键点击这个文件夹,在弹出的上下文菜单中选择类似 “New Class…” (新建类) 的选项。
类信息配置窗口:
- 这时会弹出一个窗口,让你填写新类的信息。
- 类名 (Class Name): 输入你想要的类名,例如 “Cylinder”。
- 文件名 (File Names): IDE通常会根据类名自动生成对应的头文件名 (如
Cylinder.h
或cylinder.h
) 和源文件名 (如Cylinder.cpp
或cylinder.cpp
)。 - 头文件扩展名 (Header Extension): 你可以选择使用
.h
还是.hpp
。教程中提到了作者喜欢.h
。 - 小写文件名 (Lowercase Filenames): 很多IDE允许你选择是否使用全小写的文件名。教程中作者勾选了这个选项,所以文件名会是
cylinder.h
和cylinder.cpp
。如果不勾选,可能会是Cylinder.h
和Cylinder.cpp
。 - 其他选项: 可能还会有很多其他高级选项,比如是否生成虚析构函数、是否将类添加到某个命名空间等。对于初学者,我们暂时不用关心这些。
- 这时会弹出一个窗口,让你填写新类的信息。
生成的文件:
- 点击 “OK” 或 “Create” 后,IDE会自动为你创建两个文件:
cylinder.h
(头文件 [header file]):- 包含保护 (Include Guard [include guard]): 这是非常重要的一点!IDE会自动为你添加类似下面的代码:这个结构确保了即使这个头文件被多次包含到同一个编译单元中,其内容也只会被编译器处理一次,防止了重复定义错误。手动写这个很容易出错或忘记。cpp
1
2
3
4
// ... class definition ... - 类声明 (Class Declaration [declaration]): IDE会为你生成一个基本的类声明结构,例如:它会包含一个公共的 (public) 默认构造函数 (constructor [constructor])。有时IDE还会自动添加一个析构函数 (destructor [destructor]) 或其他占位符方法。cpp
1
2
3
4
5
6
7class Cylinder {
public:
Cylinder(); // 默认构造函数 (constructor [constructor])
// IDE可能还会添加一个析构函数 ~Cylinder();
// 以及其他示例方法
// private: // IDE也可能添加private部分
};
- 包含保护 (Include Guard [include guard]): 这是非常重要的一点!IDE会自动为你添加类似下面的代码:
cylinder.cpp
(源文件 [source file]):- 包含头文件: 文件开头会自动
#include "cylinder.h"
。 - 类实现 (Class Implementation [definition/implementation]): IDE会为在头文件中声明的构造函数等成员函数生成基本的实现框架,例如:cpp
1
2
3
4
5
6
7
Cylinder::Cylinder() {
// 构造函数的实现代码
}
// 如果有析构函数或其他方法,也会有它们的框架
- 包含头文件: 文件开头会自动
- 点击 “OK” 或 “Create” 后,IDE会自动为你创建两个文件:
后续工作:
- 虽然IDE生成了这些框架,但具体类的成员变量 (member variables) 和成员函数 (member functions) 的逻辑还是需要你自己去填充和修改。
- 作者提到,他个人通常会删除大部分IDE生成的占位符代码,只保留包含保护,因为他喜欢自己完全掌控类的创建过程。但对于初学者来说,IDE提供的起点是非常有用的。
B. 使用 Microsoft Visual Studio
Visual Studio是Windows平台上非常流行的功能强大的IDE。
创建项目 (Project):
- 同样,你需要一个C++项目。例如,一个控制台应用程序。
添加新类:
- 在 “解决方案资源管理器” (Solution Explorer) 窗口中,找到你的 项目名称 (Project Name),注意,不是解决方案名称 (Solution Name)。
- 右键点击 项目名称,选择 “添加 (Add)” -> “类 (Class)…”。
添加类窗口:
- 会弹出一个窗口,与CodeLite的类似。
- 类名 (Class Name): 输入 “Cylinder”。
- 文件名 (.h file / .cpp file): Visual Studio默认会使用大写开头的类名作为文件名,例如
Cylinder.h
和Cylinder.cpp
。教程中作者手动将它们改成了小写的cylinder.h
和cylinder.cpp
。 - 其他选项: 同样有很多高级选项,初学时保持默认即可。
- 会弹出一个窗口,与CodeLite的类似。
生成的文件:
- 点击 “确定 (OK)” 后,Visual Studio也会创建
.h
和.cpp
文件。cylinder.h
(头文件 [header file]):#pragma once
: Visual Studio默认情况下更倾向于使用#pragma once
而不是传统的包含保护。cpp1
2
// ... class definition ...#pragma once
是一个非标准的但被广泛支持的编译器指令,作用与包含保护相同,都是防止头文件被多次包含。它更简洁,但理论上可移植性不如传统包含保护。不过在主流编译器上基本都支持。- 类声明 (Class Declaration [declaration]): 同样会生成基本的类结构,包括一个默认构造函数,有时也会有析构函数。cpp
1
2
3
4
5class Cylinder {
public:
Cylinder();
~Cylinder(); // Visual Studio 通常会自动添加析构函数
};
cylinder.cpp
(源文件 [source file]):- 会自动
#include "cylinder.h"
(或者如果你用了预编译头,可能是#include "pch.h"
然后是"cylinder.h"
)。 - 会为构造函数和析构函数生成空的实现。cpp
1
2
3
4
5
6
7
Cylinder::Cylinder() {
}
Cylinder::~Cylinder() {
}
- 会自动
- 点击 “确定 (OK)” 后,Visual Studio也会创建
使用类:
- 创建完类之后,你就可以在你的
main.cpp
(或其他需要使用该类的文件中) 通过#include "cylinder.h"
来包含它,然后创建和使用Cylinder
类的对象了。
- 创建完类之后,你就可以在你的
总结IDE创建类的好处:
- 自动化文件创建: 不用手动创建
.h
和.cpp
文件。 - 包含保护/
#pragma once
: 自动处理,减少因忘记或写错包含保护而导致的编译错误。 - 基本结构: 提供了一个类的基本骨架,包括构造函数等,可以作为开发的起点。
- 减少输入错误: 对于类名和文件名的一致性,IDE可以帮助保证。
虽然这些IDE功能很方便,但理解它们背后做了什么(即手动创建类时你需要做的事情)仍然非常重要。这样,无论你使用什么工具,都能游刃有余。
3. 代码示例
假设我们使用IDE(以Visual Studio风格为例,使用 #pragma once
)创建了一个名为 Box
的类。
box.h
(由IDE生成并稍作修改)
1 |
|
box.cpp
(由IDE生成并填充实现)
1 |
|
main.cpp
(我们自己编写,使用Box类)
1 |
|
这个例子展示了IDE如何帮助我们搭建框架,然后我们再往里面填充具体的逻辑。
4. QA闪卡 (QA Flash Cards)
问: 什么是 IDE (集成开发环境 [Integrated Development Environment])?它与文本编辑器 (text editor [text editor]) 的主要区别是什么?
答: IDE集成了代码编辑器、编译器、调试器等多种工具,提供了一站式的开发体验。文本编辑器主要用于编辑文本,通过插件/扩展 (extension [extension]) 可以增强功能,但通常不具备IDE那样高度集成的特性。IDE通常有更强的项目管理和代码生成辅助功能。问: 为什么在C++头文件 (header file [header file]) 中使用包含保护 (include guard [include guard]) 或
#pragma once
?
答: 为了防止同一个头文件的内容在一次编译中被多次包含和处理,这可能导致重复定义等编译错误。问: IDE 通常会为新类 (class [class]) 自动生成哪些文件?它们各自的作用是什么?
答: 通常会生成一个头文件 (.h
或.hpp
) 和一个源文件 (.cpp
)。头文件用于存放类的声明 (declaration [declaration]) (包括成员变量和成员函数的声明)以及包含保护;源文件用于存放类成员函数的具体实现 (definition/implementation [definition/implementation])。问: 在 Visual Studio 中,我们应该右键单击“解决方案 (Solution)”还是“项目 (Project)”来添加新类?
答: 应该右键单击“项目 (Project)”名称。
5. 常见误解或易犯错误
- 忘记将
.cpp
文件添加到项目中: 如果你手动创建.cpp
文件而不是通过IDE向导,有时会忘记将其添加到项目的编译列表中,导致链接错误(通常是 “undefined reference” 或 “LNK2019”)。IDE向导通常会自动处理这个问题。 - 混淆
#include "file.h"
和#include <file>
: 使用引号 (""
) 通常告诉编译器首先在当前项目目录(或用户指定的包含目录)中查找头文件,而尖括号 (<>
) 通常用于包含标准库头文件或编译器预设路径下的文件。当包含自己创建的类头文件时,应使用引号。 - 只修改头文件: 在头文件中添加或修改了成员函数声明后,忘记在对应的
.cpp
文件中提供或更新其实现,会导致链接错误。 - 过度依赖IDE生成: 虽然IDE可以生成骨架,但重要的是理解每一行代码的含义,而不是盲目接受。例如,IDE生成的默认构造函数 (constructor [constructor]) 可能不符合你的需求。
- Visual Studio 中“解决方案”与“项目”的困惑: 初学者可能不清楚一个解决方案 (Solution) 可以包含多个项目 (Project),错误地在解决方案层面尝试添加类。类是属于特定项目的。
- 对包含保护或
#pragma once
的作用不理解: 只是看到IDE生成了它们,但不明白为什么需要它们,以及它们是如何工作的。
6. 编码练习
现在,轮到你来尝试了!我们将创建一个简单的 Pen
(钢笔) 类。请你补全下面的代码。
目标:
- 创建一个
pen.h
文件。 - 在
pen.h
中定义Pen
类,它应该有:- 一个私有成员变量
ink_color_
(类型为std::string
) 来存储墨水颜色。 - 一个公有构造函数,接收一个
std::string
参数来初始化墨水颜色。 - 一个公有成员函数
write(const std::string& message)
,它会在控制台打印出类似 “[颜色] pen writes: 消息内容” 的信息。
- 一个私有成员变量
- 创建一个
pen.cpp
文件。 - 在
pen.cpp
中实现Pen
类的构造函数和write
方法。 - 下面的
main.cpp
已经为你准备好了,你需要确保你的Pen
类可以使它正确编译和运行。
main.cpp
(提供给你,无需修改)
1 |
|
请你填写 pen.h
和 pen.cpp
的内容:
pen.h
1 | // TODO: 添加包含保护 (include guard 或 #pragma once) |
pen.cpp
1 | // TODO: 包含 "pen.h" |
请你思考并尝试填写这些 TODO
部分。完成后,将你的 pen.h
和 pen.cpp
代码发给我,我会帮你检查。加油!