从C到C++要注意的33件事(2)
来源:互联网 发布:单片机液晶数字钟论文 编辑:程序博客网 时间:2024/06/17 05:46
20 如果你不想用inline来声明一个方法,或者你指向在类的定义里面包含最少的内容(或者你想用.hpp和.cpp来隔离源代码和声明),那么你只需要在类定义里面声明一下方法,然后在该类下面实现它就可以了。
using namespace std;#include <iostream>class vector{public: double x; double y; double surface(); // The ; and no {} show it is a prototype};double vector::surface(){ double s = 0; for (double i = 0; i < x; i++) { s = s + y; } return s;}int main (){ vector k; k.x = 4; k.y = 5; cout << "Surface: " << k.surface() << endl; return 0;}
OutputSurface: 20对于一个刚开始接触C++的人来说,如果你想用头文件和源文件来隔离代码,那么你可以参考下面的例子:
A header file vector.h:
class vector{public: double x; double y; double surface();};
A source file vector.cpp:
using namespace std;#include "vector.h"
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
And another source file main.cpp:
using namespace std;#include <iostream>#include "vector.h"你可以用下面的命令来编译vector.cpp,可以生成对应的.o文件
int main (){ vector k; k.x = 4; k.y = 5; cout << "Surface: " << k.surface() << endl; return 0;}
g++ -c vector.cpp
每次如果你修改main.cpp文件,你可以把它编译成一个可执行文件,我们可以起名为test:
g++ main.cpp vector.o -o test
然后我们可以执行这个可执行文件
./test
这样做有以下好处:
1.vector.cpp只需要编译一次,在大型工程中这样节约很多时间。
2.你可以把vector.h文件和.o文件给其他用户,这样他们可以使用你的.o,但是不能修改你的代码
21 当一个方法被一个实例应用的时候,这个方法可以使用这个实例的变量,并且修改,或者运算。但是有些时候,我们还是需要知道实例的地址,那么this这个关键字就派上用场了
using namespace std;#include <iostream>#include <cmath>class vector{public: double x; double y; vector (double a = 0, double b = 0) { x = a; y = b; } double module() { return sqrt (x * x + y * y); } void set_length (double a = 1) { double length; length = this->module(); x = x / length * a; y = y / length * a; }};int main (){ vector c (3, 5); cout << "The module of vector c: " << c.module() << endl; c.set_length(2); // Transforms c in a vector of size 2. cout << "The module of vector c: " << c.module() << endl; c.set_length(); // Transforms b in an unitary vector. cout << "The module of vector c: " << c.module() << endl; return 0;}
The module of vector c: 5.83095
The module of vector c: 2
The module of vector c: 1
22.在C++ 中我们也可以声明一个对象的数组
using namespace std;#include <iostream>#include <cmath>class vector{public: double x; double y; vector (double a = 0, double b = 0) { x = a; y = b; } double module () { return sqrt (x * x + y * y); }};int main (){ vector s [1000]; vector t[3] = {vector(4, 5), vector(5, 5), vector(2, 4)}; s[23] = t[2]; cout << t[0].module() << endl; return 0;}Output6.40312
23.下面是一个完整的类的实例
using namespace std;#include <iostream>#include <cmath>class vector{public: double x; double y; vector (double = 0, double = 0); vector operator + (vector); vector operator - (vector); vector operator - (); vector operator * (double a); double module(); void set_length (double = 1);};vector::vector (double a, double b){ x = a; y = b;}vector vector::operator + (vector a){ return vector (x + a.x, y + a.y);}vector vector::operator - (vector a){ return vector (x - a.x, y - a.y);}vector vector::operator - (){ return vector (-x, -y);}vector vector::operator * (double a){ return vector (x * a, y * a);}double vector::module(){ return sqrt (x * x + y * y);}void vector::set_length (double a){ double length = this->module(); x = x / length * a; y = y / length * a;}ostream& operator << (ostream& o, vector a){ o << "(" << a.x << ", " << a.y << ")"; return o;}int main (){ vector a; vector b; vector c (3, 5); a = c * 3; a = b + c; c = b - c + a + (b - a) * 7; c = -c; cout << "The module of vector c: " << c.module() << endl; cout << "The content of vector a: " << a << endl; cout << "The opposite of vector a: " << -a << endl; c.set_length(2); // Transforms c in a vector of size 2. a = vector (56, -3); b = vector (7, c.y); b.set_length(); // Transforms b in an unitary vector. cout << "The content of vector b: " << b << endl; double k; k = vector(1, 1).module(); // k will contain 1.4142. cout << "k contains: " << k << endl; return 0;}The module of vector c: 40.8167
The content of vector a: (3, 5)
The opposite of vector a: (-3, -5)
The content of vector b: (0.971275, 0.23796)
k contains: 1.41421
我们也可以定义一个函数来让两个vector类相加,不论这个函数是否在类里面。要注意,这不是一个方法,就是一个函数。
vector operator + (vector a, vector b){ return vector (a.x + b.x, a.y + b.y);}
24,如果变量在类里面被定义为static,那么它们会被所有的类的实例共享。
using namespace std;#include <iostream>class vector{public: double x; double y; static int count; vector (double a = 0, double b = 0) { x = a; y = b; count++; } ~vector() { count--; }};int vector::count = 0;int main (){ cout << "Number of vectors:" << endl; vector a; cout << vector::count << endl; vector b; cout << vector::count << endl; vector *r, *u; r = new vector; cout << vector::count << endl; u = new vector; cout << a.count << endl; delete r; cout << vector::count << endl; delete u; cout << b.count << endl; return 0;}Output1
2
3
4
3
2
0 0
- 从C到C++要注意的33件事(2)
- 从C到C++要注意的33件事(3)
- 从C到C++要注意的33件事(1)
- 从C到Java,需要注意的内容
- 项目心得(1)--在C#.net下文件路径要注意的问题
- 从C语言过渡到C++应该注意的一些问题
- 从C语言到C++(一)
- 从C到C++(二)
- 注释转换(从c到c++)
- 2-从C到C++
- 从C到C++(上)
- 从C到C++(下)
- 【C++】从c到c++
- 从c到VF
- 从C到C++
- 从C到Perl
- 从C到C#
- 从C到C++
- DevExpress.XtraGrid常用设置
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期末考试
- Java内存分配---堆与栈
- 扫 雷 小 游 戏
- 编译原理_PL/0_词法分析
- 从C到C++要注意的33件事(2)
- 网易面试题之小易是一个数论爱好者,并且对于一个数的奇数约数十分感兴趣。一天小易遇到这样一个问题: 定义函数f(x)为x最大的奇数约数,x为正整数。 例如:f(44) = 11. 现在给出一个N,需要求
- Xshell使用技巧总结
- Java EE服务端学习(1)
- docs - marahon-lb virtual host小实验
- MySql 5.7安装
- spi 驱动一:spi基本结构和spidev文件系统
- 居中
- 罗马数字转换成整数(注意罗马数字的大小表示问题)