You cannot copy content of this page.

Author Archive admin

Concept of Polymorphism in C++



Concept of Polymorphism in C++

Polymorphism is one of the most important features of Object Oriented Programming.  Polymorphic variables can take up objects or values of different types as values and polymorphic functions and procedures can be used as objects or values of different types.  It refers to exhibiting different behaviour by an instance in different situations.  Polymorphism is implemented through function overloading and operator overloading.  Overloading is a form of polymorphism.  In case of function overloading, the information regarding which function is to be invoked corresponding to a function call is available at compile time.  This is referred to as static binding or compile time polymorphism, as the object is bound with its function call at compile time.

 

Run time polymorphism is achieved through virtual functions.  The function to be invoked by the instance of a class is known during runtime.  The functions are linked with the class during runtime and not during compile time.  When a base class pointer points to a sub class instance, instead of invoking subclass function (a function having name similar to that of function of a base class), it always invokes base class version.  If the function in the base class is defined as virtual, then it is known during runtime only, which version of the function will be invoked, corresponding to a function call.  Since the function is linked after compilation process, it is termed as late binding or dynamic binding or run-time polymorphism

Tags

Difference between a Virtual function and Overloaded function in C++




Difference between a Virtual function and Overloaded function in C++

C++ supports dynamic linking through virtual functions. Whenever both the base class and derived class have function with the same name, then the base class function always invokes the base class function irrespective of the fact that it is pointing to the derived class function. When a function is defined as virtual, it is determined at runtime, which function to use depending upon the type of object pointed to by the base class pointer. Thus, if a base class pointer points to different objects of different classes, by defining base member function as virtual, we can invoke different versions of different function.

Function Overloading is the phenomenon of defining different functions with the same name but different function signatures i.e. we can use same function name to perform a wide variety of tasks on different data types. The function to be invoked is determined by the number and type of arguments. Hence in function overloading the function prototypes differ and are not identical.

If the prototypes of the virtual function defined in the base class, and all other functions defined in derived classes is identical then they are considered as virtual functions. If the two functions with the same name have different prototype, then they are considered as overloaded functions and the virtual function phenomenon is ignored irrespective of the virtual function.

Tags

Pure Virtual Functions in C++




Pure Virtual Functions in C++

A Pure Virtual Function is a virtual function that is initialised to zero and has no definition in the base class.  Such functions are merely included in the base class, but do not perform any task.  A pure virtual function is also referred to as a do-nothing function.  Such a function is defined separately in all derived classes and its definition in the base class is not required.  The compiler, in case of a pure virtual function, requires each derived class to either define the function or re-declare it as a pure virtual function.  Such a class which contains a pure virtual function cannot be instantiated,  is referred to as an abstract class.


Consider the following code segment:

class base
{
public:
virtual void show_output() = 0;
//a pure virtual function declared
//and initialized to zero.
};

class derived : public base
{
public:
void show_output()
//function re-defined in derived class.
{
cout<<”Inside show_output function.”;
}
};

As the presence of a pure virtual function in a class requires that it should be implemented in derived classes, in the above code segment show_output() function in class ‘base’ is defined as virtual and is initialized with zero, so it is a pure virtual function.  Class ‘derived’ is publicly inherited from class ‘base’.  Class ‘base’ is an abstract base class, as it includes a pure virtual function.

 

Tags

Difference between containership and inheritance in C++



Difference between containership and inheritance in C++

Containership:  Containership is the phenomenon of using one or more classes within the definition of other class.  When a class contains the definition of some other classes, it is referred to as composition, containment or aggregation.  The data member of a new class is an object of some other class.  Thus the other class is said to be composed of other classes and hence referred to as containership.  Composition is often referred to as a “has-a” relationship because the objects of the composite class have objects of the composed class as members.


Inheritance:  Inheritance is the phenomenon of deriving a new class from an old one.  Inheritance supports code reusability.  Additional features can be added to a class by deriving a class from it and then by adding new features to it.  Class once written or tested need not be rewritten or redefined.  Inheritance is also referred to as specialization or derivation, as one class is inherited or derived from the other.  It is also termed as “is-a” relationship because every object of the class being defined is also an object of the inherited class.

 

Tags

Difference between a Virtual class and an Abstract class in C++




Difference between a Virtual class and an Abstract class in C++

Virtual Class: A virtual class is one that has been qualified as virtual in the inheritance definition.  When a derived class inherits from more than one base class, then it can inherits the members of a base class from multiple inheritance paths. If the base class is defined as virtual, only one copy of the members of the base class is inherited by the derived class irrespective of the fact that it can be inherited from a number of inheritance paths.

 

