Wednesday, 8 April 2015

Types of Inheritance

The derived class inherits some or all the features of the base class depending on the visibility mode and level of inheritance. Level of inheritance refers to the length of its path from the root. A base class itself might have been derived from other classes in the hierarchy. Inheritance is classified into the following forms.

? Single inheritance
? Multilevel inheritance
? Multiple inheritance
? Hierarchical inheritance
? Hybrid inheritance
? Multipath inheritance


INHERITANCE

Base and Derived class

Reusability is yet another important feature of OOP. It is always nice if we could reuse something that already exits rather than trying to create the small all over again. Inheritance allows new classes to be built from older and less specialized classes instead of being rewritten from scratch.

Classes are created by first inheriting all the variables and behavior defined by some primitive class and then adding specialized variables and behaviors. The mechanism of deriving a new class from an old one is called inheritance. The old class is referred to as the base class and the new one is called the derived class.

Defining Derived Class

A derived class is defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is.

Class derived-classname : visibility-mode base -classname
{
members of derived class
}

When a base class is privately inherited by derived class, ‘public members’ of the base class become ’private members’ of the derived class and therefore the public members of the base class can only be accessed by the member functions of the derived class. They are inaccessible to the objects of the derived class. Remember a public member of a class can be accessed by its own objects using the dot operator. The result is that no member of the base class is accessible to the objects of the derived class.


When a base class is publicly inherited by derived class, ‘public members’ of the base class become ‘public members’ of the derived class and therefore they are accessible to the objects of the derived class. In both the cases, the private members are not inherited, and therefore the private members of a base class will never become the members of its derived class.


Monday, 6 April 2015

Non-Overloadable Operators

C++ supports a wide variety of operators, but all of them cannot be overloaded to operate in an analogous way on standard operators. These excluded operators are very few compared to the large number of operators, which qualify for overloading.


Subscript Operator Overloading

The subscript operator [ ] can be overloaded to access the attributes of an object. It is mainly useful for bounds checking while accessing elements of an array.

For example the statement int a[10];

Where 10 indicates the number of locations, where we cannot give the value either by float or char. Because the locations must be integer. We can break this concept by using the subscript operator overloading.

Sample Program

#include <iostream.h>
class sub
{
public:
void operator[](char *n)
{
cout<<”Subscript operator with string”<<endl;
cout<<”Given string is “<<n<<endl;
}
void operator[](float fl)
{
cout<<”Subscript operator with float”<<endl;
cout<<”Given float value is “<<fl<<endl;
}
void operator[](char n)
{
cout<<”Subscript operator with character”<<endl;
cout<<”Given character is “<<n<<endl;
}
};
void main()
{
A[“Subscript”];
A[123.34];
A[‘c’];
}

Overloading Stream Operators using Friend function

The class istream uses the predefined stream cin that can be used to read data from the standard input device. The extraction operator >> is used for performing input operations in the iostream library. The insertion operator << is used for performing output operations in the iostream library.

Sample Program

#include <iostream.h>
class info
{
private:
int rno;
char *name;
float tot;
public:
friend istream & operator>>(istream &in, info & obj)
{
in>>obj.rno;
in>>obj.name;
in>>obj.tot;
}
friend ostream & operator<<(ostream &out, info &obj);
};
ostream & operator<<(ostream &out, info & obj)
{
out<<obj.rno;
out<<obj.name;
out<<obj.tot;
}
void main()
{
info A;
cout<<”Enter the Object Contents”;
cin>>A; // similar to cin.operator>>(A)
cout<<”Given Values are”;
cout<<A; // similar to cout.operator<<(A)
}

Operator overloading with Friend Function

#include <iostream.h>
class complex
{
private:

float real;
float imag;

public:

complex()
{
real = imag = 0.0;
}
void getdata(void)
{
cout<<”Real Part ?”;
cin>>real;
cout<<”Imag Part ?”;
cin>>imag;
}
void outdata(char *msg)
{
cout<<msg;
cout<<”(“<<real;
cout<<”, “<<imag<<endl;
}
friend complex operator – (complex c1)
{
complex c;
c.real = -c1.real;
c.imag = -c1.imag;
return (c);
}
};
void main()
{
complex c1, c2;
cout<<”Enter complex c1…”<<endl;
c1.readdata();
c2 = -c1;
c1.outdata(“Complex c1:”);
c2.outdata(“Complex c2 = - Complex c1:”);
}
Note :

Friend functions offer the flexibility of writing an expression as a combination of operands of user defined and primitive data types. For instance, consider the statement.

C3 = c1 + 2.0;

The expression is made up of the object c1 and a primitive type. In case of an operator member function, both the operands must be of object’s data type. When the friend operator functions are used, both the operands need not be instances of user-defined data type. It requires a parameterized constructor tak ing a primitive data type parameter.

#include <iostream.h>
class complex
{
private:
float real;
float imag;
public:
complex()
{}
complex(int realpart)
{
real = realpart;
}
void readdata(void)
{
cout<<”Real Part ?”:
cin>>real;
cout<<”Imag Part ?”:
cin>>imag;
}
void outdata(char *msg)
{
cout<<endl<<msg;
cout<<”(“<<real;
cout<<”, “<<imag<<”)”;
}
friend complex operator + (complex c1, complex c2);
};
complex operator +(complex c1, complex c2)
{
complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return (c);
}
void main()
{
complex c1, c2, c3 = 3.0;
cout<<”Enter complex c1…”<<endl;
c1.readdata();
cout<<”Enter Complex c2….”<<endl;
c2.readdata();
c3.outdata(“Result of C3 = c1 + c2”);
c3 = c1+ 2.0; // c3 = c1.complex(2.0);
c3.outdata(“Result of c3 = c1 + 2.0”);
c3 = 3.0 + c2;
c3.outdata(“Result of c3 = 3.0 + c2);
}
the statement
c3 = c1 + 2.0;

has an expression which is a combination of the object c1 and the primitive floating point constant 2.0. though, there is no member function matching this expression, the compiler will resolve this by treating the expression as follows:

c2 = c1 + complex(2.0);

the compiler invokes the single argument constructor and converts the primitive values to a new temporary object and passes it to the friend operator function.

Operator Overloading Using Friend Functions

Friend functions play a very important role in operator overloading by providing the flexibility denied by the member functions of a class. They allow overloading of stream operators for stream computation on user defined data types.

The only difference between a friend function and member function is that, the friend function requires the arguments to be explicitly passed to the function and processes them explicitly, whereas the member function considers the first argument implicitly.

Friend functions can either be used with unary or binary operators. The syntax of operator overloading with friend functions is

Friend return_type operator Operator_Symbol (arg1, arg2,…..)
{
// body of function
}

Difference between the normal function and friend function

? The prototype of friend function must be prefixed with the keyword friend inside the class body.

? The body of friend function can appear either inside or outside the body of a class. It is advisable to define    a friend function outside the body of a class.

? The definition of the friend function outside the body of a class is defined as normal function and is not             prefixed with the friend keyword.
? The arguments of the friend functions are generally objects of friend classes. In a friend function, using its        objects can access all the members of a class.
? Friend function is not allowed to access members of a class directly, but it can access all the members            including the private members by using objects of that class.
? Friend function is similar to the normal member function except that it can access the private members of a   class using its objects.