- 浏览: 430248 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
su6838354:
我有点疑问啊,thread1中的i自增的慢的话,thread2 ...
浅析pthread_cond_wait -
zeronever:
请问pthread_cond_signal有解锁操纵吗?我在p ...
浅析pthread_cond_wait -
paladin1988:
你这帖子真心不错。。
浅谈bitmap算法 -
parabellum_sky:
昨天还有个姑娘让我去考我说会考虑
个人日志
C++ 继承
如何设计C++的类?子类和基类之间的关系?子类默认构造函数的初始化列表,多态性,虚构函数的应用?
//Object-Oriented #include<iostream> #include <string> using namespace std; class LibMat { public: LibMat(){cout << "LibMat::LibMat default constructor!\n";} virtual ~LibMat(){cout << "LibMat::~LibMat destructor!\n";} virtual void print ()const { cout << "LibMat::print() -- I am a LibMat object! \n"; } }; class Book : public LibMat { public: Book(const string &title,const string &author):_title(title),_author(author) { cout<< "Book::Book (" << _title << "," << _author << ") constructor\n"; } virtual ~Book(){cout<< "Book::~Book destructor\n";} void print ()const { cout << "Book::print() -- I am a Book object! \n"; cout << "My title is : " << _title << endl; cout << "My author is : "<< _author << endl; } const string & title(){return _title;} const string &author(){return _author;} protected: string _title; string _author; }; class AudioBook : public Book { public: AudioBook(const string &title,const string &author,const string &narrator):Book(title,author),_narrator(narrator) { cout<< "AudioBook::AudioBook (" << _title << "," << _author << "," << _narrator << ") constructor\n"; } ~AudioBook(){cout<< "AudioBook::~AudioBook destructor\n";} void print ()const { cout << "AudioBook::print() -- I am a AudioBook object! \n"; cout << "My title is : " << _title << endl; cout << "My author is : "<< _author << endl; cout << "My narrator is : "<< _narrator << endl; } const string & narrator() const {return _narrator;} //这里两个const,为何两个,用一个可以吗?当语句为string & narrator() const {return _narrator;}时,会提示编译错误,为何?return是const 而接收的却不是const,接收和返回的类型不匹配。 //为何要用两个const:简单的说为了防止改变私有成员的值。 private: string _narrator; }; void print(const AudioBook & audiobook) { cout << "in gobal print(): about to print mat.print()\n"; audiobook.print(); } int main() { AudioBook ab("Mason and Dixon","Thomas Pynchon","Edwin Leonard"); print(ab); //ab.narrator() = "hewei"; //cout <<"\n\n"; //print(ab); }
上述代码的输出:
LibMat::LibMat default constructor!
Book::Book (Mason and Dixon,Thomas Pynchon) constructor
AudioBook::AudioBook (Mason and Dixon,Thomas Pynchon,Edwin Leonard) constructor
in gobal print(): about to print mat.print()
AudioBook::print() -- I am a AudioBook object!
My title is : Mason and Dixon
My author is : Thomas Pynchon
My narrator is : Edwin Leonard
AudioBook::~AudioBook destructor
Book::~Book destructor
LibMat::~LibMat destructor!
构造函数的调用顺序:在使用AudioBook的时候,会最先初始化基类的基类LibMat,调用它的构造函数,然后再初始化基类Book,最后在调用AudioBook的构造函数。
析构函数的调用顺序:和构造函数的调用顺序是相反的。
构造函数初始化列表:首先初始化最底层的基类,依次初始化。
多态性:子类为了实现自己独特的功能而屏蔽掉基类的功能,用virtual作为关键词修饰,类的每一个对象都与一个虚函数列表,程序在编译阶段并不会指定要调用哪个函数,只有在运行时通过查找各个虚函数类表,找出对应的函数。
比如上面的代码,AudioBook的函数print,当程序调用print时,会先查找ab对象的函数列表,发现Book 和 LibMat的print函数均是虚函数,则会调用ab自己的print函数。
发表评论
-
Google编程风格
2012-04-01 17:05 1075Google编程风格(自己整 ... -
VS2008快捷键的设置
2012-02-02 13:43 2614VS2008快捷键的设置 VS2008默认的快捷键和VC++ ... -
LINK : fatal error LNK1000: Internal error during IncrBuildImage
2011-12-16 17:07 1189Win7安vc2008编译报LINK : fatal erro ... -
volatile关键字(摘自:百度百科)
2011-12-16 14:59 703volatile关键字[align=center][/alig ... -
InterlockedIncrement
2011-12-16 14:44 2147InterlockedIncrement[align=cent ... -
关键词explicit
2011-12-10 20:32 823关键词explicit[size=large][/size][ ... -
Souce Insight 设置
2011-12-09 17:16 1956Souce Insight 设置 【问题】 Source I ... -
VS2008下Boost库的安装编译下载boost库
2011-11-09 19:19 2012下载boost库 (最好去官网下,一般有SGI(GCC+用的较 ... -
C++多态技术的实现和反思(转)
2011-10-17 17:05 775面向对象技术最早出现于1960年代的Simula 67系统,并 ... -
memmove and memcpy
2011-07-31 13:11 1154memmove and memcpy 字符串的拷贝函数mem ... -
Polymorphism & Virtual Function
2011-07-25 21:38 828Polymorphism & Virtual Func ... -
C++ 不要重新定义继承的非虚函数
2011-07-18 14:15 1269不要重新定义继承的非虚函数 如果基类和派生类有相同的非虚函数 ... -
C++ 虚函数表解析
2011-07-14 21:02 947C++ 虚函数表解析 为什么在C++机制里要有虚函数表?虚函 ... -
C++ 默认构造函数
2011-07-14 11:39 3694C++ 默认构造函数 一直 ... -
尽量使用const
2011-07-12 10:51 1080尽可能的使用const const: ... -
Operator=
2011-07-11 21:54 913Operator= 赋值构造函数 ... -
虚析构函数
2011-07-11 10:43 1134虚析构函数 一 、为何要单独讨论虚析构函数?虚函数在类中到底 ... -
初始化列表和声明顺序之间的关系
2011-07-11 09:18 1443初始化列表和声明的顺序之间的关系 类中数据成员的声明顺序和初 ... -
初始化函数列表和构造函数内赋值之区别
2011-07-10 17:05 4472初始化函数列表和构造 ... -
传值和传引用的区别
2011-07-10 15:30 2351传值和传引用的区别 在C语言中,大都是通过值传递,C++也是 ...
相关推荐
C++继承与多态性实验报告
在Qt中编写的c++继承的视频
C++继承与派生课件 很值得下载 C++继承与派生课件 很值得下载
08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08_31_C++ 继承的二义性08...
C++继承,虚函数与多态性专题.C++继承,虚函数与多态性专题.
c++继承中的三种访问权限 pdf,一个简单总结,方便查阅。
C++ 继承详解 C++ C++ 继承详解 C++ 继承详解 继承详解
C++中继承与派生的PPT课件 欢迎新手们参考
在Qt中编写的c++继承分类的视频
c++上的一道题,网上尚未有答案,来自郭文平老师的书
c++继承描述
信息科学技术学院《程序设计实习》,C++程序员入门级教程
入门基础实例 有关继承和派生的应用小程序 希望对大家有帮助
利用C++中的继承与多态,实验构造函数的作用
C++继承
c++继承与派生习题答案
本资源是C++继承与派生类实验报告,欢迎大家下载阿!
c++继承与派生,上机操作题,帮助理解继承与派生
C++继承与多态C++继承与多态C++继承与多态C++继承与多态C++继承与多态C++继承与多态
很不错的C++课件,简单易懂,适合自学。深入浅出指点到位