现在的位置: 首页 > 综合 > 正文

解惑1

2018年02月16日 ⁄ 综合 ⁄ 共 1588字 ⁄ 字号 评论关闭

/////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;
class   A   
  {   
  public:   
  A(){   doSth();   }   
  virtual   void   doSth(){   printf("I   am   A");}   
  };
  
class   B : public   A   
  {   
  public:  
  
  virtual   void   doSth(){   printf("I   am   B");}   
  };
int main()
{
    A a;
    B b;
    return 0;
}

//////////////////

I   am   AI   am   A

因为子类B在构造时,A先构造,B随后构造.A构造函数调用doSth时,子类B还不存在,所以只能调用本身的doSth

////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;
class  A
{
public:
 //A () : y(0)  // “y”: 无法通过构造函数初始化静态类数据
 //{}
    //只是声明
 static int x;
 // static int y; // “y”: 无法通过构造函数初始化静态类数据
};
// 定义一次,才有内存,在全局中定义,否则重复定义
int A::x;
int main()
{
 // 若不定义,x没有内存空间,是一个无法解析的外部命令
 cout << A::x << endl;
 //cout << A::y << endl;
}

//规定:在定义静态成员的时候不用static

///////////////////////////////////////////////////////////////////////////

 string流用法

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void main()
{
 string content;
 stringstream prefix;
 int num = 999;
 prefix << "[" << num << "]";
 prefix >> content;
 cout << content;
}

///

[999]

///////////////////////////////////////////////////////////////////////////

// vector模拟二维数组

#include <iostream>
#include <vector>

using namespace std;
void main()
{
int i = 0, j = 0;
vector < vector <int> > Array;
vector < int > line;
for( j = 0; j < 10; j++ )
{
  Array.push_back( line ); // push_back拷贝一份line
  for ( i = 0; i < 9; i++ )
  {
  Array[ j ].push_back( i+j );
  }
}

if(!Array.empty()){    //如果不空则输出,空了就不会输出!
        for(i=0; i<10; ++i){
            for(j=0; j<9; ++j)
                cout<<Array[i][j]<<' ';
            cout<<endl;
        }
    }

}

/////////////////////////////////////////////////////////////////////

抱歉!评论已关闭.