ANDROID APPS

Home‎ > ‎

Blog

Recent C++ Posts

  • Polymorphism In C++ DefinitionPolymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies ...
    Posted Aug 12, 2012, 9:31 PM by Thiyagaraaj Mp
  • Input Output Formats In C++ Introduction: C++ language describes input/output using iostream.h.The standard input / output library for c described by the header stdio.h is still available in C++.The header stream ...
    Posted Jul 18, 2011, 4:53 AM by thiyagaraaj Mr
  • Data Hiding and Encapsulation byThiyagaraaj MVidhyashree.RDefinition:Data Hiding is also known as Encapsulation.Encapsulation is the process of combining data and function into a single unit called class.Data Hiding ...
    Posted Aug 31, 2012, 2:29 AM by Thiyagaraaj Mp
  • Scope Resolution Operator In C++ byThiyagaraj MJamuna MaryDefinition:               The scope resolution operator is use for the Unary scope operator , if a namespace scope  (or) global Scope name  is hidden by an explicit ...
    Posted Apr 12, 2010, 10:20 AM by thiyagaraaj Mr
  • Constructor In C++ Simple Programs For ConstructorIntroductionIt is special member function of the class.It has same name of class.It is automatically called when object(instance of class) create.It ...
    Posted Aug 28, 2011, 6:32 AM by thiyagaraaj Mr
Showing posts 1 - 5 of 5. View more »

C++ Key Concepts

  • What is Polymorphism? Key Concept:polymorphism means that some code or operations or objects behave differently in different contexts.Poly  - Means ManyMorphism Means FormsMany FormsPolymorphismC++ supports several kinds of ...
    Posted Jan 20, 2011, 7:54 AM by thiyagaraaj Mr
Showing posts 1 - 1 of 1. View more »

Polymorphism In C++

posted Mar 14, 2012, 5:45 AM by Thiyagaraaj Mp

Definition

Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions. A single function usage or an operator functioning in many ways can be called polymorphism. 


Types of Polymorphism:

C++ provides three different types of polymorphism.

  • Virtual functions
  • Function name overloading
  • Operator overloading

In addition to the above three types of polymorphism, there exist other kinds of polymorphism:

  • run-time
  • compile-time
  • ad-hoc polymorphism
  • parametric polymorphism
  • Other types of polymorphism defined:

run-time:
The run-time polymorphism is implemented with inheritance and virtual functions.

compile-time:
The compile-time polymorphism is implemented with templates.

ad-hoc polymorphism:
If the range of actual types that can be used is finite and the combinations must be individually specified prior to use, this is called ad-hoc polymorphism.

parametric polymorphism:
If all code is written without mention of any specific type and thus can be used transparently with any number of new types it is called parametric polymorphism.


Explanation:

                    A   polymorphic function or operator has many forms. For example, in c++ the division operator is polymorphic. If the arguments to the division operator areintegral, then integer division is used. However ,if one or both arguments are floating –point then floating –point division is used.

       object oriented programming language support polymorphism,which is characterized by the phares,    "ONE    INTERFACE,    MULTIPLE       METHODS".

      A real world example of polymorphism is a thermostat. no matter what type of furnace your house has (gas,oil,electric etc.)

        In your programe you will create three specific version of these function one for each type of stack,but names of the function will be the same. 

             In C++, a function name or operator can be

 overloaded .A function is called based on its signaturewhich is the list of argument types in its parameter list.

Exaplanation With Example

                     For example ,in the division expression

 a/b  the result depends on the arguments being automaticallycoerced to the widest type. so if both arguments  are integer ,the result is an integer division .But ifone orboth arguments are floating-point, the result is floating-point.

        Another example , is the output statement             

        cout<<a;

Where the shift operator << is invoking a function that is able to output an object of type.  A technique for implementing a package of routines toprovide a shape type could rely on  a comprehensive structural  description of any shape

for instance ,

Struct shape               {

                 enum{circle,rectangle,….}e-val;

                 double center,radius;

                 …………………………

               };

        would have all the members necessary for any shapecurrently drawable in our system.. It would also have anenumerator value, so that it could be identified .The area routine would  than be written as :

           double area(shape *s)

           {                switch(s->e-val)

                   {

                     case circle:return(PI*s>radius*s>radius);

                     case rectangle:return(s>height*s>width);

                     ………………………………………

                    }

             };

 An additional case in the code body and additional members in the structure are needed .Unfortunately, these would have ripple effects throughout our entire code body.

