Skip to main content

Posts

Showing posts from January, 2018

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 members of the

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 objects can also access a

Dynamic Memory allocation in C++

Similar to C, C++ supports allocating memory at run time in the heap.  In C we use malloc (), calloc () and realloc () for this operation. And the memory thus allocated, is released using free ().  In C++ dynamic memory allocation uses two operators instead of functions, new and delete . Both these operators work on all basic data types as well as user defined classes and structures new operator new operator allocates memory to a variable and returns a pointer to it. new does not need number of bytes to be allocated, instead it needs data type of pointee. int * ptr = new int ; Here memory is allocated to an integer and its address is stored in ptr .  Value pointed by this pointer is not initialized. To initialize the pointee we can use the modified form int * ptr = new int ( 10 ); We have allocated memory for ptr and stored 10 in the location pointed by ptr. If new operator fails, it throws a bad_alloc exception. new[] operator The secon

Friends of C++

Friend function Remember that a member which is not public (Private and protected )  can not be accessed from outside the class.  Three are some situations where you may need to access these. Keyword friend  is used for this. A friend functions and classes can access all members of a class.  This concept is controversial. People say friend concept violates data encapsulation  A friend function is a non-member function but still can access all the members of a class including private members. Such a function is declared within class body with the prefix "friend" class Number { int num; public: Number( int m){ /*code*/ } friend void printNum(Number ob); /*friend function*/ }; void printNum (Number obj) { cout << obj.num << "endl" ; } printNum() is not member of class Number . But it can still access all members including private members, because it is a friend. Friend class An object of a friend class