17

I know what overriding is in C++. But, is there overwriting? If so, what does it mean?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • I've never heard the term "overwriting" used for anything in relation to C++. – Edward Strange Jan 19 '11 at 17:26
  • 3
    The origin of the term "overwriting" could be a bad translation from german to english. The german technical term for "overriding" is "überschreiben" which literally(!) translates back to english as "overwriting". So might be you speak german? – nabulke Jan 19 '11 at 18:14
  • @nabulke, I have ZD(Zertifikat Deutsh), so, ich spreche etwas Deutsch. – Simplicity Jan 19 '11 at 21:55

6 Answers6

22

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).

7

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?

moinudin
  • 134,091
  • 45
  • 190
  • 216
5

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.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • 2
    Actually it's not only _operators_ that can be overloaded, but any function. (IIRC, `std::sqrt()` is overloaded. – sbi Jan 19 '11 at 17:17
2

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.

Frank N
  • 9,625
  • 4
  • 80
  • 110
0

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.

  • 1
    Welcome to SO. Unfortunately, your answer does not provide any further useful information than the answers already given by other users. Additionally, the questioner mentioned he already knows about **overriding** and want's to know if there is something like **overwriting**. So you didn't really answer the question. Consider editing or deleting this answer. – wychmaster Jul 02 '20 at 07:59
0

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
    */
hessamini
  • 49
  • 1
  • 9