C++ code following this design uses shape as an abstract class containing one or more pure virtual functions, as shown in the following code.   

      //shape is an abstract  base class

     class shape        {

         public:

             virtual double area()=0;

         };

     class  rectangle:public shape

        {

                       public:

                  rectangle             double area ()

             {

                 return(height*width)

               }

 private:

    double height,width;

 };

   class circle:public shape

             {

         public:

         double area()    

         {

             return(3.14159*radius*radius);

         }

         private:

             double radius;

            };

             client code for computing an arbitrary area is polymorphic the appropriate  area() function is selected at run time.              

                           shape * ptr _shape;

                           … …. … … …                     

Cout<<area=”<<ptr_shape->area();

Now imagine is improving our hierarchy of types by

developing a square class:

                      Class square:public rectangle

                     {

                  public:

                  square(double h):rectangle(h,h)

               {

                    }

                    double  area()

                   {

                     return(rectangle :: area());

               }

             };

 The client code remains unchanged. This was not the case with the non-oops code.

Hierarchical design should minimize interface parameter passing. Each layer tends to absorb, within its implementation, state detail that is affected by function invocation. This practice is almost universally condemned, because it leads to opaque side-effect-style coding that is difficult to debug, revise and maintain.

            It is the compiler's job to select the specific action as it applies to each situation.

        The first object -oriented programming language were interpreters,so polymophism was ,of course,supported at run-time. However,c++ is a compiled language.Therefore,in c++,both run-time and compile-time polymorphism are supported.

Input Output Formats In C++

posted Dec 6, 2010, 8:44 AM by thiyagaraaj Mr   [ updated Jul 18, 2011, 4:53 AM ]

Introduction: 

  • C++ language describes input/output using iostream.h.
  • The standard input / output library for c described by the header stdio.h is still available in C++.
  • The header stream.h was used on systems before release 2.0 and is still available under many C++ systems.

Operator: 

      • <<
      • >>

Output class:
 

  • Output is inserted into an object of type ostream.
  • It is declared in the headerfile iostream.h.
  • An operator << is loaded in this class to perform output conversions from standard types
  • The overloaded left shift operator is called the “insertion or put to operator”.
  • The standard output ostream corresponding to stdout is cout.
  • The standard output ostream corresponding to stderr is cerr.
 

Syntax: 

Cout<<”var”<<var<<”\n”; 

Public members: 

      Ostream& operator<<int(i);

      Ostream& operator<<(long i);

      Ostream& put(char c);

      Ostream& write(const char *p, int n);

      Ostream& flush(); 

The member function flush() forces the stream to be written. 

Formatted output & iomanip.h 

      The put to operator << produces by default the minimum number of characters.

Eg: 

      int i=8,j=9;

      cout<<i<<j;

      cout<<i<<” “j;

      cout<<”i=”<<i<<”j=”<<j;

Immediately prints the line:

          X=1; 

