Differentiate between function overloading and function overriding

前端 未结 11 1909
臣服心动
臣服心动 2020-11-29 18:40

Differentiate between function overloading and function overriding in C++?

11条回答
  •  孤街浪徒
    2020-11-29 19:06

    Function overloading is done when you want to have the same function with different parameters

    void Print(string s);//Print string
    void Print(int i);//Print integer
    

    Function overriding is done to give a different meaning to the function in the base class

    class Stream//A stream of bytes
    {
    public virtual void Read();//read bytes
    }
    
    class FileStream:Stream//derived class
    {
    public override void Read();//read bytes from a file
    }
    class NetworkStream:Stream//derived class
    {
    public override void Read();//read bytes from a network
    }
    

提交回复
热议问题