继承的定义

上一篇 / 下一篇  2007-11-25 15:00:32 / 个人分类:c++

非常简单...

class 派生类名:<访问限定符> 基类名1,<访问限定符> 基类名2,...//默认为private

{

/*类题*/

};

实例:

// deriveclass.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class animal
{
public:
 int m_nAge;
 int m_nWeight;
 void sound(){};
};
///////
class Dog:public animal
{
public:
 void swim()
 {
  cout<<"Dog can swim"<<endl;
 }
 void sound()
 {
  cout<<"bow--bow--...--bow"<<endl;
 }
};
///////
class Cat:public animal
{
public:
 void catchrat()
 {
  cout<<"cat can catch rat"<<endl;
 }
 void sound()//重载基类..
 {
  cout<<"miaow--miaow--...---miaow"<<endl;
 }
};

int _tmain(int argc, _TCHAR* argv[])
{
 Dog dog;
 dog.m_nAge = 3;
 dog.swim();
 dog.sound();
 Cat cat;
 cat.m_nAge = 2;
 cat.catchrat();
 cat.sound();

 getchar();
 return 0;
}


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar