c++子类调用基类方法的一个例子
2016-08-19 09:59
288 查看
Base.h
#pragma once class Base { public: Base(void); ~Base(void); bool CreatClone(Base* item); char* m_baseprop1; }; |
Base.cpp
#include "StdAfx.h" #include "Base.h" Base::Base(void) : m_baseprop1(NULL) { } Base::~Base(void) { } bool Base::CreatClone(Base* item) { m_baseprop1 = item->m_baseprop1; return false; } |
Derived.h
#pragma once #include "base.h" class Derived : public Base { public: Derived(void); ~Derived(void); char* m_devprop1; bool CreatClone(Derived* item); }; |
Derived.cpp
#include "StdAfx.h" #include "Derived.h" Derived::Derived(void) : m_devprop1(NULL) { } Derived::~Derived(void) { } bool Derived::CreatClone(Derived* item) { m_devprop1 = item->m_devprop1; //基类方法的调用通常放在方法的最后。 Base::CreatClone(item); return false; } |
Main.cpp
#include "stdafx.h" #include <string> #include <windows.h> #include "Derived.h" int _tmain(int argc, _TCHAR* argv[]) { Derived* d = new Derived(); d->m_baseprop1 = "aa"; d->m_devprop1 = "bb"; Derived* d2 = new Derived(); d2->CreatClone(d); system("pause"); //这里打断点跟踪,发现d的m_baseprop1和m_devprop1都克隆给了d2 return 0; } |
相关文章推荐
- 声明一个父类A,它只有一个非默认构造方法;声明A的子类B,B具有默认构造方法及非默认方法,并在B的构造方法中调用基类A的构造方法。
- 关于C++中子类调用父类方法的一个问题
- java程序练习:尝试创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,为子类创建一个对象,将他向上转型到基类并调用这个方法。
- 尝试创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,为子类创建一个对象,将它向上转型到基类并调用这个方法。
- 关于C++中子类调用父类方法的一个问题
- 一个内核模块例子(实现调用其他模块中的方法,传递参数等)
- PL/SQL小技巧一个:在子类中怎么调用父类被重载的方法
- C++和JAVA中一些区别,包括指向子类的引用会调用谁的方法,子类构造函数自动调用父类构造函数时,父构造会调用的谁方法
- PL/SQL小技巧一个:在子类中怎么调用父类被重载的方法
- c# 关于基类和子类的方法调用问题--总结
- 子类调用父类方法:c++和java的不同
- 基接口,基类、子类在调用时的一个有趣现象
- C++中方法定义和调用需要注意的一个地方
- c++与java中子类中调用父类成员的方法
- PHP父类调用子类方法的代码例子
- Java 程序调用一个 C++ 类的方法
- 在C++中怎么调用一个js中的方法
- 一个C++调用C的例子
- c/c++中利用gsoap调用webservice服务的一个诡异问题解决方法
- new 一个没有定义构造方法的子类对象,会调用父类的默认构造方法