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;
}
| Output |
| Surface: 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;
}
| Output |
| 6.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.8167The 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;
}
| Output |
| 1 2 3 4 3 2 |
C++类与对象实战
本文通过多个示例深入浅出地介绍了C++中的类与对象概念,包括成员函数的定义与实现、构造函数、静态成员变量、运算符重载等核心特性。展示了如何将类的声明与实现分离,以及如何利用this指针操作对象状态。

被折叠的 条评论
为什么被折叠?



