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 区别
参考资料