zoukankan      html  css  js  c++  java
  • 一道关于继承和多态的题目

    #include<iostream>
    #include <complex>
    
    using namespace std;
    //----------------------------------------------
    class Base//基类
    {
    public:
    	virtual void f(int);
    	virtual void f(double);
    	virtual void g(int i=10);
    };
    
    void Base::f(int)
    {
    	cout<<"Base::f(int)"<<endl;
    }
    
    void Base::f(double)
    {
    	cout<<"Base::f(double)"<<endl;
    }
    
    void Base::g(int i)
    {
    	cout<<i<<endl;
    }
    //------------------------------------------------
    
    class Dervived:public Base
    {
    public:
    	void f(complex<double>);
    	void g(int i=20);
    };
    
    void Dervived::f(complex<double>)
    {
    	cout<<"Dervived::f(complex<double>)"<<endl;
    }
    
    void Dervived::g(int i)
    {
    	cout<<i<<endl;
    }
    //----------------------------------------------
    void main()
    {
    	Base b;
    	Dervived d;
    	Base *pb=new Dervived; 
    	b.f(1.0);//基类对象调用,执行基类的函数
    	d.f(1.0);//子类对象调用,执行子类的函数
    	pb->f(1.0);//基类的指针,指向子类,调用函数时,执行基类的函数
    	b.g();//基类对象调用,执行子类的函数
    	d.g();//子类对象调用,执行子类的函数
    	pb->g();//基类的指针,指向子类,调用函数时,执行基类的函数
    	delete pb;
    }
    
  • 相关阅读:
    第三章 kubernetes核心原理
    Jmeter
    Docker 入门学习
    第二章 Kuberbetes实践指南
    第一章 Kubernetes入门
    java中的引用与ThreadLocal
    Disruptor极速队列
    Redis设计与实现——单机数据库的实现
    Redis设计与实现——数据结构与对象
    python装饰器
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/2008948.html
Copyright © 2011-2022 走看看