• C++重载函数定义和用法


    /************************************************************************/
    #include <iostream.h>
    #include <WINDOWS.H>
    
    /************************************************************************/
    /* 定义一个CLOCK类                                                      */
    /************************************************************************/
    class CLOCK
    {
    	private:
    		int hour, minute, second;
    	public:
    		CLOCK();
    		CLOCK(int, int, int);
    
    		void update();
    		void display();
    	
    };
    
    /************************************************************************/
    /* 定义CLOCK函数                                                        */
    /************************************************************************/
    CLOCK::CLOCK()
    {
    	hour = minute = second = 0;
    	cout << "\t One object initialalized." << endl;
    }
    
    /************************************************************************/
    /* 定义CLOCK重载函数                                                    */
    /************************************************************************/
    CLOCK::CLOCK(int h, int m, int s)
    {
    	hour = h;
    	minute = m;
    	second = s;
    	cout << "\t An object initialalized." << endl;
    }
    
    /************************************************************************/
    /*                                                                      */
    /************************************************************************/
    void CLOCK::update()
    {
    	second++;
    	if(second == 60)
    	{
    		second = 0;
    		minute ++;
    	}
    	if (minute == 60)
    	{
    		minute = 0;
    		hour++;
    	}
    	if (hour == 24)
    	{
    		hour = 0;
    	}
    }
    
    /************************************************************************/
    /*                                                                      */
    /************************************************************************/
    void CLOCK::display()
    {
    	cout  << hour << ":" << minute << ":" << second <<endl;
    }
    
    /************************************************************************/
    /* main函数                                                             */
    /************************************************************************/
    void main()
    {
    	CLOCK MyClock(12, 0, 0);
    //声明类名
    	while(1)
    	{
    		MyClock.display();
    		MyClock.update();
    		Sleep(1000);
    	}
    }
    

      

    上面的是函数的重载,

    下面是函数的隐藏和覆盖

    #include <IOSTREAM.H>
    
    int a = 5;
    int &b = a;
    
    /************************************************************************/
    /*   Base                                                                   */
    /************************************************************************/
    class Base
    {
    public:
    	virtual void fn();
    };
    /*Derived类的fn(int)函数隐藏了Base类的fn函数*/
    class Derived:public Base
    {
    public:
    	/*派生类的函数与基类的函数同名,但参数列表不同
    	在这种情况下,不管基类的函数声明是否有virtual
    	关键字,基类的函数都将被隐藏。*/
    	void fn(int);
    };
    /*Derived2类的fn()函数隐藏了Derived类的fn(int)
    */
    class Derived2:public Derived
    {
    public:
    	void fn();
    };
    
    
    class animal
    {
    public:
    	void eat()
    	{
    		cout << "animal eat" << endl;
    	}
    
    	void sleep()
    	{
    		cout << "animal sleep" << endl;
    	}
    
    	void breathe()
    	{
    		cout << "animal breathe" << endl;
    	}
    };
    
    class fish:public animal
    {
    public:
    	void breathe()
    	{
    		cout << "fish bubble" << endl;
    	}
    };
    
    void fn(animal *pAn)
    {
    	pAn->breathe();
    }
    
    void main()
    {
    	animal *pAn;
    	fish fh;
    	pAn = &fh;
    	fn(pAn);
    }
    

      

  • 相关阅读:
    js addEventListener事件多次绑定问题
    whistle手机调试工具使用简单教程
    css利用padding-top设置等比例遇到的问题
    webpack多页面配置
    js延时定时器
    webpack打包配置禁止html标签全部转为小写
    css媒体查询aspect-ratio宽高比在less中的使用
    比较好用的移动端适配的两种方案及flexible和px2rem-loader在webpack下的配置
    image-webpack-loader包安装报错解决
    js动态设置padding-top遇到的坑
  • 原文地址:https://www.cnblogs.com/tao560532/p/2369640.html
Copyright © 2020-2023  润新知