开发者

C++ Overriding... overwriting?

I know what overriding is in C++. But, is there overwriting? If so, what does 开发者_运维问答it mean?

Thanks.


In C++ terminology, you have overriding (relating to virtual methods in a class hierarchy) and overloading (related to a function having the same name but taking different parameters). You also have hiding of names (via explicit declaration of the same name in a nested declarative region or scope).

The C++ standard does not use the term "overwrite" except in its canonical English form (that is, to replace one value with a new value, as in the assignment x = 10 which overwrites the previous value of x).


You can overwrite variables, e.g. int a = 0; a = 42; and files (open an existing file for write - if you have permission it will overwrite the existing file contents) if that's what you mean. This has little in relation to overriding. Were you perhaps thinking of overloading?


The usual distinction I'm familiar with is of overriding and overloading. Virtual functions are overridden. Functions are overloaded when there's a version with same name but different signature (this exists in many languages). In C++ you can also overload operators.

AFAIK, overwriting is an unrelated concept (overwrite a variable, file, buffer, etc.), and is not specific to C++ or even OOP languages.


Override is "the normal thing" in OOP: A derived class provides a different (i.e. more specialized) implementation for something, overriding the base class, e.g. apple::foo() overrides fruit::foo() if apple is a class derived from fruit. (not to be mistaken with overload by using different parameter signatures, which leads to completely distinct functions).

Overwrite I know as to completely replace with another-definition. Not on a specific level but in general for the remainder of the programm. This sometimes gets used javascript, if a big framework has some special issues, and you don't want to tear the big file apart:

<script type="text/javascript" 
    src="some super big framework, often in one big file">
<script type="text/javascript">
  Ext.ux.window.createWin = function() {
     // completely OVERWRITE the implementation 
       (often to 'hotfix' a particular bug)
  }
</script>

However: I don't know of any such thing in C++, as a concurring redefinition of a function would always lead to errors already at compile time. At most, I can imaginge bending function pointers, or (re)defining call back hooks.


C++ Function Overriding. If derived class defines same function as defined in its base class, it is known as function overriding in C++. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the function which is already provided by its base class.


Override is meaning making a method with virtual keyword in the base class and the base class is allowing to the child classes for make a body of same method for it self .

Overwrite is meaning Override without virtual keyword.

Overload is meaning make multiple methods with different input parameters with One name.

SAMPLES:

    #include <stdio.h>
    #include <stdlib.h>
    class par_overwrite
    {public:    par_overwrite() {}; ~par_overwrite() {};
        int at() { return 1; };
    };
    class chld_overwrite :public par_overwrite
    {public:    chld_overwrite() {};    ~chld_overwrite() {};
        int at() { return 2; }//overwrite
    };
    void main_overwrite()
    {
        par_overwrite pt;
        int ptat = pt.at();
        chld_overwrite ct;
        int ctat = ct.at();
        printf("ptat:%d,ctat:%d\n",ptat, ctat);  //output : ptat:1,ctat:2
    }
    class par_override
    {public:    par_override() {};  ~par_override() {};
        virtual int ad() { return 3; };
    };
    class chld_override :public par_override
    {public:    chld_override() {}; ~chld_override() {};
        int ad() { return 4; }//override
    };
    void main_override()
    {
        par_override pd;
        int pdad = pd.ad();
        chld_override cd;
        int cdad = cd.ad();
        printf("pdad:%d,cdad:%d\n", pdad, cdad); //output : pdad:3,cdad:4
    }
    class par_override_pure
    {public:    par_override_pure() {}; ~par_override_pure() {};
        virtual int adp()=0;//Pure Virtual Function
    };
    class chld_override_pure :public par_override_pure
    {public:    chld_override_pure() {};    ~chld_override_pure() {};
        int adp() { return 5; }//override from Pure Virtual Function
    };
    void main_override_pure()
    {
        //par_override_pure pdp;//error : Can not create object from abstract class that have (Pure Virtual Function)
        //int pdpad = pdp.ad();//error
        chld_override_pure cdp;
        int cdpadp = cdp.adp();
        printf("cdpadp:%d\n", cdpadp); //output : cdpadp:5
    }
    class overload
    {public:    overload() {}; ~overload() {};
        void method_overload(int prm1) { printf("ol1\t"); }
        void method_overload(int prm1, int prm2) { printf("ol2\t"); }
        void method_overload(int prm1, int prm2, int prm3) { printf("ol3\t"); }
        void method_overload(double prm1) { printf("ol4\t"); }
        void method_overload(double prm1, double prm2) { printf("ol5\t"); }
        void method_overload(double prm1, double prm2, double prm3) { printf("ol6\t"); }
    };
    void main_overload()
    {
        overload ol;
        ol.method_overload(1);
        ol.method_overload(1, 1);
        ol.method_overload(1, 1, 1);
        ol.method_overload(0.1);
        ol.method_overload(0.1, 0.1);
        ol.method_overload(0.1, 0.1, 0.1); // output : ol1     ol2     ol3     ol4     ol5     ol6
    }
    int main()
    {   main_overwrite();
        main_override();
        main_override_pure();
        main_overload();

        getchar();
        return 0;
    }
    /* output:
    ptat:1,ctat:2
    pdad:3,cdad:4
    cdpadp:5
    ol1     ol2     ol3     ol4     ol5     ol6
    */
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