快乐学C++:(5) 对象的初始化

 

快乐学C++: 对象的初始化
摘要:
本文避开面向对象的理论,简单讲述面向对象的由来,并通过代码和对代码结果的分析,阐述了面向对象中的重要的概念:对象的初始化。
 
对象和面向对象的由来:
C++ 面向对象编程中,最重要的概念莫过于“对象”了。简单的说,对象就是把数据和对数据的操作进行抽象和封装形成的用户自定义的数据类型。
 
C++ 提供了简单的基本数据类型,如整型,字符型,浮点型,还提供了对这些基本类型的简单复合,如数组,字符数组,及字符串; C++ 还提供了用户自定义的结构体,枚举类型。
 
在引入面向对象编程之前,程序员思考问题的方法对已知的问题一步一步进行分解,直至问题小得可以通过数据结构加算法来描述。这种思想方法一般称为面向过程的编程思想。面向过程解决需求明确,规范小的软件还可以,当面复杂的行业应用,有大量的应用领域的专门业务逻辑的规则的时候,程序员就面临着巨大的挑战,这个挑战就是程序员不得不面对具体的数据和对数据操作的细节。往往程序员并不是应用领域的专家,所以处理这些业务逻辑并不擅长,比如,某个程序员要编写保险行业的软件,保险行业本身的业务规则就非常复杂,因此程序员在大型软件的开发上所以很容易就败下阵来。 20 世80年代,曾经有统计,高达 70% 的软件不能在预算内如期完成。
 
人们呼唤换作思想来开发软件,这种思想就是面向对象。面向对象的核心思想就是把数据隐藏起来形成新的数据类型,只提供访问这些数据的接口,只要接口满足预期的目的,使用这些对象的程序员就不必关心接口内部的实现细节。面向对象有一个重要的原则,就是面向接口编程。
 
对象的实现
本文的上一节把面向对象“吹”得神忽其神,那么,在 c++ 中,什么是对象,对象怎么创建,对象的接口怎么访问,对象怎么销毁呢?
请看下面的代码和代码的运行结果。我们重点演示了对象的初始化。
请注意对象的下面几个重要函数:
构造函数;
带参数的构造函数 ;
拷贝构造函数;
赋值函数;
析构函数。
 
这些概念请同学们自己从教科书上查看详细解释,按照本系列文章的惯例,我们通过代码来理解上述函数。
 
代码和运行结果
 main.cpp
Code:
  1. //file: main.cpp   
  2. #include "square.h"   
  3. #include <iostream>   
  4. using namespace  std;   
  5. Square_T g_s(9);   
  6.   
  7. int main (int , char *[])   
  8. {   
  9.     cout << "main start here" << endl;   
  10.     cout << "a: ";     
  11.     Square_T  a;       
  12.     a.draw();   
  13.   
  14.     cout << "b: ";     
  15.     Square_T b (5);   
  16.     b.draw();   
  17.   
  18.   
  19.     {   
  20.         cout << "inner b: ";       
  21.         Square_T b (5);   
  22.         b.draw();   
  23.     }   
  24.   
  25.     cout << " c: ";    
  26.     Square_T c = b;   
  27.     c.draw();   
  28.   
  29.     cout << " d: ";    
  30.     Square_T d (b);   
  31.     d.draw();   
  32.     d.side(10);   
  33.   
  34.     cout << " d: ";    
  35.     Square_T e;   
  36.     e = d;   
  37.     e.draw();   
  38.   
  39. //  Square_T f = 20;   
  40. //  f.draw();   
  41.     cout << "create object dynamically" << endl;   
  42.   
  43.     Square_T* h = new Square_T(30);   
  44.     h->draw();   
  45.     delete h;   
  46.     h = 0;   
  47.   
  48.     cout << " begin of array "<< endl;   
  49.     {   
  50.         Square_T  many[5];   
  51.     }   
  52.     cout << "end of  array "<< endl;   
  53.        
  54.     cout << "main end here" << endl;   
  55.   
  56.     return 0;   
  57. }   
  58.   
  59.   

 

Square.h

