1. 程式人生 > >5.3-day03-C++建構函式/this指標/解構函式

5.3-day03-C++建構函式/this指標/解構函式

四、 5.建構函式 class 類名 {   ...   類名 (形參表) {     建構函式體;   } }; 當一個物件被建立時,建構函式會自動被執行,其引數來自構造實參。 int i = 10; int i (10); 6.建構函式可以通過構造引數實現過載 7.如果一個類沒有定義任何建構函式,那麼系統就會預設地為其提供一個無參建構函式,該建構函式對於基本型別的成員變數不做初始化,對於類型別的成員變數,呼叫其相應型別的無參建構函式初始化。 8.物件的建立過程 分配記憶體->呼叫建構函式           ->呼叫類型別成員的建構函式->建構函式的程式碼 9.初始化表 class 類名 {   類名(...) : 初始化表 {     建構函式體;   } }; const int x = 100; x = 100; int& a = b; a = b; 1)如果類中含有常量或引用型的成員變數,必須通過初始化表對其初始化。 2)成員變數的初始化順序僅於其被宣告的順序有關,而與初始化表的順序無關。 class A { public:    A (char* psz) : m_str (psz),       m_len (strlen (psz)) private:   size_t m_len;   string m_str; }; 練習:實現一個Clock類支援兩種工作模式,計時器和電子鐘。 00:01:00 14:05:37 #include <iomanip> cout << setw (10) << setfill ('0') << 12; 0000000012 Clock   時、分、秒   走 - 顯示、滴答 五、this指標 1.一般而言,在類的建構函式或成員函式中,關鍵字this表示一個指標,對於建構函式而言,this指向正在被構造的物件,對於成員函式而言,this指向呼叫該函式的物件。 2.this指標的用途 1)在類的內部區分成員變數。 2)在成員函式中返回呼叫物件自身。 3)在成員函式內部通過引數向外界傳遞呼叫物件自身,以實現物件間互動。 老 -問題-> 學 師 <-答案- 生 class A {   B& m_b; }; class B {   A& m_a; }; sizeof (A) ? class C {   C m_c; }; 六、常函式與常物件 1.如果在一個類的成員函式的引數表後面加上const關鍵字,那麼這個成員函式就被稱為常函式,常函式的this指標是一個常指標。在常函式內部無法修改成員變數,除非該變數具有mutable屬性。而且在常函式內部也無法呼叫非常函式。 2.常物件:擁有const屬性的物件,物件引用或指標。 常物件只能呼叫常函式。 同型的常函式和非常函式可以構成過載關係。常物件呼叫常版本,非常物件呼叫非常版本。如果沒有非常版本,非常物件也可以呼叫常版本。 const XXX 函式名 (const YYY yyy) const {   ... } 七、解構函式 class 類名 {   ~類名 (void) {      解構函式體;   } }; 當一個物件被銷燬時自動執行解構函式。 區域性物件離開作用域時被銷燬,堆物件被delete時被銷燬。 如果一個類沒有定義任何解構函式,那麼系統會提供一個預設解構函式。預設解構函式對基本型別的成員變數什麼也不幹,對類型別的成員變數,呼叫相應型別的解構函式。 一般情況下,在解構函式中釋放各種動態分配的資源。 構造:基類->成員->子類 析構:子類->成員->基類
1.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. void foo (const int& a) {
  4. cout << a << endl;
  5. }
  6. int main (void) {
  7. char c = 'A';
  8. foo (static_cast<int> (c));
  9. return 0;
  10. }
 靜態型別轉換:
 static_cast <目標型別>(源型別變數)

