1. 程式人生 > >從C到C++要注意的33件事(2)

從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