类型转换构造函数(内置类型->自定义类型)

1.PNG

实例

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Complex
  4. {
  5. public:
  6. double real,imag;
  7. Complex(int);
  8. Complex(double,double);
  9. };
  10. Complex::Complex(double r,double i)
  11. {
  12. real=r;
  13. imag=i;
  14. std::cout<<"Constructor Called"<<endl;
  15. }
  16. Complex::Complex(int n)
  17. {
  18. real=n;
  19. imag=0;
  20. std::cout<<"int-type Conversion Constructor Called"<<endl;
  21. }
  22. int main(void)
  23. {
  24. Complex c1(3,4);//普通构造函数
  25. Complex c2=5;//类型转换构造函数
  26. c1=7;//类型转换构造函数 7自动转换成一个临时Complex类对象赋给c1
  27. std::cout<<c1.real<<ends<<c1.imag<<endl;
  28. /*Output
  29. Constructor Called
  30. int-type Conversion Constructor Called
  31. int-type Conversion Constructor Called
  32. 7 0 */
  33. return 0;
  34. }

自动类型转换函数(自定义类型->内置/自定义类型)

1.PNG

实例

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. class A
  4. {
  5. private:
  6. int x;
  7. public:
  8. A(int m): x(m) {};
  9. void int_t(int i) { cout << "int = " << i << endl; };
  10. void double_t(double d) { cout << "double = " << d << endl; }
  11. void A_t(const A& a) { cout << "A = " << a.x << endl; }
  12. };
  13. class Fraction
  14. {
  15. private:
  16. int num;
  17. int den;
  18. public:
  19. Fraction(int x, int y):num(x), den(y) {};
  20. operator int() const {return (int)(num / den);};
  21. operator double() const {return (double)(num * 1.0 / den);};
  22. operator A() const {return A(num);}
  23. };
  24. int main(void)
  25. {
  26. Fraction fra(9,7);
  27. A a(0);
  28. a.double_t(fra);
  29. a.int_t(fra);
  30. a.A_t(fra);
  31. }
  32. /*
  33. double = 1.28571
  34. int = 1
  35. A = 9
  36. */

析构函数

2.PNG

实例

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class CName
  4. {
  5. private:
  6. char *Name;
  7. public:
  8. CName(void);
  9. ~CName(void);
  10. };
  11. CName::CName(void)
  12. {
  13. Name=new char[10];
  14. cout<<"Constructor Called"<<endl;
  15. }
  16. CName::~CName(void)
  17. {
  18. delete [] Name;
  19. cout<<"Destructor Called"<<endl;
  20. }
  21. int main(void)
  22. {
  23. CName c;//对象初始化,调用构造函数
  24. //Constructor Called
  25. return 0;//程序结束,对象消亡,调用析构函数
  26. //Destructor Called
  27. }

析构函数和数组

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class CTest
  4. {
  5. public:
  6. ~CTest(void);
  7. };
  8. CTest::~CTest(void)
  9. {
  10. cout<<"Destructor Called"<<endl;
  11. }
  12. int main(void)
  13. {
  14. CTest arr[2];
  15. //delete运算符导致析构函数的调用
  16. CTest *pTest=new CTest;
  17. delete pTest;
  18. CTest *c=new CTest[3];
  19. delete [] c;
  20. cout<<"End Main"<<endl;
  21. return 0;
  22. }
  23. /*Destructor Called
  24. Destructor Called
  25. Destructor Called
  26. Destructor Called
  27. End Main
  28. Destructor Called
  29. Destructor Called
  30. */

在对象作为形参和作为函数返回值时被调用

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class CTest
  4. {
  5. public:
  6. ~CTest(void);
  7. };
  8. CTest::~CTest(void)
  9. {
  10. cout<<"Destructor Called"<<endl;
  11. }
  12. CTest C;
  13. CTest obj(CTest c)
  14. {
  15. return c;//函数执行完毕,对象形参消亡,调用
  16. }
  17. int main(void)
  18. {
  19. C=obj(C);//函数调用返回的临时对象被使用过了,消亡
  20. return 0;//程序结束,全局对象消亡
  21. }
  22. /*
  23. Destructor Called
  24. Destructor Called
  25. Destructor Called
  26. */