打赏

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. usingnamespace std;
  3. void foo (constint& a){
  4. cout << a << endl;
  5. }
  6. int main (void){
  7. char c ='A';
  8. foo (static_cast<int>(c));
  9. return0;
  10. }
 静态类型转换:
 static_cast <目标类型>(源类型变量)
 

argthis.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classStudent;
  4. classTeacher{
  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. classStudent{
  14. public:
  15. void ask (const string& question,Teacher* t){
  16. cout <<"问题:"<< question << endl;
  17. t->reply ("不知道。");
  18. }
  19. };
  20. voidTeacher::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. return0;
  29. }
 
  1. void ask (const string& question,Teacher* t){
  2. cout <<"问题:"<< question << endl;
  3. t->reply ("不知道。");
 
voidTeacher::educate (Student* s){
s->ask ("什么是this指针?",this);
cout <<"答案:"<< m_answer << endl;
 

clock.cpp
  1. #include<iostream>
  2. #include<iomanip>
  3. usingnamespace std;
  4. classClock{
  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. return0;
  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. usingnamespace 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. return0;
  35. }
 

init.cpp
  1. #include<iostream>
  2. usingnamespace 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. constint m_c;
  15. int& m_r;
  16. };
  17. int main (void){
  18. A a;
  19. a.print ();
  20. return0;
  21. }
 
 

 
integer.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classDouble{
  4. public:
  5. Double(double data):
  6. m_data (newdouble(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 =newDouble(1.23);
  26. delete d2;
  27. cout <<"再见!"<< endl;
  28. return0;
  29. }
 

main.cpp
  1. #include"s.h"
  2. // 使用Student类
  3. int main (void){
  4. Student s ("张飞",25);
  5. s.eat ("包子");
  6. return0;
  7. }
 
retthis.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. classCounter{
  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. return0;
  24. }

s.h
  1. #ifndef _S_H
  2. #define _S_H
  3. #include<string>
  4. usingnamespace std;
  5. // 声明Student类
  6. classStudent{
  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. usingnamespace 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. voidStudent::eat (const string& food){
  9. cout << m_name <<","<< m_age <<","<< food
  10. << endl;
  11. }
 
student.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. class A {
  4. public:
  5. A (int a){}g++ g
  6. };
  7. classStudent{
  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 =newStudent("关羽",30);
  61. s4->eat ("烤鸭");
  62. delete s4;
  63. Student& s5 =*newStudent();
  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 =newStudent[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. return0;
  78. }
 

this.cpp
  1. #include<iostream>
  2. usingnamespace std;
  3. class A {
  4. public:
  5. A (int data): data (data){
  6. cout <<"构造: "<<this<< endl;:set cursorline
  7.  
  8. // this->data = data;
  9. }
  10. void foo (void){
  11. cout <<"foo: "<<this<< endl;
  12. cout <<this->data << endl;
  13. }
  14. int data;
  15. };
  16. int main (void){
  17. A a (1000);
  18. cout <<"main: "<<&a << endl;
  19. a.foo ();
  20. A* pa =new A (1000);
  21. cout <<"main: "<< pa << endl;
  22. pa->foo ();
  23. delete pa;
  24. }
 
 
 
 this指针:
  一般而言,在类的构造函数或成员函数中,关键字this表示一个指针,
   对于构造函数而言,this 指向正在被构造的对象,对于成员函数而言,
   this指向调用该函数的对象。
 
 
 
 
 
 
 
 
 
 
 
 

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">





posted on 2018-11-29 22:58  XuCodeX  阅读(259)  评论(0编辑  收藏  举报

导航