C++ 继承题

来源:互联网 发布:商城模板html源码 编辑:程序博客网 时间:2024/05/24 00:02

1#include 
class A{ 

public: 
A(){func(0);}; 
virtual void func(int data){printf("A1 :%dn",data);} 
virtual void func(int data) const{printf("A2 :%dn",data);} 
void func(char *str){printf("A3 :(%s)n",str);} 
}; 
class B:public A{ 
public: 

void func(){printf("B1 :%sn","");} 
void func(int data){printf("B2 :%dn",data);} 
void func(char *str){printf("B3 :(%s)n",str);} 
}; 
int main() 

A *pA; //1
B b; //2
const A *pcA; //3
pA=&b; //4
pA->func(1); //5
pA->func("test"); //6
A().func(1); //7
pcA=&b; //8
pcA->func(2); //9
return 0; //
}  
-----------------------------------------------------
分析:
1、声明了一个A类指针pA
2、实例了一个B类对象b;(执行默认构造函数,先父类A的构造,调fun(0),是虚函数,但是在基类构造时,所有虚函数都不虚;后子类的构造函数,无)
3、const A类指针pcA
4、基类指针pA指向派生类对象;
5、基类的指针指向派生类对象:那么该指针只能够调用基类所定义的函数,如果基类中没有该函数,则报错,如果有且非虚则调基类该函数(不管派生类中该函数情况),但是如果该函数为虚函数,引发了动态绑定,到派生类中去找是否重写了该函数,重写则调用该派生类自己的成员函数,没重写则仍调用基类的该函数。
基类有,虚,派生类中也有,调派生类的。
6、基类的func(char *str)不是虚函数,所以调A的。
7、A()实例一个临时A类对象,调A的默认构造,.func(1)调A的,因为这个对象就是个A类对象,不因为是虚函数就必须调派生类重写函数。
8、const A类指针pcA指向派生类B对象b;
9、虽然基类对应函数是虚,但是派生类B类并没有对其进行重写,所以还是输出A类的。
----------------------------------------------------------
答案:
A1 :0      //第2句输出
B2 :1      //第5句输出
A3 :(test) //第6句输出
A1 :0      //第7句输出
A1 :1 
A2 :2      //第9句输出

 ------------------------------------------------------------------------

 

一个父类写了一个virtual 函数,如果子类覆盖它的函数不加virtual ,也能实现多态.virtual 修饰符会被隐形继承的。

 

出处:http://blog.sina.com.cn/s/blog_8af1069601012wl3.html




一道关于C++ 继承/虚函数 笔试题

#include "stdafx.h"#include "stdio.h"#include "string.h"class Father{public:name(){printf("father name\n");};virtual call(){printf("father call\n");};};class Son: public Father{public:name(){printf("Son name\n");};virtual call(){printf("Son call\n");};};main(){Son *Son1=new Son();Father *father1=(Father *)Son1;father1->call();father1->name();((Son *)(father1))->call();((Son *)(father1))->name();Father *f2=new Father();Son *s2=(Son*)f2;s2->call();s2->name();((Father *)(s2))->call();((Father *)(s2))->name();}
output::
Son callfather nameSon callSon namefather callSon namefather callfather name

虚函数的调用通过虚函数指针来调用,如果new的对象是Son的,则不管它转化成什么指针,它的指针都是Son内部的,与指针类型无关,只与指针地址有关

非虚函数的调用则由指针类型决定

出处:http://www.cnblogs.com/yangyh/archive/2011/06/04/2072393.html


 


原创粉丝点击