Abstract Class: An abstract class is one, which is never instantiated.  The objects of an abstract base class are never created.  A base class that contains pure virtual functions is an abstract base class.  A pure virtual function is a virtual function that has no implementation in its class.  Thus an abstract class provides a base upon which to build other classes.  It is designed only to act as a base class for other classes and it is not used to create objects.  Thus an abstract base class provides some transit to the derived classes and is never initiated.

Tags

Class Inheritance in C++


Class Inheritance in C++

Class Inheritance which is a property of OOPs allows a new class to take up the behaviour as well as structure of existing classes, without having to re-design and re-implement this behaviour and structure for the new class.  Hence, the most important advantage of inheritance is code reusability.  A class that has been defined and tested may be used many times, which is extensibility feature supported by inheritance.  Thus the software developed can be reused and can be easily extended.  Every class inherits all the features of the super class.  Only additional features need to be defined, which specify and extend the class further.  The class from which the properties are inherited is referred to as the base class, whereas that class receiving inherited properties is termed as derived class.

Inheritance is achieved by deriving new classes from the existing ones and thus forming a hierarchy of classes.  The following are some of the advantages of inheritance:

a) Reusability:  Inheritance supports the concept of reusability. i.e. reusing existing codes without rewriting the entire code again.  We can derive new classes out of existing ones, without actually modifying it.

b)  Extensibility:  Extensibility of existing codes is the other advantage of using inheritance.  The derived class inherits some or all the properties of the base class and adds some extra features of its own.  This phenomenon of deriving a new class out of existing one i.e. derivation or inheritance helps in extending existing classes by adding new features to them.

c)  Transitive nature:  Inheritance is transitive in nature.  By transitive nature of inheritance we mean that if a child class inherited from a parent class is inherited further then all the sub-child of child class will in turn receive the properties of the parent class too.  For example: If a class named ‘child’ is inherited from another class named ‘father’, then all the sub-classes of ‘child’ will automatically inherit the properties of ‘father’.

d)  We can easily express inheritance relationship through inheritance diagram which is another advantage of inheritance.

All these features make inheritance an important phenomenon / property of OOPs.


Example: Create a class “Employee” of an organization with members Employee-no, Name, Designation, Basic pay.  Define a derived class Executive whose basic_pay is more than Rs. 10,000.

#include <iostream.h>
#include <conio.h>

#define MAX 5

class employee
{
int employee_no;
char name[25];
char desig[25];
float basic_pay;
public:
employee();
void getdata();
float check_sal();
void putdata();
};

employee :: employee()
{
employee_no=0;
basic_pay=0;
}

void employee :: getdata()
{
cout<<“\nEnter Employee Code>”;
cin>>employee_no;
cout<<“\nEmployee Name>”;
cin>>name;
cout<<“\nDesignation>”;
cin>>desig;
cout<<“\nBasic Salary>”;
cin>>basic_pay;
}

void employee :: putdata()
{
cout<<“\n\n\nEmployee Code:”<<employee_no;
cout<<“\nEmployee Name>”<<name;
cout<<“\nDesignation>”<<desig;
cout<<“\nBasic Salary>”<<basic_pay;
}

float employee :: check_sal()
{return(basic_pay);
}

class executive : public employee
{
int empno;
float comm;
public:
void getinfo(int i)
{empno=i;cout<<“\nCommission = “;
cin>>comm;
}
void putinfo()
{cout<<“\nFor employee no “<<empno;
cout<<“, commission = “<<comm;
}
};

void main()
{
int i,k=0,n=0;
employee emp[MAX];
executive exec[MAX];
clrscr();
cout<<“\nInput Employee Information:”;
for(i=0;i<MAX;i++)
{emp[i].getdata();
if(emp[i].check_sal()>10000)
{
cout<<“\nEnter Executive Information:”;
exec[k].getinfo(i+1);
k++;
}
}
cout<<“\nDisplaying Employee Information:”;
for(i=0,k=0;i<MAX;i++)
{
emp[i].putdata();
if(emp[i].check_sal()>10000)
{exec[k++].putinfo();
}
}
getch();
}

 

Tags,

Limitations on Constructors and Destructors in C++




Limitations on Constructors and Destructors in C++

Limitations on Constructors:-

The following are the limitations on constructor functions.

  1. Constructor functions should be declared in the public section of the class.  Private and Protected constructors are available only to the member functions and friends of that class.
  2. A constructor function does not have any return type, not even void.  Hence, constructor functions can’t return any value.
  3. A constructor function may not be static.
  4. We cannot fetch the address of a constructor function.
  5. Constructors cannot be made virtual.
  6. Constructors may not be static.
  7. Constructor functions cannot be inherited.  However, a derived class can call the base class constructor.
  8. An object of a class with a constructor can’t be member of a union.
  9. The copy constructor and the default constructor are generated by the compiler only if these have not been declared for a class.
  10. Name of the constructor function must be same as that of the class.