Code:
  1. //file : square.h   
  2. #ifndef SQUARE_H   
  3. #define SQUARE_H   
  4.   
  5. class Square_T    
  6. {   
  7. public:   
  8.     Square_T ();   
  9.     explicit Square_T (int side);   
  10.     Square_T (const Square_T & a);   
  11.     Square_T & operator = (const Square_T & a);   
  12.   
  13.     virtual ~Square_T ();   
  14.     virtual int draw();   
  15.   
  16.   
  17.     void side( int sideLength);   
  18.     int side() const ;   
  19.     //  求面积   
  20.     int area();   
  21.     //  求周长   
  22.     int circumference();   
  23.   
  24. private:   
  25.     int side_;   
  26. };   
  27. #endif   
  28.   

 

 

Square.cpp

Code:
  1. //file: square.cpp   
  2. #include <iostream>   
  3. using namespace  std;   
  4.   
  5. #include "square.h"   
  6. Square_T ::Square_T ()   
  7. :side_(0)   
  8. {   
  9.     cout << "call: default constructor function " << endl;   
  10. }   
  11.   
  12. Square_T::Square_T(int side)   
  13. : side_(side)   
  14. {   
  15.     cout << "call: constructor with parameter (side =" << side_ << " )" << endl;   
  16. }   
  17. Square_T ::Square_T (const Square_T & a)   
  18. :side_(a.side_)   
  19. {   
  20.     cout << "call: copy constructor function (a.side =" << a.side_ << " )" << endl;   
  21.   
  22. }   
  23.   
  24.   
  25. Square_T& Square_T::operator = (const Square_T& a)   
  26. {   
  27.     cout << "call : assigement function " << endl;   
  28.     if (this != &a)   
  29.     {   
  30.         this->side_ = a.side_;   
  31.     }   
  32.     return *this;   
  33. }   
  34.   
  35.   
  36. Square_T::~Square_T()   
  37. {   
  38.     cout << "call : destructor function , side = " << this->side_ << endl;   
  39.   
  40. }   
  41.   
  42. int Square_T::draw()   
  43. {   
  44.     cout << "square, side " << this->side_ << endl;   
  45.   
  46.     return 0;   
  47. }   
  48.   
  49. void Square_T::side(int side)   
  50. {   
  51.     this->side_ = side;   
  52. }   
  53.   
  54. int Square_T::side() const    
  55. {   
  56.     return this->side_;   
  57. }   
  58.   
  59. int Square_T::area()   
  60. {   
  61.     return this->side_ * this->side_;   
  62. }   
  63.   
  64. int Square_T::circumference()   
  65. {   
  66.     return 4 * this->side_;   
  67. }  
运行结果
Code:
  1. call: constructor with parameter (side =9 )   
  2. main start here   
  3. a: call: default constructor function    
  4. square, side 0   
  5. b: call: constructor with parameter (side =5 )   
  6. square, side 5   
  7. inner b: call: constructor with parameter (side =5 )   
  8. square, side 5   
  9. call : destructor function , side = 5   
  10.  c: call: copy constructor function (a.side =5 )   
  11. square, side 5   
  12.  d: call: copy constructor function (a.side =5 )   
  13. square, side 5   
  14.  d: call: default constructor function    
  15. call : assigement function    
  16. square, side 10   
  17. create object dynamically   
  18. call: constructor with parameter (side =30 )   
  19. square, side 30   
  20. call : destructor function , side = 30   
  21.  begin of array    
  22. call: default constructor function    
  23. call: default constructor function    
  24. call: default constructor function    
  25. call: default constructor function    
  26. call: default constructor function    
  27. call : destructor function , side = 0   
  28. call : destructor function , side = 0   
  29. call : destructor function , side = 0   
  30. call : destructor function , side = 0   
  31. call : destructor function , side = 0   
  32. end of  array    
  33. main end here   
  34. call : destructor function , side = 10   
  35. call : destructor function , side = 10   
  36. call : destructor function , side = 5   
  37. call : destructor function , side = 5   
  38. call : destructor function , side = 0   
  39. call : destructor function , side = 9   
补充说明
请注意:
1.       全局变量 (side=9) 的初始化会早于 main 开始,销毁要晚于 main 结束。
2.       请注意 C++ 关键字 explicit 的运行,它可以防止将一个整数转换成 Square_T 对象。即
Square_T f = 20; 或 f = 20; 编译时就报错,阻止这种不正当的行为。
3.       注意不同变量的作用域,查看析构函数的调用时机。
 
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值