从C到C++要注意的33件事(2)

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"
int main () { vector k; k.x = 4; k.y = 5; cout << "Surface: " << k.surface() << endl; return 0; }
你可以用下面的命令来编译 vector.cpp,可以生成对应的.o文件

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.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;
}
Output
1
2
3
4
3
2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值