Vous êtes sur la page 1sur 3

In C++, overriding is a concept used in inheritance which involves a base class implementation of a method.

Then in a subclass, you would make another implementation of the method. This is overriding. Here is a simple example. class Base { public: virtual void DoSomething() {x = x + 5;} private: int x; }; class Derived : public Base { public: virtual void DoSomething() { y = y + 5; Base::DoSomething(); } private: int y; }; Here you can see that the derived class overrides the base class method DoSomething to have its own implementation where it adds to its variable, and then invokes the parent version of the function by calling Base::DoSomething() so that the x variable gets incremented as well. The virtual keyword is used to that the class variable can figure out which version of the method to call at runtime. Overloading is when you make multiple versions of a function. The compiler figures out which function to call by either 1) The different parameters the function takes or 2) the return type of the function. If you use the same function declaration, then you will get a compiler error because it will not know which function to use. Here is another simple example. class SomeClass { public: void SomeFunction(int &x) { x *= x; } int SomeFunction(int x) { return x * x; } }; // In main() SomeClass s; int x = 5; x = SomeFunction(x); The compiler knows to call the second implementation of the method because we are assigning the return value from the function to x. C++ Overriding What is overriding in C++? Redefining a base class function in the derived class to have our own implementation is referred as overriding. Often confused with overloading which refers to using the same function name but with a different signature. However in overriding the function signature is the same. EXAMPLE: Demonstrate the usage of overriding #include <iostream> using namespace std; class Base { public: virtual void myfunc() { cout << "Base::myfunc .." << endl; }

}; class Derived : public Base { public: void myfunc() { cout << "Derived::myfunc .." << endl; } }; void main() { Derived d; d.myfunc(); } OUTPUT:Derived::myfunc .. C++ Operator Overloading What is Operator Overloading? Mimics conventional usage of operators for user defined types. Operator :: (scope resolution), . (member selection) and .* (member selection thru pointer to function) cannot be overloaded. EXAMPLE: Demonstrate the usage of operator overloading #include <iostream> #include <ostream> using namespace std; class Employee { private: int iEmpId; string iEmpName; public: Employee(int, string); friend ostream& operator<<(ostream&, const Employee&); }; // Constructor Employee::Employee(int aEmpId, string aEmpName) { iEmpId = aEmpId; iEmpName = aEmpName; } // Operator Overloading ostream& operator<<(ostream& os, const Employee& emp) { cout << "Emp Id = " << emp.iEmpId << endl; cout << "Emp Name = " << emp.iEmpName << endl; return os; } void main() { Employee e1(100, "Employee1");

Employee e2(200, "Employee2"); cout << e1 << endl; cout << e2 << endl; } OUTPUT: Emp Id = 100 Emp Name = Employee1 Emp Id = 200 Emp Name = Employee2

Vous aimerez peut-être aussi