C++入门2
1,第一个 C++ 程序
- 编译方式:
g++ main.cpp
1.1 输出
main.cpp
#include <iostream>
using namespace std;
// 输出
int main()
{
cout << "Hello, World! I am " << 18 << " Today!" << endl;
return 0;
}
1.2 输入
main.cpp
#include <iostream>
using namespace std;
// 输入
int main()
{
int number;
cout << "Enter a decimal number:";
cin >> number;
cout << "The number you entered is " << number << endl;
return 0;
}
2,面向对象
- Objects = Attributes + Services
- Data: the properties or status
- Operations: the functions
2.1 Procedural Languages(过程语言)
- C doesn't support relationship between data and functions.
// 定义 struct
typedef struct point3d {
float x;
float y;
float z;
} Point3d;
// 定义 function
void Point3d_print(const Point3d* pd);
Point3d a;
a.x = 1; a.y = 2; a.z = 3;
Point3d_print(&a);
2.2 C++ 版本
// 类里面既包括数据,又包括 function
class Point3d {
public:
Point3d(float x, float y, float z);
print();
private:
float x;
float y;
float z;
};
Point3d a(1,2,3);
a.print();
2.3 class 和 struct 区别
参考资料
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~