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

25 类的变量也可以是常量,就像静态变量一样

using namespace std;
#include <iostream>

class vector
{
public:

   double x;
   double y;
   const static double pi = 3.1415927;

   vector (double a = 0, double b = 0)
   {
      x = a;
      y = b;
   }

   double cylinder_volume ()
   {
      return x * x / 4 * pi * y;
   }
};

int main()
{
   cout << "The value of pi: " << vector::pi << endl << endl;

   vector k (3, 4);

   cout << "Result: " << k.cylinder_volume() << endl;

   return 0;
}
Output
The value of pi: 3.14159

Result: 28.2743

26 一个类可以从其他的类派生过来,这个新的类可以继承原有类的所有成员和方法,并且可以增加新的成员和方法


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);
   }

   double surface()
   {
       return x * y;
   }
};

class trivector: public vector    // trivector is derived from vector
{
public:
   double z;                      // added to x and y from vector

   trivector (double m=0, double n=0, double p=0): vector (m, n)
   {
      z = p;                      // Vector constructor will
   }                              // be called before trivector
// constructor, with parameters
// m and n. trivector (vector a) // What to do if a vector is { // cast to a trivector x = a.x; y = a.y; z = 0; } double module () // define module() for trivector { return sqrt (x*x + y*y + z*z); } double volume () { return this->surface() * z; // or x * y * z } }; int main () { vector a (4, 5); trivector b (1, 2, 3); cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl; cout << "Surface of a: " << a.surface() << endl; cout << "Volume of b: " << b.volume() << endl; cout << "Surface of base of b: " << b.surface() << endl; cout << "Module of a: " << a.module() << endl; cout << "Module of b: " << b.module() << endl; cout << "Module of base of b: " << b.vector::module() << endl; trivector k; k = a; // thanks to trivector(vector) definition
// copy of x and y, k.z = 0 vector j; j = b; // copy of x and y. b.z leaved out vector *r; r = &b; cout << "Surface of r: " << r->surface() << endl; cout << "Module of r: " << r->module() << endl; return 0; }


a (4, 5)    b (1, 2, 3)    *r = b

Surface of a: 20
Volume of b: 6
Surface of base of b: 2
Module of a: 6.40312
Module of b: 3.74166
Module of base of b: 2.23607
Surface of r: 2

Module of r: 2.23607


27  在上面的程序里面,r->module 计算vector的模型,这样做为什么可以是因为r被声明为vector类的指针。但是我们要记住,如果你想程序检查这种对象的指针或者使用相关的方法,那么我们必须把类里面的方法声明为虚函数。

(如果继承的基类里面有至少一个方法被声明为虚函数的话,那么每一个实例都会被加上事实上的4个Byte的指针,当然,64位有可能是8个Byte)。

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;
   }

   virtual double module()
   {
      return sqrt (x*x + y*y);
   }
};

class trivector: public vector
{
public:
   double z;

   trivector (double m = 0, double n = 0, double p = 0)
   {
      x = m;                  // Just for the game,
      y = n;                  // here I do not call the vector
      z = p;                  // constructor and I make the
   }                          // trivector constructor do the
// whole job. Same result. double module () { return sqrt (x*x + y*y + z*z); } }; void test (vector &k) { cout << "Test result: " << k.module() << endl; } int main () { vector a (4, 5); trivector b (1, 2, 3); cout << "a (4, 5) b (1, 2, 3)" << endl << endl; vector *r; r = &a; cout << "module of vector a: " << r->module() << endl; r = &b; cout << "module of trivector b: " << r->module() << endl; test (a); test (b); vector &s = b; cout << "module of trivector b: " << s.module() << endl; return 0; }
Output
a (4, 5)    b (1, 2, 3)

module of vector a: 6.40312
module of trivector b: 3.74166
Test result:          6.40312
Test result:          3.74166
module of trivector b: 3.74166

28 你可能想知道一个类是否可以同时继承多个类, 那么答案是对的

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 surface()
   {
      return fabs (x * y);
   }
};

class number
{
public:

   double z;

   number (double a)
   {
      z = a;
   }

   int is_negative ()
   {
      if (z < 0) return 1;
      else       return 0;
   }
};

class trivector: public vector, public number
{
public:

   trivector(double a=0, double b=0, double c=0): vector(a,b), number(c)
   {
   }           // The trivector constructor calls the vector
// constructor, then the number constructor,
// and in this example does nothing more.
double volume() { return fabs (x * y * z); } }; int main () { trivector a(2, 3, -4); cout << a.volume() << endl; cout << a.surface() << endl; cout << a.is_negative() << endl; return 0; }
Output
24
6
1