Limitations on Destructors:

The following are the limitations imposed on destructor functions:

  1. Destructors neither take any argument nor do they return any value, not even void.
  2. Destructors cannot be inherited.
  3. It is not possible to refer to the address of a destructor.
  4. A destructor may not be static.
  5. An object of a class with a destructor cannot be a member of a union.
  6. A destructor function must have public access in the class declaration.
  7. A destructor name must be similar to that of a class, preceded by a tilde (~) symbol.


Tags,

Concept of Data hiding in a class in C++


Concept of Data hiding in a class in C++

In C++, the class groups data members and functions under three access specifiers – private, protected and public, where private and protected members remain hidden from outside world and thereby support data hiding, so that only relevant and required information is exposed to the user and rest of the unnecessary information remains hidden from the user.  The private and protected membes remain hidden from the user and the public members form an interface by providing the relevant and needed information to the outside world, which is nothing but data hiding.  Consider the following example:

class student
{
int roll_no;
public:
int rno;
void get_info()
{
roll_no = 5;
}
void put_info()
{
cout<<”Roll no = “<<roll_no;
}
};
void main(void)
{
student obj;
obj.roll_no = 5;  //results in an error //message-roll_no //not accessible.
obj.rno = 7;  // a valid assignment
obj.get_info();
}

In the above example, when the private variable roll_no is referred to by the object of the same class, it results in an error message, as a private member is accessible to only member functions.  So in order to initialize this private data member, a member function named get_info() is used, that assigns the value 5 to roll_no.  But the second assignment is perfectly a valid assignment in C++ as rno is a public variable.  Hence data members are hidden by specifying them under private section in a class.

 

Tags,

Macro vs. Inline function in C++




Macro vs. Inline function in C++

Functions considerably increase the speed of the program, when it is likely to be called many number of times.  But, if the function is small in size, then it may be defined as inline function which greatly reduces the function calling overheads.  Whenever an inline function is called, the function is inserted in the code itself rather than calling it, as is the case with normal functions.  This insertion is performed during compilation time.  Inline functions are declared like normal functions except that the function precedes with the keyword inline.

 

However, it should be noted that it is up to the wish of the compiler to make a function inline or not, even after inline specification has been given.  Also the inlining procedure will not follow if a function contains static variable or is recursive.  If a function has looping or conditional construct, and it also return value, then the inlining won’t function.


 

Macro is a preprocessor directive.  It is not really a function.  So some overheads are involved in error checking during compilation time.  When a constant is associated with a user defined symbol, that could be used in place of constant, the characters making up the constant are assigned not the numeric value of the constants.  All macros can occupy more than one line but they can not include a newline.  Macros can take arguments too.  The advantages of macros vs. functions depend on the situation.  C++ provides its own facility to define functions as inline, which have same working as that of macros, but, since they are functions, so the compiler checks for the possible errors very easily.  Further, it is upto the compiler whether to make a function inline or not, but macros, without memory consideration are expanded.  Thus in C++, inline functions prove to be much useful then macros.

Tags,

Function scope and File scope in C++




Function scope and File scope in C++

The scope for a function or a variable is determined by the place, where it is declared.  If it is defined within the body of a function then the scope is local to the function or function scope.  For example: if a function named sub() is defined inside another function named super(), then sub() can be accessed only through super().  Hence scope of sub() is local to super().

super()
{
———-
———-
sub()
{
———-
———-
}
}

Here sub() is a local function to super() and is not externally accessible.

If the above declaration of sub() function appears outside super() or all other functions, then it becomes available to all the functions in the file and the scope now is referred to as the file scope.  Thus it is accessible from anywhere in the program.  Such variables and functions which are globally available within a program are global variables and functions.  Hence

super()
{
————-
————-
}
sub()
{
————–
————–
}
Here sub() function is globally available within the program from the point of its definition.


The program demonstrating function scope and file scope is as follows:

#include <iostream.h>
#include <conio.h>
void super(void)
{
cout<<”Inside super…!”;
void sub(void)
{
cout<<”Inside sub…!”;
}
cout<<”Calling sub…!”;
sub();  // sub() function accessible
//as local to the block.
}

void main()
{
cout<<”Calling Super…!”;
super();  //function invoked as defined as a
//global function and has file scope.
cout<<”Calling Sub…!”;
sub();    // sub() function not accessible as
//it is local to the function super() and
//has function scope, thus the
//function is not accessible.
getch();
}

 

Tags,

x Close

Like Us On Facebook