小知識點

1.virtualDestructorOfBaseClass

#include<iostream>
using namespace std;

class Basea {
public:
  Basea() {
    cout << "it is Basea" << endl;
  }
  virtual ~Basea() {
    cout << "destruction of Basea" << endl;
  }
  // s聲明爲虛函數的時候才能夠在delete的時候將兩個對象的內存全部釋放,否則派生類的內存沒有
  // 釋放,進而導致內存垃圾
};
class derived:public Basea {
public:
  derived() {
    cout << "it is derived" << endl;
  }
  ~derived() {
    cout << "destruction of derived" << endl;
  }
};
int main() {
  Basea* p = new derived;
  delete p;
  return 0;
}

2.default function

#include<iostream>
using namespace std;

// 此處n個參數沒有默認值就要求輸入的時候至少要有>= n個實參
void func(int a = 0, int b, int c = 0) {
  cout << a << endl;
  cout << b << endl;
  cout << c << endl;
}
int main() {
  func(1);
  return 0;
}

//  可以是void func(int a, int b = 0, int c = 0)
//  但是不可以是void func(int a = 1, int b, int c = 2);
//  因爲但只傳入一個參數的時候,默認是第一個,而此時第二個參數卻沒有默認值,就會出現錯誤,
//  complie error:
//  test.cpp:5:6: error: default argument missing for parameter 2 of ‘void func(int, int, int)
//  void func(int a = 0, int b, int c = 0) {

3.static member function

#include<iostream>
using namespace std;

class test {
private:
  static int count;
  int num;
public:
  test(int a) {
    count++;
    num = a;
  }
  static int getCount();
};
//  後面加上const會報錯,尚未理解原因
// static int getCount() const;  不可以
// compile:
// static.cpp:17:22: error: static member function ‘static int test::getCount()’
// declared with type qualifiers
// int test::getCount() const {


int test::count = 0;
int test::getCount(){
  return count;
}
int main() {
  cout << test::getCount() << endl;  // 注意因爲靜態成員函數屬於類,但並不是單個對象的獨有函數
  //  注意這兩種調用方式
  test t = test(1);
  cout << t.getCount() << endl;
  return 0;
}

4.inheritance

#include<iostream>
using namespace std;

class A {
public:
  A() {
    cout << "A" << endl;
  }
  ~A() {
    cout << "~A" << endl;
  }
};
class B {
public:
  B() {
    cout << "B" << endl;
  }
  ~B() {
    cout << "~B" << endl;
  }
};
class C : public B, public A {
public:
  C() {
    cout << "C" << endl;
  }
  ~C() {
    cout << "~C" << endl;
  }
};
int main() {
  C c;
  return 0;
}
// B
// A
// C
// ~C
// ~A
// ~B

5.inheritance again

//  派生類可以繼承基類的靜態成員函數和靜態數據成員,不可以繼承基類的友元函數
// 派生類可以訪問基類的ptotected成員
#include<iostream>
using namespace std;

class BASE {
public:
  BASE();
  void get_ij();
  static void get() {
    cout << count << endl;
  }
protected:
  int i, j;
  static int count;
private:
  int x_temp;

};
int BASE::count = 0;
class Y:public BASE {
public:
  Y() {
    count++;
  }
  void increment();
private:
  float nmember;
};
BASE::BASE() {
  count++;
  i = j = x_temp = 0;
}
void BASE::get_ij() {
  cout << i << ' ' << j << endl;
}
void Y::increment() {
  i++;
  j++;
}
int main() {
  BASE obj1;
  obj1.get();
  Y obj2;
  obj2.increment();
  obj2.get_ij();
  obj1.get();
  obj2.get();
  obj1.get_ij();
}
// output:
// 1
// 1 1
// 3
// 3
// 0 0


//  因爲友元函數不屬於類的成員函數,所以很明顯的,派生類不能夠繼承類的友元函數
//   error: ‘class Y’ has no member named ‘get_ij’
//   obj2.get_ij();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章