关于c++类成员方法覆盖或屏蔽

这个是我在调试模版方法模式时得到的结论,与我开始预想的不一样(基本功不扎实)

#include "stdafx.h"
#include <iostream>
#include  <string>
using namespace std;

class Base
{
public:
    virtual	void SeeBook();
protected:
	Base(const string _bookname);
    virtual void DoSeeBook()=0;
	void OpenBook();
    void CloseBook();
private:
   string BookName;
};

Base::Base(const string _bookname):BookName(_bookname){}

void Base::OpenBook()
{
	cout<<"Base: open the book "<<BookName<<endl;
}

void Base::CloseBook()
{
    cout<<"Base: close the book "<<BookName<<endl;
}

void Base::SeeBook()
{
	OpenBook();
	DoSeeBook();
	CloseBook();
}

class Derived2:public Base
{
public:
    Derived2(const string _bookname):Base(_bookname){}
    
protected:
	void OpenBook()
	{
	   cout<<"Derived2: open the book "<<endl;
	}

    virtual void DoSeeBook()
	{
		cout<<"Derived2: see the book "<<endl;
	}

    void CloseBook()
	{
       cout<<"Derived2: close the book "<<endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Derived2 d("stl 源码解析");
	d.SeeBook();
	system("PAUSE");
	return 0;
}


关于c++类成员方法覆盖或屏蔽_第1张图片

结论:

父类方法funA在函数体内调用自己的方法funB(funB为非虚),虽然子类覆盖了父类的方法funB(即重新实现了父类的方法funB),但是子类对象执行方法funA时,funA还是执行的是父类方法FunB.(及上面d.SeeBook();的SeeBook()方法还是执行Base内的OpenBook()ColseBook()方法

要使子类对象执行funA时执行自己的funB,可以将父类funB设定为virtual 及虚函数

#include "stdafx.h"
#include <iostream>
#include  <string>
using namespace std;

class Base
{
public:
    virtual	void SeeBook();
protected:
	Base(const string _bookname);
    virtual void DoSeeBook()=0;
	virtual void OpenBook();
    virtual void CloseBook();
private:
   string BookName;
};

Base::Base(const string _bookname):BookName(_bookname){}

void Base::OpenBook()
{
	cout<<"Base: open the book "<<BookName<<endl;
}

void Base::CloseBook()
{
    cout<<"Base: close the book "<<BookName<<endl;
}

void Base::SeeBook()
{
	OpenBook();
	DoSeeBook();
	CloseBook();
}

class Derived2:public Base
{
public:
    Derived2(const string _bookname):Base(_bookname){}
    
protected:
	void OpenBook()
	{
	   cout<<"Derived2: open the book "<<endl;
	}

    virtual void DoSeeBook()
	{
		cout<<"Derived2: see the book "<<endl;
	}

    void CloseBook()
	{
       cout<<"Derived2: close the book "<<endl;
	}
};

int _tmain(int argc, _TCHAR* argv[])
{
	Derived2 d("stl 源码解析");
	d.SeeBook();
	system("PAUSE");
	return 0;
}


关于c++类成员方法覆盖或屏蔽_第2张图片

一个简单一点的例子如下:functionB为非虚函数

#include "stdafx.h"
#include <iostream>
#include  <string>
using namespace std;

class Base
{
public:
  void	functionA()
  {
	  functionB();
  }
protected:
  void functionB()
  {
	  cout<<"Base::functionB"<<endl;
  }
};

class Derived:public Base
{
protected:
  void functionB()
  {
	  cout<<"Derived::functionB"<<endl;
  }
};

int _tmain(int argc, _TCHAR* argv[])
{
	Derived d;
	d.functionA();
	system("PAUSE");
	return 0;
}


关于c++类成员方法覆盖或屏蔽_第3张图片

functionB设置为虚函数时:

#include "stdafx.h"
#include <iostream>
#include  <string>
using namespace std;

class Base
{
public:
  void	functionA()
  {
	  functionB();
  }
protected:
  virtual void functionB()
  {
	  cout<<"Base::functionB"<<endl;
  }
};

class Derived:public Base
{
protected:
  void functionB()
  {
	  cout<<"Derived::functionB"<<endl;
  }
};

int _tmain(int argc, _TCHAR* argv[])
{
	Derived d;
	d.functionA();
	system("PAUSE");
	return 0;
}
关于c++类成员方法覆盖或屏蔽_第4张图片

你可能感兴趣的:(成员方法覆盖)