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;
    }
    
  • 相关阅读:
    Domain Space
    Class WriteGroupAttribute
    HelloCube:IJobForEach
    HelloCube:ForEach
    组件
    世界
    DOTS默认情况下的性能
    ECS适合你吗?
    DOTS原则和愿景
    Packages window(包窗口)
  • 原文地址:https://www.cnblogs.com/zhangdongsheng/p/2008948.html
Copyright © 2011-2022 走看看