Skip to main content

Posts

RTTI

Oh, do not confuse this word with RTI - right to information. This is not a political blog :). RTTI - run time type identification is a concept in C++, which gives the type of the object during run time. But it can be used only with polymorphic classes - i.e. the classes which have at least one virtual function. Typeid typeid is an operator in C++ which gives the actual type of the object. So what is actual type of object? Is it different from the one we have defined it with? It may be. class A { public: virtual void print(){} }; class B : public A { }; int main () { int a; A obj1; B obj2; A * ptr1 = new B; }  In the code above, type of a is integer. Type of obj1 is A. Type of obj2 is B. But what about *ptr1? Is the type of *ptr1 A or B? typeid operator correctly gives its type as B. Note : We have added a dummy print function in base class, to make A as polymorphic. Now let us use typeid operator - which needs the inclusion of ...

Polymorphism

You hear the term Polymorphism too frequently with object oriented languages. Along with Inheritance and Encapsulation, polymorphism is one of the corner stones of object oriented design. What is Polymorphism, exactly? Polymorphism is a mechanism by which you provide single interface for multiple methods. (poly - many, morph - form). In C++, polymorphism can be compile time or run-time. Compile time polymorphism is provided with the help of overloaded operators/functions and templates. Run time polymorphism is provided with the help of virtual functions and late binding. Late Binding: Connecting a function call to function body is called binding. Most functions use early binding where this binding happens before the program is run - during compile time. This is also called static binding. Late binding (also called dynamic binding)  is when a function call is connected to function body at run time. This is done after looking at the type of the object. Late binding is...

Multiple Inheritance

In C++,  a class can inherit from more than one class. This is called multiple inheritance. Some other OOPS languages like Java, do not support multiple inheritance. Syntax for multiple inheritance is class cls - name : public base - cls1, public base - cls2,public base-cls3 { /*code*/ } We are specifying multiple classes as base classes here. The base class list is comma separated and has access specifier for each base class. Each of these base classes must have been declared earlier. Derived class objects contain sub-objects of each of these base classes. class LandAnimal { int legs; public: void walk() { cout << "walks" ; } }; class WaterAnimal { int fins; public: void swim() { cout << "swims" ; } }; class Amphibian : public LandAnimal, public WaterAnimal { }; int main () { Amphibian frog; frog.walk(); frog.swim(); } Output walks swims In the above example, class Amp...

Inherticance in C++

What is inheritance? In object oriented programming, Inheritance is a mechanism by which a new class can be created as an extension of an existing class. Inheritance helps us in code reuse . The existing class is called base class /super class  and new class is called derived class / sub class  or inherited class. Derived class is an extension of base class. It inherits all the members of the base class (even the private members). And it can have new members of its own. Base class members which are public and protected are accessible from derived class object. Private members of base class are not directly accessible in derived class but can be accessed using base class methods. Syntax class derived - class : access - specifier base - class { /* members*/ }; We write derived class name followed by :, followed by public/private/protected followed by base class name. Then the usual definition of class is written where in we write new membe...

Operator Overloading -II

Let us consider some special cases of operator overloading Overloading of subscript operator:   Subscript operator ([]) can be overloaded to access the dynamically allocated array elements within an object. Using subscript operator, we can treat these like a POD array and access i th element of the array using obj[i]   class Arr { int * arr; int size; public: int & operator []( int n) ; int operator []( int n) const ; /****code *****/ }; int & Arr :: operator []( int index) { return arr[index]; } int Arr :: operator []( int index) const { return arr[index]; } int main() { Arr obj(10); for ( int i=0;i<10;i++) obj[i] = i*i; for ( int i=0;i<10;i++) cout<<arr[i]<<" "; } Output      0 1 4 9 16 25 36 49 64 81 Why do we have two functions for this operator?  Is it allowed? Is it necessary? const version of [] operator function is written so that constant object...