User-defined types: output:
 

    It have typically been printed by creating a member function print().

      Ostream& operator<<(ostream& out,dock x)

      {

      for(int i=0;i<52;++i)

      {

      if(i % 13==0)

      out<<endl;

      out<<x.d[i];

      }

      Return out;


The input class isstream: 

  • An operator >> is overloaded in istream to perform input conversions to standard types
  • The overloaded right shift operator is called the extraction or get from operator.
  • The standard input istream corresponding to stdin is cin.
 

Syntax: 

      Cin>>var>>char; 

Public members: 

      istream& operator>>(int& i);

      istream& get(char& c);

      istream& read(char* s,int n); 

Useful member function: 

      int gcount();

      istream& ignore (int n=1,int delimeter=EOF); 

Files:
 

  • C have stdin,stdout,stderr.
  • In addition, systems may define other standard files such as stdprn & stdaux.
  • File I/O is handled by including fstream.h
  • It contains the classes ofstream and ifstream
 

      Ifstream();

      Ifstream(const char *, int=ios::in);

      Ofstream();

      Ofstream(const char *, int =ios::out); 

Standard files 

CC++NameConnected to
Stdin

Stdout

Stderr

Stdprn

stdaux

Cin

Cout

Cerr

Cprn

caux

Standard input file

Standard ouput file

Standard error file

Standard printer file

Standard auxiliary file

Keyboard

Screen

Screen

Printer

Auxiliary port

 
 

File Modes: 

        ArgumentMode
        ios::in

        ios::app

        ios::out

        ios::noreplace

        Input mode

        Append mode

        Output mode

        If file exists open fails


Data Hiding and Encapsulation

posted Apr 14, 2010, 1:21 AM by thiyagaraaj Mr   [ updated Aug 31, 2012, 2:29 AM by Thiyagaraaj Mp ]

Definition:

  • Data Hiding is also known as Encapsulation.
  • Encapsulation is the process of combining data and function into a single unit called class.
  • Data Hiding is the mechanism where the details of the class are hidden from the user.
  • The user can perform only a restricted set of operations in the hidden member of the class.
  • Encapsulation is a powerful feature that leads to information hiding,abstract data type and friend function.
  • They encapsulate all the essential properties of the object that are to be created.
  • Using the method of encapsulation the programmer cannot directly access the class.

Access Specifier:

There are three types of access specifier.They are

  • Private :Within the block.
  • Public:Whole over the class.
  • Protected:Act as a public and then act as a private.

Within a class members can be declared as either public protected or private in order to explicitly enforce encapsulation.
The elements placed after the public keyword is accessible to all the user of the class.
The elements placed after the protected keyword is accessible only to the methods of the class.
The elements placed after the private keyword are accessible only to the methods of the class.
The data is hidden inside the class by declaring it as private inside the class. Thus private data cannot be directly accessed by the object.

For example,
In order to make the design and maintenance of a car reasonable the complex of equipment is divided into modules with particular interfaces hiding design decisions. 

General Form:

class class name
{
private:
datatype data;
public:
Member functions;
};

main()
{
classname objectname1,objectname2……………;

Example:

class Square
{
private:
int Num;

public:
void Get()    {
    cout<<"Enter Number:";
    cin>>Num;
}
void Display()    {
    cout<<"Square Is:"<<Num*Num;
}
};

void main()
{
Square Obj;
Obj.Get();
Obj.Display();
getch()
}

Features and Advantages of Data Encapsulation:

The advantage of data encapsulation comes when the implementation of the class changes but the interface remains the same.
It is used to reduce the human errors. The data and function are bundled inside the class that take total control of maintenance and thus human errors are reduced. 
Makes maintenance of application easier. 
Improves the understandability of the application. 
Enhanced Security. 

Note:Thus the concept of encapsulation shows that a non member function cannot access an object’s private or protected data.

Scope Resolution Operator In C++

posted Mar 31, 2010, 7:35 AM by thiyagaraaj Mr   [ updated Apr 12, 2010, 10:20 AM ]

Definition:

               The scope resolution operator is use for the Unary scope operator , if a namespace scope  (or) global Scope name  is hidden by an explicit declaration of the   Name in block or class.

Example:

int  count=0;
int main(void)
{
int  count=n;
::count=1;
count=2;
return 0;
}

The declaration of count  is declared in the main function Hides the integer named count declared in global namespace scope.The  statement  :: count =1 accesses the variable named Count declared in global namespace scope.

Scope resolution operator  in  C++:

                The scope resolution operator (::) in c++ used to Define the already declared in the member functions of the class.

C++ supports to the global variable from a function,Local variable is to defined in the same function name.

The syntax of the scope resolution operator:

:: global variable name

Resolution operator is placed between the front of the variable name then the global variable is affected.If no resolution operator is placed between the local variable is affected.

Example:

#include<iostream.h>
int n=12;    //global variable
int main()
{
int n=13;   //local variable
cout<<::n<<endl;        //print global variable:12
cout<<n<<endl;         //print the local variable:13
}

If the resolution operator is placed between the class name and the data member belonging   the class then data name belonging to the particularly class is affected.

If it is place front of the variable name then the global variable is affected. It is no resolution operator is placed then the local variable is affected. 

Constructor In C++

posted Jan 30, 2010, 9:17 PM by thiyagaraaj Mr   [ updated Aug 28, 2011, 6:32 AM ]

Simple Programs For Constructor

Introduction

  • It is special member function of the class.
  • It has same name of class.
  • It is automatically called when object(instance of class) create.
  • It must be a public member.
  • No Return Values.

Constructor General Form:

class class-name
{
    Access Specifier:
        Member-Variables
        Member-Functions
    public:
        class-name()
        {
            // Constructor code 
        }
        
        ... other Variables & Functions
}

Constructor Example:

Program:

class Example        {
    int a,b;
    public:

    //Constructor
    Example()            {
    a=10;
    b=20;
    cout<<"Im Constructor"
    }

    void Display()    {
    cout<<"Values :"<<a<<"\t"<<b;
    }
}

int main()                {
        Example Object;
        // Constructor invoked.
        Object.Display()
        return;
}

Output:

10    20
    

Parameterized Constructor:

Which constructor has arguments thats called Parameterized Constructor.

Parameterized Constructor General Form:

class class-name
{
    Access Specifier:
        Member-Variables
        Member-Functions
    public:
        class-name(type Variable,type Varibale2......)
        {
            // Constructor code 
        }
        
        ... other Variables & Functions
}

Parameterized Constructor Example:

Program:

class Example        {
    int a,b;
    public:

    //Constructor
    Example(int x,int y)            {
    a=x;
    b=y;
    cout<<"Im Constructor"
    }

    void Display()    {
    cout<<"Values :"<<a<<"\t"<<b;
    }
}

int main()                {
        Example Object(10,20);
        // Constructor invoked with parameters.
        Object.Display()
        return;
}

Output:

10    20

1-5 of 5