29 类的继承可以让你依据基类设计更负责的类,而另外一个用处则是可以让程序员写出一些通用的函数。

当我们写了一个没有任何变量的基类的时候,在你的程序里面是没有办法使用这个类的。也就是说当你想继承这个类的时候,就一定要让成员函数正确的在类里被定义,这样才能合理的继承基类

using namespace std;
#include <iostream>
#include <cmath>

class octopus
{
public:

virtual double module() = 0; // = 0 implies function is not
// defined. This makes instances
// of this class cannot be declared.
}; double biggest_module (octopus &a, octopus &b, octopus &c) { double r = a.module(); if (b.module() > r) r = b.module(); if (c.module() > r) r = c.module(); return r; } class vector: public octopus { public: double x; double y; vector (double a = 0, double b = 0) { x = a; y = b; } double module() { return sqrt (x * x + y * y); } }; class number: public octopus { public: double n; number (double a = 0) { n = a; } double module() { if (n >= 0) return n; else return -n; } }; int main () { vector k (1,2), m (6,7), n (100, 0); number p (5), q (-3), r (-150); cout << biggest_module (k, m, n) << endl; cout << biggest_module (p, q, r) << endl; cout << biggest_module (p, q, n) << endl; return 0; }
Output
100
150
100

30 public:这些变量或者函数能够被外部程序方位

   protect:仅能被类本身或者继承类访问

   private:只能被自己类的方法所访问。

   

using namespace std;
#include <iostream>

class vector
{
protected:

   double x;
   double y;

public:

   void set_x (int n)
{
x = n;
}
   void set_y (int n)
{
y = n;
}
double surface ()
{
double s;
s = x * y;
if (s < 0) s = -s;
return s;
}
};

int main ()
{
vector a;

a.set_x (3);
a.set_y (4);

cout << "The surface of a: " << a.surface() << endl;

return 0;
}
 
 
 
Output
The surface of a: 12
 
31 让我们看看C++是怎么读写文件的
这是写一个文件的 程序
using namespace std;
#include <iostream>
#include <fstream>

int main ()
{
   fstream f;

   f.open("test.txt", ios::out);

   f << "This is a text output to a file." << endl;

   double a = 345;

   f  << "A number: " << a << endl;

   f.close();

   return 0;
}

 
 
 
Content of file test.txt
This is a text output to a file. A number: 345
下面是怎么读文件
using namespace std;
#include <iostream>
#include <fstream>

int main ()
{
   fstream f;
   char c;

   cout << "What's inside the test.txt file" << endl;
   cout << endl;

   f.open("test.txt", ios::in);

   while (! f.eof() )
   {
      f.get(c);                          // Or c = f.get()
      cout << c;
   }

   f.close();

   return 0;

 
32 有时候我们可以像文件一样操作字符数组,这对我们在转换和管理内存数组的时候很有用
using namespace std;
#include <iostream>
#include <strstream>
#include <cstring>
#include <cmath>

int main ()
{
   char a[1024];
   ostrstream b(a, 1024);

   b.seekp(0);                           // Start from first char.
   b << "2 + 2 = " << 2 + 2 << ends;     // ( ends, not endl )
// ends is simply the
// null character '\0' cout << a << endl; double v = 2; strcpy (a, "A sinus: "); b.seekp(strlen (a)); b << "sin (" << v << ") = " << sin(v) << ends; cout << a << endl; return 0; }
Output
2 + 2 = 4
A sinus: sin (2) = 0.909297

 
33 C++可以格式化输出用两个不同的方式,要注意到,width()和setws()只对下一个要输出的对象起作用。
using namespace std;
#include <iostream>
#include <iomanip>

int main ()
{
   int i;

   cout << "A list of numbers:" << endl;
   for (i = 1; i <= 1024; i *= 2)
   {
      cout.width (7);
      cout << i << endl;
   }

   cout << "A table of numbers:" << endl;
   for (i = 0; i <= 4; i++)
   {
      cout << setw(3) << i << setw(5) << i * i * i << endl;
   }

   return 0;
}

  
  
Output
A list of numbers:       1       2       4       8      16      32      64     128     256     512    1024 A table of numbers:   0    0   1    1   2    8   3   27   4   64
以上是一些基本的C++的知识,主要是针对一些C用户转换到C++需要注意的一些点,没什么复杂的。其实翻译这个系列文章的目的也是自己现在正处于这个挑战中,也希望通过这个积累和构造自己的知识体系吧。同时也能和各位同学共勉。
如果同学们在看文章的过程中发现错误和有任何问题,意见,欢迎发表评论。我个人很愿意和大家一起讨论,进步。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值