Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3015260
  • 博文数量: 167
  • 博客积分: 613
  • 博客等级: 中士
  • 技术积分: 5473
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-13 21:35
个人简介

人, 既无虎狼之爪牙,亦无狮象之力量,却能擒狼缚虎,驯狮猎象,无他,唯智慧耳。

文章分类
文章存档

2015年(19)

2014年(70)

2013年(54)

2012年(14)

2011年(10)

分类: C/C++

2013-07-11 14:47:01

    《Essential C++》虽然短小但是读起来并不容易,尤其对于一直面向过程编程的自己来说,上午看STL,虽然不太懂,但感觉应该是很好用的东东,大大提高了程序代码的通用性,而且是不是类似C的标准库呢?跳过这章后是面向对象编程模块,作者举了之前列出的数列的例子,但是自己的心有些浮躁,因为C++语法自己在考二级的时候已经学过一些了,现在想更多的转换编程的思路,具体的语法和技术细节可以自己去查阅Prime,利用下午的时间快速的浏览完两章,基本算是结束这本小册子了,接下来要开始学习MFC进入编程实战了。今天下午最主要的收获是:
1. C++面向对象引入了三个独特的概念:继承、多态和动态绑定。继承使得一组类具有共同的基类,即通用的接口;多态使得当通过基类的指针或者引用进行操作时,实际执行的操作要等到实际运行时才能确定绑定的对象方法。一般使用虚函数加以实现。总的思想是:针对基类编写利用通用接口的函数操作,借助虚函数实现多态。
2. 面向对象设计的首要一步是明确编程对象,从中抽象出基类,即共同的操作/行为;第二步要筛选出与数据类型有关联的操作/行为,作为虚函数实行多态;第三步则要根据需要划分基类中成员的层级:public,protected与private。当然编程实现的过程中还需要根据需要调整基类的成员和方法,以实现最优。
   接下来要开始实际编写程序了,实际去体会C++的程序设计思想;学习MFC,可以写出GUI程序。在那之前先贴一个有关图书馆图书的类,简单的三层类体系:Library(LibMat)----->Book----->Magazine

点击(此处)折叠或打开

  1. //A example of Library System
  2. //Basic class: LibMat--->Book---->Magazine

  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <string>

  6. using namespace std;

  7. class LibMat
  8. {
  9.       public :
  10.              LibMat() {cout << \"LibMat::LibMat() default constructor!\\n\";}
  11.              virtual ~LibMat() {cout <<\"LibMat::~LibMat() destructor!\\n\";}
  12.              virtual void print()
  13.                     {cout << \"LibMat::print()--> I\'m a LibMat Object!\\n\";}
  14.       };

  15. class Book : public LibMat
  16. {
  17.       public :
  18.              Book(string title, string author):_title(title), _author(author)
  19.              {
  20.                          cout << \"Book::Book(\" <<_title<<\",\"<<_author
  21.                          <<\") constructor!\\n\";
  22.                          }
  23.              virtual ~Book() {cout <<\"Book::Book() destructor!\\n\";}
  24.              virtual void print()
  25.              {
  26.                      cout << \"Book::print()--> I\'m a Book Object!\\n\"
  27.                           << \"Title is \"<< _title<< endl
  28.                           << \"Author is \"<< _author <<endl;
  29.                      
  30.                      }
  31.              string& title() {return _title;}
  32.              string& author() {return _author;}
  33.       protected : //proctected层级可以供子类访问
  34.                 string _title;
  35.                 string _author;
  36.       };
  37. class Magazine : public Book
  38. {
  39.       public :
  40.              Magazine(string title, string author, string date)
  41.              :Book(title, author), _date(date)
  42.              {
  43.                           cout << \"Magazine::Magazine()\\n\"
  44.                                << _title<<endl
  45.                                << _author<<endl
  46.                                << _date<< \"constructor!\\n\";
  47.                           }
  48.              virtual ~Magazine() {cout << \"Magazine::~Magazine() destructor!\\n\";}
  49.              virtual void print()
  50.              {
  51.                      cout <<\"Magazine::print()\\n\"
  52.                           <<\"My title is \"<<_title<<endl
  53.                           <<\"My author is \"<<_author<<endl
  54.                           <<\"My date is \"<<_date<<endl;
  55.                      }
  56.       protected :
  57.                 string _date;
  58.       };

  59. void print(LibMat &libmat)
  60. {
  61.      cout << \"in global print()\\n\";
  62.      libmat.print();
  63.  }
  64.  
  65. int main()
  66. {
  67.     cout << \"Begin to print for test...\\n\\n\";
  68.     cout << \"LibMat object test...\\n\";
  69.     LibMat mat;
  70.     LibMat &r_mat = mat;
  71.     print(r_mat);
  72.     cout << \"*******************************************************************\";
  73.     system(\"pause\");
  74.     
  75.     cout << \"Book object test...\\n\";
  76.     Book book(\"西游记\",\"哈利波特\");
  77.     Book &r_book = book;
  78.     print(r_book);
  79.     cout << \"*******************************************************************\";
  80.     system(\"pause\");
  81.     
  82.     cout << \"Magazine object test...\\n\";
  83.     Magazine mag(\"妈妈宝宝\",\"**科技出版社\",\"2013-07\");
  84.     Magazine &r_mag = mag;
  85.     print(r_mag);
  86.     cout << \"*******************************************************************\";
  87.     system(\"pause\");
  88.     return 0;
  89.     
  90.     
  91.     
  92. }
运行结果如下,可以通过打印输出看出构造对象的顺序:
  
阅读(3215) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~