Skip to main content

Posts

C++ Namespace

C++ Namespaces Namespaces in C++ are used to organize too many classes so that it can be easy to handle the application. For accessing the class of a namespace, we need to use namespacename::classname. We can use  using  keyword so that we don't have to use complete name all the time. In C++, global namespace is the root namespace. The global::std will always refer to the namespace "std" of C++ Framework. C++ namespace Example Let's see the simple example of namespace which include variable and functions. #include <iostream>    using   namespace  std;   namespace  First {          void  sayHello() {            cout<< "Hello First Namespace" <<endl;               }     }     namespace  Second  {             void  sayHello() {               cout<< "Hello Second Namespace" <<endl;           }     }    int  main()   {    First::sayHello();    Second::sayHello();   return  0;   }   Output: Hello First Namespace Hello Second

C++ Function Overriding & Virtual Function

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. C++ Function Overriding Example Let's see a simple example of Function overriding in C++. In this example, we are overriding the eat() function. #include <iostream>    using   namespace  std;   class  Animal {        public :   void  eat(){     cout<< "Eating..." ;         }       };    class  Dog:  public  Animal     {       public :     void  eat()         {            cout<< "Eating bread..." ;         }     };   int  main( void ) {      Dog d = Dog();        d.eat();       return  0;   }   Output: Eating bread... C++ virtual function A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using th

C++ Overloading (Function and Operator)

C++ Overloading (Function and Operator) If we create two or more members having the same name but different in number or type of parameter, it is known as C++ overloading. In C++, we can overload: methods, constructors, and indexed properties It is because these members have parameters only. Types of overloading in C++ are: Function overloading Operator overloading C++ Function Overloading Function Overloading is defined as the process of having two or more function with the same name, but different in parameters is known as function overloading in C++. In function overloading, the function is redefined by using either different types of arguments or a different number of arguments. It is only through these differences compiler can differentiate between the functions. The  advantage  of Function overloading is that it increases the readability of the program because you don't need to use different names for the same action. C++ Function Overloading Example Let's see the simple