argthis.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. class Student;
  4. class Teacher {
  5. public:
  6. void educate (Student* s);
  7. void reply (const string& answer) {
  8. m_answer
    = answer;
  9. }
  10. private:
  11. string m_answer;
  12. };
  13. class Student {
  14. public:
  15. void ask (const string& question, Teacher* t) {
  16. cout << "問題:" << question << endl;
  17. t->reply ("不知道。");
  18. }
  19. };
  20. void Teacher::educate (Student* s)
    {
  21. s->ask ("什麼是this指標?", this);
  22. cout << "答案:" << m_answer << endl;
  23. }
  24. int main (void) {
  25. Teacher t;
  26. Student s;
  27. t.educate (&s);
  28. return 0;
  29. }
 
  1. void ask (const string& question, Teacher* t) {
  2. cout << "問題:" << question << endl;
  3. t->reply ("不知道。");

void Teacher::educate (Student* s) { s->ask ("什麼是this指標?", this); cout << "答案:" << m_answer << endl;

clock.cpp
    
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. class Clock {
  5. public:
  6. Clock (bool timer = true) :
  7. m_hour (0), m_min (0), m_sec (0) {
  8. if (! timer) {
  9. time_t t = time (NULL);
  10. tm* local = localtime (&t);
  11. m_hour = local->tm_hour;
  12. m_min = local->tm_min;
  13. m_sec = local->tm_sec;
  14. }
  15. }
  16. void run (void) {
  17. for (;;) {
  18. show ();
  19. tick ();
  20. }
  21. }
  22. private:
  23. void show (void) {
  24. cout << '\r' << setfill ('0')
  25. << setw (2) << m_hour << ':'
  26. << setw (2) << m_min << ':'
  27. << setw (2) << m_sec << flush;
  28. // printf ("\r%02d:%02d:%02d", m_hour,
  29. // m_min, m_sec);
  30. }
  31. void tick (void) {
  32. sleep (1);
  33. if (++m_sec == 60) {
  34. m_sec = 0;
  35. if (++m_min == 60) {
  36. m_min = 0;
  37. if (++m_hour == 24)
  38. m_hour = 0;
  39. }
  40. }
  41. }
  42. int m_hour;
  43. int m_min;
  44. int m
  45. };
  46. int main (void) {
  47. Clock clock (false);
  48. clock.run ();
  49. return 0;
  50. }
  1. << setw (2) << m_hour << ':'
  2. << setw (2) << m_min << ':'
  3. << setw (2) << m_sec << flush;
setw 設定預寬; m_hour 指定填充字元;  cout << ......<< endl;      ‘\n’  cout << ......<< flush;   "把緩衝區裡的東西衝刷到螢幕上"
 
constfunc.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. class A {
  4. public:
  5. // void bar (void) {
  6. // cout << "非常bar" << endl;
  7. // }
  8. void bar (void) const {
  9. cout << "常bar" << endl;
  10. }
  11. // void XXXbarYYY (A* this) {}
  12. void foo (void) const {
  13. // m_i = 100;
  14. const_cast<A*>(this)->m_i = 100;
  15. }
  16. void print (void) const {
  17. cout << m_i << endl;
  18. }
  19. // _ZNK1A3fooEv (const A* this) {
  20. // const_cast<A*>(this)->m_i = 100;
  21. // }
  22. int m_i;
  23. };
  24. void func (void) /*const*/ {}
  25. int main (void) {
  26. A a;
  27. a.foo ();
  28. a.print ();
  29. const A& r = a;
  30. r.bar ();
  31. // XXXbarYYY (&r); // const A*
  32. a.bar ();
  33. // XXXbarYYY (&a); // A*
  34. return 0;
  35. }
 
init.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. int g_data = 1000;
  4. class A {
  5. public:
  6. A (void) : m_c (100), m_r (g_data) {
  7. // m_c = 100;
  8. /// m_r = g_data;
  9. }
  10. void print (void) {
  11. cout <initc << ' ' << m_r << endl;
  12. }
  13. private:
  14. const int m_c;
  15. int& m_r;
  16. };
  17. int main (void) {
  18. A a;
  19. a.print ();
  20. return 0;
  21. }

 

integer.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. class Double {
  4. public:
  5. Double (double data) :
  6. m_data (new double (data)) {
  7. cout << "構造" << endl;
  8. }
  9. ~Double (void) {
  10. cout << "析構" << endl;
  11. delete m_data;
  12. }
  13. void print (void) const {
  14. cout << *m_data << endl;
  15. }
  16. private:
  17. double* m_data;
  18. string m_lable;
  19. };
  20. int main (void) {
  21. // {
  22. Double d1 (3.14);
  23. d1.print ();
  24. // }
  25. Double* d2 = new Double (1.23);
  26. delete d2;
  27. cout << "再見!" << endl;
  28. return 0;
  29. }
 
main.cpp
    
  1. #include "s.h"
  2. // 使用Student類
  3. int main (void) {
  4. Student s ("張飛", 25);
  5. s.eat ("包子");
  6. return 0;
  7. }

retthis.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. class Counter {
  4. public:
  5. Counter (void) : m_data (0) {}
  6. Counter& inc (void) {
  7. ++m_data;
  8. return *this;
  9. }
  10. void print (void) {
  11. cout << m_data << endl;
  12. }
  13. private:
  14. int m_data;
  15. };
  16. int main (void) {
  17. Counter c;
  18. // c.inc ();
  19. // c.inc ();
  20. // c.inc ();
  21. c.inc ().inc ().inc ();
  22. c.print (); // 3
  23. return 0;
  24. }

s.h
    
  1. #ifndef _S_H
  2. #define _S_H
  3. #include <string>
  4. using namespace std;
  5. // 宣告Student類
  6. class Student {
  7. public:
  8. Student (const string& name = "", int age = 0);
  9. void eat (const string& food);
  10. private:
  11. string m_name;
  12. int m_age;
  13. };
  14. #endif // _S_H


s.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. #include "s.h"
  4. // 實現Student類
  5. Student::Student (const string& name /* = "" */,
  6. int age /* = 0 */) : m_name (name),
  7. m_age (age) {}
  8. void Student::eat (const string& food) {
  9. cout << m_name << "," << m_age << "," << food
  10. << endl;
  11. }

student.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. class A {
  4. public:
  5. A (int a) {}g++ g
  6. };
  7. class Student {
  8. private:
  9. string m_name;
  10. int m_age;
  11. A m_a;
  12. public:
  13. void eat (const string& food) {
  14. cout << m_age << "歲的" << m_name
  15. << "同學正在吃" << food << "。" << endl;
  16. }
  17. // void _ZN7Student3eatERKSs (Student* this,
  18. // const string& food) int{
  19. // cout << this->m_age << "歲的"<<this->m_name
  20. // << "同學正在吃" << food << "。" << endl;
  21. // }
  22. void setName (const string& name) {
  23. if (name == "2")
  24. cout << "你才" << name << "!" << endl;
  25. else
  26. m_name = name;
  27. }
  28. void setAge (int age) {
  29. if (age < 0)
  30. cout << "無效的年齡!" << endl;
  31. else
  32. m_age = age;
  33. }
  34. // 建構函式
  35. Student (const string& name, int age) :
  36. m_a (100) {
  37. m_name = name;
  38. m_age = age;
  39. }
  40. // 無參構造
  41. Student (void) : m_a (100) {
  42. m_name = "沒名";
  43. m_age = 0;
  44. }
  45. // 單參構造
  46. Student (const string& name) : m_a (100),
  47. m_name (name), m_age (0) {
  48. m_name = "哈哈";
  49. }
  50. };
  51. int main (void) {
  52. Student s1 ("張飛", 25);
  53. s1.eat ("包子");
  54. // _ZN7Student3eatERKSs (&s1, "包子");
  55. Student s2 = Student ("趙雲", 22);
  56. s2.eat ("燒餅");
  57. // _ZN7Student3eatERKSs (&s2, "燒餅");
  58. Student s3;
  59. s3.eat ("煎餅果子");
  60. Student* s4 = new Student ("關羽", 30);
  61. s4->eat ("烤鴨");
  62. delete s4;
  63. Student& s5 = *new Student ();
  64. s5.eat ("麵條");
  65. delete &s5;
  66. Student sa1[3] = {s1, s2};
  67. sa1[0].eat ("KFC");
  68. sa1[1].eat ("KFC");
  69. sa1[2].eat ("KFC");
  70. Student* sa2 = new Student[3] {s1, s2}; // 2011
  71. sa2[0].eat ("KFC");
  72. sa2[1].eat ("KFC");
  73. sa2[2].eat ("KFC");
  74. delete[] sa2;
  75. Student s6 ("劉備");
  76. s6.eat ("米飯");
  77. return 0;
  78. }
 
this.cpp
    
  1. #include <iostream>
  2. using namespace std;
  3. class A {
  4. public:
  5. A (int data) : data (data) {
  6. cout << "構造: " << this << endl;:set cursorline

  7. // this->data = data;
  8. }
  9. void foo (void) {
  10. cout << "foo: " << this << endl;
  11. cout << this->data << endl;
  12. }
  13. int data;
  14. };
  15. int main (void) {
  16. A a (1000);
  17. cout << "main: " << &a << endl;
  18. a.foo ();
  19. A* pa = new A (1000);
  20. cout << "main: " << pa << endl;
  21. pa->foo ();
  22. delete pa;
  23. }
   
 this指標:   一般而言,在類的建構函式或成員函式中,關鍵字this表示一個指標,    對於建構函式而言,this 指向正在被構造的物件,對於成員函式而言,    this指向呼叫該函式的物件。













來自為知筆記(Wiz)