C++学习笔记
序章
C++内容:
- 基本元素
- Objects(对象) & Class(类)
- Inheritance(继承) & Polymorphism(多态)
- Exceptions(异常) & Templates(模板)
- C++ Standard Library
C++阅读推荐
- Thinking in C++, 2nd Edition, Bruce Eckel
- C++ primer, 5th Edition, Stanley Lippman(语法不清楚时可以看)
- The C++ Programmer Language, 4rd Edition, Bjarne Stroustrup (接触C++一段时间后可以看)
- Essential C++, Stanley Lippman (一般般,可以快速过一遍)
- (more) Effective C++, 3rd 2005, (如何高效使用C++)
- Effective Modern C++, 2014
- Inside The C++ Object Model, Stanley Lippman(底层与细节)
- Information: Download books and code(vol.1)
http://www.mindview.net/Books/DownloadSites/
Preparation
one C++ Compiler(gcc/g++, visual c++, clang)
准备好一个c++的编译器即可,不管什么地方都可以写代码,不过应该会更多的使用IDE吧…
那么开始写第一个C++程序吧
程序员的hello world!//hello.cpp
#include <iostream>
int main()
{
std::cout << "hello world!\n";
}
其中 “
C++源文件处理过程
- C++源文件(.cpp / .h)
- 预处理(宏展开,加入头文件)
- 编译(处理语法问题)
- 各个编译单元独立处理
- 任何标识符使用前必须声明
- 声明不会分配空间
- 定义会分配空间,而且可以充当声明
- 链接(*.obj)
- 可执行文件(*.exe)
以上文的“hello.cpp”为例
具体的处理过程如下:
1. 编译 hello.cpp 生成 hello.obj (目标文件)
2. 链接 hello.cpp 生成 hello.exe (可执行文件)
3. 运行hello.exe(或hello)
ODR原则
One Definition Rale
任何标识符必须有定义并且只能定义一次
一条建议:声明放在头文件中,定义放在cpp文件中
IDE
Visual Studio介绍
1. *.sln 解决方案文件 (包含···工程,显示版本)
2. *.vcxproj 工程文件 (包含源文件)
3. *.obj 在 Debug 文件夹下 (product的Debug下)
4. *.exe 在 Solution的Debug下
其他IDE还有很多,找自己喜欢用的就好
那么用IDE写个程序吧
第二个程序
加强版hello
//hello.cpp
#include <iostream>
#include <string.h>
//using namespace std; //表示std的名称空间都开放了,之后就不用写“std::”了
//using std::cout; //表示开放了cout,以后的cout都不用在前面“std::”了
int main()
{
std::string name;
std::cin >> name;
std::string greating = "hello " + name + "~";
std::cout << greating << std::endl;
}
运行一下试试会发生什么吧~