码迷,mamicode.com
首页 > 编程语言 > 详细

C++继承与多态示例

时间:2014-11-05 13:04:46      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   os   sp   2014   log   as   c++   

#include <iostream>
#include <string>


using namespace std;


class Pet {
  protected:
	string name;
  public:
      void setname(string aname) {
	    this->name = aname;
	  }
	  
	  virtual string bark() {
		return name + ", Hello";
	  }
};


class Dog : public Pet {
  public:
	  virtual string bark() {
		string tmp = Pet::bark();
		return tmp + ", dog";
	}
};




int main() {
  Dog d;
  d.setname("doga");
  cout << d.bark() << endl;
  return 0;
}

C++继承与多态示例

标签:blog   io   ar   os   sp   2014   log   as   c++   

原文地址:http://blog.csdn.net/miscclp/article/details/40820821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!