20121022

Constructors in C++/Copy Constructor in CPP

Constructors in C++/Copy Constructor in CPP:
What is constructor?
Constructor is a member function whose task is to initialize the object of a class and  which is automatically gets called when object of that class gets created.
Why do we need constructor?
we need constructor to initialize the objects of class at the time of their creation.
What is Copy Constructor?
Copy Constructor is a constructor which gets called when an object is passed by value as argement or when an object is returnrd by value by a function or when an object is initialize by another object.
Why do we need Copy Constructor?
We need copy constructor to avoid shallow or member wise copy done by the the compy constructor provided by compiler.
for example:

Previous                             Home                               Next

20121020

How to use template in cpp

How to use template in C++:
Template is mechanism in C++ to generate fanily of
 generic classes and generic functions that works with
different types of data types hence eliminate duplicate
 program for diffrent data types and make the program
 more manageable and easier.

C++ Program for 1 argument template class

#include<iostream.h>
#include<string.h>
template <class T>
class test
{
T t;
public:
test(T x)//will work with int, float,char,string
{
t=x;
}
 void show(void)
 {
  cout<<"t= "<<t<<endl;
 }
};
//void test< T>::show(void)
//{
//cout<<"t"<<t<<endl;
//}
int main()
{
test<int>t1(10); //calls int version of test(T t)
t1.show();
test<float>t2(1.0);  //calls float version of test(T t)
t2.show();
test<char>t3('m');   //calls char version of test(T t)
t3.show();
test<char *>t4("HelloTemplate"); // calls string version of test(T t)
t4.show();
return 0;
}


C++ Program for 1 argument template function

#include<iostream.h>
//using namespace std
template <class T>
void display(T a)
{
cout<<a<<endl;
}
int main()
{
display(10);
display(20.12);
display('M');
display("MamaMeaa");
return 0;

}



Previous                             Home                               Next

20121002

Why to use explicit keyword in C++

//explicit keyword is used only with constructor
// which  avoids conversion only it will allow to create objects
 #include<iostream.h>
class sample
{
private:
int i;
public:
sample(){}
// explicit sample(int ii)
 sample(int ii)
{
i=ii;
}

sample operator+(sample s)
{
sample temp;
temp.i=i+s.i;
return temp ;
}
void display()
{
cout<<i<<endl;
}
};
int main()
{
sample s1(25), s2;
s1.display();
s2=s1+25;
s2.display();

return 0;
}
//How to use explicit keyword in C++ with exaple
//How to use explicit keyword in C++  with exaple
//How to use explicit keyword in C++  with exaple
//Why to use explicit keyword in C++  with exaple
//Why to use explicit keyword in C++  with exaple

//overloading S2=S1+25;


Previous                             Home                               Next

20121001

How to use new and delete in C++

//C++ program to use new //How to use new and delete in C++
int main()
{
// allocate memory for the string
//char *string1 = (char *) malloc(100*sizeof(char));
char *string1;
string1=new char;

// write some data to the string
strcpy(string1, "Hello String world xyz abcdef zxy!");

// display the string
//printf("%s\n", string1);
cout<<"String is:"<<string1<<endl;

// remove the string from memory
//delete string1;
return 0;
}
//C++ program to use new //How to use new and delete in C++
//C++ program to use new //How to use new and delete in C++

//Program in c++ to learn new and delete with example
//how to use new  and delete with example in C++
#include
int main()
{
char name[10]="xyz";
cout<<("\n You entered: \n", name);

char *p;
p=new char[strlen(name)]- 1;// Note the Sysntax
strcpy(p,name);
cout<<("\n copied string is: \n", name);

return 0;
}


Previous                             Home                               Next

20120930

Getter and Setter Methods in C++

How to Use Getter and Setter Methods in C++
In C++ Getter and Setter Methods are used to set the value of data members/variable and return the value of the data member. Its a very common technique in object oriented programming for setting and returning the values of variable.
Example Getter and Setter Methods in C++
#include<iostream>
using namespace std;

class getsetDemo
{
   private:
       int age;
   public:
       void getData()
          {
            return age;
          }
      void  setData(int a)
             {
               age=a;
              }
int main()
{
getsetDemo gs1(10) ;
cout<<"Age is"<<setData();
return 0;
}


Getter and Setter Methods makes the program more readable for the programmers who uses a previously written program. For changing the value of a variable he does not need to search for the variables. Simply search the setter method and edit the variable value which much convenient.
Previous                             Home                               Next

20120831

C++ Program to Add Two Numbers using Class

//Cpp Program to Add Two Numbers using Class

     #include <iostream>
     using namespace std;
        class SumWithClassDemo
        {
            public :
             void Add(int a,int b)
            {
            int total;
            total=a+b;
            cout<<"Total= "<<total;
            }
        };

        int main()
        {
        int x=3,y=4;
        SumWithClassDemo s;
        s.Add(x,y);
        return 0;
        }


//=====Program 2=====
//Cpp Program to Add Two Numbers using Class
//==================

#include <iostream>
using namespace std;

int main()
{
int x;
int y;
int sum;
         
   cout << "Insert two numbers to be added: "<<endl;
    cin >> x;
    cout<<"Enter second No: "<<endl;
    cin>> y;

   sum = x + y;
   cout <<"Sum is: "<<sum<<endl;
   return 0;
}

Simplest Way to learn Data Structure in C++ CPP or C

C++ Program for Fibonacci series

//C++ Program for Fibonacci series
//cpp program to find fibonacci series
#include<iostream>
using namespace std;
int main()
{
   int n, first = 0, second = 1, next;
   cout << "Enter the number of terms of Fibonacci series you want " << endl;
   cin >> n;
    cout << "Fibbonacci Series of first "<<n<<"numbers is = " <<endl;
    for ( int i = 0 ; i < n ; i++ )
       {
     if ( i <= 1 )
         next = i;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      cout<<next<<endl;
   }
   return 0;
}

//How to find fibonacci Series in C++/Cpp
//How to find fibonacci Series in C++/Cpp
//C++ program for fibonacci Number
 //How to find fibonacci Series in C++/Cpp
//How to find fibonacci Series in C++/Cpp
//C++ program for fibonacci Number

Previous                             Home                               Next

20120728

Simple Program for malloc in C

How to use malloc in C. Simple Program for malloc in C.

->malloc function allocates required amount of memory in heap (instead of stack) and
returns the pointer to the address of allocated block of memory.
->We need to type cast the returned pointer by malloc as it returns a void pointer.
->memory allocated by malloc in heap is deallocated by free, so that memory in heap can be
  allocated by other malloc functions.
->Syntax of malloc in C
datatype *p;
p=(datatype *)malloc(sazeof(datatype));
example of malloc:
int *p;
p=(int *)malloc(100*sizeof(int));

Explanation:
Here, malloc function allocates 400( 100*size of integer ie 100*4=400 ) bytes in heap and
assigns the address of the allocated memory in integer pointer p.
How to use malloc in C.
Simple Program for malloc in C.
#include<stdio.h>
int main
{
int *p;
p=(int *)malloc(sizeof(int));
*p=10;
printtf("%d",*p);
free(p);
return 0;
}
Previous                             Home                               Next

Is it necessary to accept an object by reference in copy constructor?

Is it necessary to accept a reference in copy constructor?
Yes, It is necessary to accept an object by reference in copy constructor because if an object is accepted by value then the copy constructor will fall in recursive loop.
C++ program for copy constructor:

#include<iostream>
using namespace std;
class copyDemo
{
int x;
public:
       copyDemo(copyDemo v) //taking object as value
        {
         x=v.x;
        }
};

int main()
{
copyDemo c;
copyDemo c1(c);// this will invoke copy constructor
return 0;
}

//explanation

The statement copyDemo c1(c);  causes the copy constructor to get called.
The value of c is passed If the copy constructor collects c in v by value then
the statement  copyDemo v would become as
copyDemo v=c;
here v is getting created and initialized so again copy constructor will get called and will go in recursive call loop.
Previous                             Home                               Next

Multiplication of two numbers using BITWISE operators

//How will you multiply two numbers using BITWISE operators
#include<stdio.h>
main()
{
   int a,b,result;
   printf("nEnter the numbers to be multiplied :");
   scanf("%d%d",&a,&b);
   result=0;
   while(b != 0)               // Iterate the loop till b==0
   {
      if (b&01)               // Logical ANDing of the value of b with 01
      result=result+a; // Update the result with the new value of a.
      a<<=1;              // Left shifting the value contained in 'a' by 1.
      b>>=1;             // Right shifting the value contained in 'b' by 1.
   }
   printf("nResult:%d",result);
}

//program to set and show bit at nth position

20120727

New Features in ANSI C++ Standard

How to Use New Features in ANSI C++ Standard.

1)      Newly added  data types in ANSI C++:
a)bool  data type
b)wchar_t data types
c) wide_character Literals
a)bool  data type:
bool  bb; //declarion of bool data type
bb=true;  //assignment of true value
bool  bb2 = false; //declaration and initialization(to true) simultaneously

b)wchar_t data types:
To represent the character sets of the languages that contains more than 255 characters.e.g. Japanese Language wchar_t data type is used that hold 16-bit wide characters. 
c) wide_character:
                                Wide character Literals begin  with the letter  L . It has 2 byte memory.
                                e.g.    L  ‘ms’ //example of wide character Literal.
         2)      New Operators:
A)     Static_cast operator
B)      Const_castoperator
C)      Reinterpret_cast operator
D)     Dynamic_cast operator
E)      Typeid operator

       3)      Namespace
       4)      Some old style header files are renamed.for example:
           Math.h file renamed as cmath in the new standard
Previous                             Home                               Next

20120718

Bitwise operation for division

// Bitwise operation for division

int main(void) {
int L, leftShift, i, changeMask;

leftShift = 0; /* added or subtracted value will be 2^leftShift */
i = 25; /* value we are adding to or subtracting from */
changeMask = 1 << leftShift;

for (L = leftShift; L < INT_BIT; L++) {
i ^= changeMask;
if ( /* ! */ (i & changeMask)) { /* comment in or out "!" for
addition or subtraction */
break;
}
changeMask <<= 1;
}

printf("%i", i);

return 0;
}

Previous                             Home                               Next

20120706

Compiler Terminologies



  • Compiling:  converting source code  into an executable.
  • Linking: converting  the compiled code into an executable
  • Building: Creating the end executable. Tools exist to help to reduce the complexity of the build process--makefiles, for instance.
  • Compiler: refers to both a compiler and a "linker"
  • Linker The program that generates the executable by linking

  • Previous                             Home                               Next

    20120124

    How to pass a object and return a object in C++/Cpp

    //How to pass a object and return a object in C++/Cpp
    //Program to show how to pass a object and return a object
    // How to use friend function in C++/ Cpp
    #include<iostream>
    using namespace std;
    class complex
    {
    private:
            float x;
            float y;
    public:
            void getdata(float real, float imag)
            {
            x=real;
            y=imag;
            }
            friend complex sum(complex, complex);
            void show(complex);
    };
    //defining friend function outside the class
     //does not require scop resolution operator
    complex sum(complex C1, complex C2)// friend function defined outside the class
    //passing objects as function arguments
    {
    complex C3;
    C3.x= C1.x + C2.x;
    C3.y= C1.y + C2.y;
    return(C); //returning object C

    How to acces private member function in C++/CPP

    How to acces private member function in C++/CPP
    Program to show accessibility of private member function.
    //private member function(skeleton) can be accessed using
    //member function only and not by object and dot operator
    #include<iostream>
    using namespace std;
    class item
    {
    private:
            int number;
            float price;
            void getdata(int a, float b) // here member function getdata
                                                   //is private so not accessible by object

    20120123

    Why destructor should be virtual in C++ ?

    Why destructor should be virtual in C++ ?
    Memory Leak ?

    When delete is called virtual destructor makes sure to release memory of first derived class object then of by base class object. If destructor is not virtual then only the base class object's memory get released and not that of derived class object.
    example:
    #include<iostream>
    using namespace std;
    class base
    {
      base(){};
     ~base(){};
    };
    class derived:public
    {
      derived(){};
     ~derived(){};
    };
    int main()
    {
    base *ptr= new derived(); //exp1
    delete ptr; //exp2
    }
    Explanation:
    Line exp1 makes the derived and base constructoes to be called to allocate memory for base as well derived class objects.
    But, if destructor is not virtual in base class then
    Line exp2 makes to call the base destructor and derived destructor never gets called and hence memory allocated to derived class object is not release and leads to memory leak because memory used by derived class object can not be used util the end of the program.
    Simples Way to learn Data Structure in C++ CPP or C
    C++ Program for Circular Queue implementation using linked list
     
    C++ program for Circular Queue implementation using Array

    C++ program for reversing single Linked list nodes 

    C++ Program for sorted single linked list with class

    C++ Program for BST-Binary Search Tree   


    Previous                             Home                               Next

    20120118

    How to use tricky while loop in CPP,C++,C

    #include<stdio.h>
    int main()
    {
    int i=0;
    while(i<5)
            {
            printf("\n Iteration No=%d",i);
            }
    return 0;
    }
    ////////////////////
    o/p = infinite times Iteration No=0 as we are not incrementing the i
    ///////////////////

    How to use for loop in C++ CPP C

    //==========================
    //What will be the out put in C++, C or CPP
    //1.Tricky for loop program
    ===========================
    #include<stdio.h>
    int main()
    {
    int i;
    for(i=0;i<=5;i++); //when i=6 condition will become false and
                                  //control flow will go next stmt to print once
            {

    20120113

    Program in C++ to learn virtual function

    //========================
    //Program in cpp to learn virtual function, vrtuual1.cpp
    //========================

    #include<iostream>
    using namespace std;
    class base
    {
    public:
          virtual   void func()
            {
            cout<<"base function \n";
            }
    };

    How to use Virtual function in C++

    //========================
    //Program in cpp to learn virtual function, virtu.cpp
    //========================

    How to use Virtual function C++

    //========================
    //Program in cpp to learn virtual function, vertuual1.cpp
    //========================

    Program to create a table in C

    //========================
    //Program in c to create a table of square , table.c
    //========================

    How to use Structures in C

    //========================
    //Program in Cto learn structures in C, struct4.c
    //========================
    // array of structures, collection of collection of hetrogeneous data types

    #include<stdio.h>
    int main()
    {
    struct book
            {
            char name;
            int pages;
            float price;
            };
            struct book b[5]; // b is array of 5 books
            int i;
            for(i=0;i<5;i++)

    How to use structures in C language

    //========================
    //Program in C to learn structures in C, struct3.c
    //========================

    Program in C to learn structures in C

    //========================
    //Program in C to learn structures in C, struct2.c
    //========================
    #include<stdio.h>
    int main()
    {
            struct book
            {
            char title;
            int page;
            float price;
            };
            struct book b1,b2,b3;
            printf("\n Enter the title, page and price for the first book \n");
            scanf("%c  %d  %f",&b1.title, &b1.page, &b1.price);
            printf("\n Details of first book are: %c %d %f \n",b1.title,b1.page,b1.price);

            printf("\n Enter the title, page and price for the Second book \n");
            scanf("%c  %d  %f",&b2.title, &b2.page, &b2.price);
            printf("\n Details of first book are: %c %d %f \n",b2.title,b2.page,b2.price);
            printf("\n Enter the title, page and price for the third book \n");
            scanf("%c  %d  %f",&b3.title, &b3.page, &b3.price);
            printf("\n Details of first book are: %c %d %f \n",b3.title,b3.page,b3.price);

    return 0;
    }
    Previous                             Home                               Next

    Program in C to learn structures in C

    //========================
    //Program in C to learn structures in C,struct1.c
    //========================

    Program in C++ to learn to reverse a strings in Cpp

    //========================
    //Program in C++ to learn to reverse a strings in Cpp, strrevcpp.cpp
    //========================

    Program in C++ to learn to reverse a strings in Cpp

    //========================
    //Program in Cpp to learn to reverse a strings in Cpp, strrevcpp3.cpp
    //========================

    Program in Cpp to learn to reverse a strings in Cpp

    //========================
    //Program in Cpp to learn to reverse a strings in Cpp, strrevcpp2.cpp
    //========================
    #include<iostream>
    using namespace std;
    #include<string.h>
    int main()
    {
    char orgstr[]="manoj";
    cout<<"orginal string is:\n"<<orgstr;
    char revstr[50];
    char *p1,*p2;
    p1=orgstr+strlen(orgstr)-1;
    p2=revstr;
    while(p1>=orgstr)
    *p2++=*p1--;
    *p2='\0';
    cout<<"\n reversed string is: \n"<<revstr;
    return 0;
    }


    Previous                             Home                               Next

    Program in C++ to learn to reverse a strings in Cpp

    //========================
    //Program in Cpp to learn to reverse a strings in Cpp, strrev.cpp
    //========================
    #include<iostream>
    using namespace std;
    int main()
    {
    char str[]="ProgramtoReverseString";
    int length= strlen(str);
    cout<<"***********Reversing a String******************";
    cout<<"\n The given String is: \n \t";
    for(int i=0;i<length;i++)
      {
       cout<<str[i];
      }
    //Reversing the String
    int length2=length/2;
    length--;
    for(int i=0;i<length2;i++)
       {
       str[i]=str[i]+str[length-i];
       str[length-i]=str[i]-str[length-i];
       str[i]=str[i]-str[length-i];
       }
     cout<<"\n The Reversed String is:\n  \t "<<str;
     cout<<"\n";
    return 0;
    }
    Previous                             Home                               Next

    Program in C to learn to reverse a strings in C

    //========================
    //Program in C to learn to reverse a strings in C,stringreverse.c
    //========================
    #include<stdio.h>
    #include<string.h>
    int main()
    {
    char name[]="SalmanKhan";
    printf("%s \n",name);
    strrev(name);
    printf("%s \n",name);
    return 0;
    }

    Previous                             Home                               Next

    Program in Cto learn strings using pointers in C

    //========================
    //Program in Cto learn strings pointers in C, stringptr.c
    //========================
    #include<stdio.h>
    int main()
    {
    char name[]="Karnighan";
    int *ptr;
    ptr=name;
            int i=0;
            while(*ptr != '\0')
            {
            printf("%c",*ptr);
            ptr++;
            }
    return 0;
    }


    Previous                             Home                               Next

    Program in C to learn strings in C language

    //========================
    //Program in C to learn strings in C, string2s.c
    //========================
    include<stdio.h>
    int main()
    {
    char name[]="Ritchie";
    printf("%s \n",name);
    return 0;
    }
    Previous                             Home                               Next

    Program in C to learn strings in

    //========================
    //Program in C to learn strings in C, string1.c
    //========================
    #include<stdio.h>
    int main()
    {
    char name[]="DannishRitchie";
    int i=0;
            while(name[i]!='\0')
            {
            printf("%c",name[i]);
            i++;// If not incrementing while loop then its a infite loop as it will never reach to \0
            }
    return 0;
    }

    Previous                             Home                               Next

    Program in C to learn static variable in C

    //========================
    //Program in C to learn static variable in C
    //========================
    #include<iostream>
    using namespace std;
    class base
    {
    public:
            static int x;
            void modifyx()
            {
            x++;
            }
            void displayx()
            {
            cout<<"\n x="<<x;
            }
    };
     int base::x=0;
    class derived:public base
    {
    };
    int main()
    {
    base b;
    derived d;
    b.modifyx();
    b.displayx();
    d.modifyx();
    d.displayx();
    return 0;
    }
    Previous                             Home                               Next

    Program in C to learn right shift

    Program in C to learn right shift, shiftright.c


    #include<stdio.h>
    int main()
    {
    printf("\n Bit wise left shift demo \n");
    unsigned int value=32;
    unsigned int shift=3;
    value=value>>shift;//32>>3 means 32/(2*2*2)//means 4/(3 times 2)
    printf("Result is: %d \n" ,value);
    return 0;
    }

    Previous                             Home                               Next

    Program in C to learn shift opertor

    //========================
    //Program in C to learn shift opertor, shiftop.c
    //========================
    #include<stdio.h>
    int main()
    {
    printf("\n Bit wise left shift demo \n");
    unsigned int value=4;
    unsigned int shift=3;
    value=value<<shift;//4<<3 means 4*(2*2*2)//means 4*(3 times 2)
    printf("Result is: %d \n" ,value);
    return 0;
    }

    Previous                             Home                               Next

    Program in C to reverse a string using pointer

    //========================
    Program in C to reverse a string using pointer, revstrusingptr.c

    #include<stdio.h>
    #include<string.h>
    int main()
    {
    char orgstr[50];
    char revstr[50];
    char *p1,*p2;
    printf("Enter the string to reverse:\n");
    scanf("%s",orgstr);
    printf("You entered: %s \n", orgstr);
    p1=orgstr+ strlen(orgstr)-1;
    p2=revstr;
    while(p1>=orgstr)
    *p2++ = *p1--;
    *p2='\0';
    printf("Reversed String is: %s \n",revstr);
    return 0;
    }



    Previous                             Home                               Next

    Program in C to reverse a string

    Program in C to reverse a string, revstr99.cpp

    #include<iostream>
    using namespace std;
    int main()
    {
    char orgstr[50];
    char revstr[50];
    cout<<"Enter a String: "<<endl;
    cin>>orgstr;
    char *p1,*p2;
    p1=orgstr+ strlen(orgstr)-1;
    p2=revstr;
    while(p1>p2)
            {
            *p2++ = *p1--;
            }
    *p2='\0';
    cout<<"Reversed String is:"<<revstr;
    cout<<endl;
    return 0;
    }


    Previous                             Home                               Next

    Program in C to reverse a string

    Program in C to reverse a string,Reversing a String

    #include<stdio.h>
    void reverse(char s[]);
    int main()
    {
    char text[50]="India";
    printf("\n Text is :%s",text);
    reverse(text);
    printf("\n Reversed Text is :%s \n",text);
    return 0;
    }
    void reverse(char s[])
    {
    int c,i,j;
    for(i=1,j=strlen(s)-1; i<j; i++,j--)
            {
            c=s[i];
            s[i]=s[j];
            s[j]=c;
            }
    }
    Previous                             Home                               Next

    Program in C to learn pointer in C


    Program in C to learn pointer in C, ptr1.cpp

    #include<iostream>
    using namespace std;
    int main()
    {
    int numbers[50],*ptr,n,i;
    cout<<"\n Enter the counts of number: \n";
    cin>>n;
     cout<<"Enter the numbers 1 by 1 \n";
     for(i=0;i<n;i++)
     cin>>numbers[i];
      //checking for the even numbers and adding
      ptr=numbers; //assigning the base address of the array numbers to ptr
      //ptr=numbers[0];
    int sum=0;
    for(i=0;i<n;i++)
     {
     if((*ptr%2)==0)
       {
       sum+=*ptr;
       ptr++;
       }
      }
            cout<<"Sum of even Numbers in the array numbers is:"<<sum;
            return 0;
    }

    Previous                             Home                               Next

    Program in C to learn preprocessor directive,

    Program in C to learn preprocessor directive, preprocessor.c

    #include<stdio.h>
    #define size 5
    int main()
    {
    int i=1;
    for(i=1;i<=5;i++)
    printf("\n %d \n", i);
    return 0;
    }
    Previous                             Home                               Next

    How to use Pointer Arithmatic in C++

    Program in c to learn pointer in C
    #include<iostream>
    using namespace std;
    int main()
    {
    int num[]={10,20,30,40,50};
    int *ptr1;
    ptr1=num;//assigning the base address of array of integer
    cout<<"Array elements are: \n";
    int i;
    for(i=0;i<5;i++)
    {
    cout<<num[i];
    cout<<"\n";
    }
    return 0;
    }
    Previous                             Home                               Next

    How to use pointer arithmatic in C++

    Program in c to learn pointer in C, pointerarithmatic2.cpp

    #include<iostream>
    using namespace std;
    int main()
    {
    int num[]={10,20,30,40,50};
    int *ptr1;
    ptr1=&num[0];//assigning the base address of array of integer
    //ptr=num;
    cout<<"Value of ptr is:"<<*ptr1<<"\n";
    ptr1++;
    cout<<"value of ptr1++ is \n"<<*ptr1<<"\n";
    ptr1++;
    cout<<"value of ptr1++ is \n"<<*ptr1<<"\n";

    ptr1--;
    cout<<"value of ptr1-- is \n"<<*ptr1<<"\n";
    cout<<"Now adding an integer to the pointer\n";
    ptr1=ptr1+2;
    cout<<"value of ptr1 +2 is \n"<<*ptr1<<"\n";

    cout<<"Now ubtraction 1 pointer from another \n";
    //ptr1=ptr1+2;

    return 0;
    }
    Previous                             Home                               Next

    How to learn Pointers in C++

    Program in c to learn pointer in C,pointer2.cpp

    #include<iostream>
    using namespace std;
    int main()
    {
    int a=10;
    int *ptr1, **ptr2; //note thatptr2 is ptr to ptr
    ptr1=&a;
    ptr2=&ptr1;
    cout<<"Value of a is : \n"<<a<<"\n";
    cout<<"Value of a is : \n"<<*ptr1<<"\n";

    cout<<"Manipulating the value of contained by pointer \n";
    *ptr1=(*ptr1)/2;
    cout<<"Value of a is : \n"<<a<<"\n";
    cout<<"Value of a is : \n"<<*ptr1<<"\n";
    //cout<<"Adress of a is : \n"<<ptr1<<"\n";
    //cout<<"Address of ptr1 is: \n"<<ptr2<<"\n";
    return 0;
    }

    Previous                             Home                               Next

    How to use pointer in C++


    //Program in c to learn pointer in C, pointer1.cpp

    #include<iostream>
    using namespace std;
    int main()
    {
    int *ptr1, **ptr2, a; //note thatptr2 is ptr to ptr
    ptr1=&a;
    ptr2=&ptr1;
    cout<<"Adress of a is : \n"<<ptr1<<"\n";
    cout<<"Address of ptr1 is: \n"<<ptr2<<"\n";
    return 0;
    }
    Previous                             Home                               Next

    How to initialization member in derived constructor

    Program in c to learn "member initialization example in derived constructor", memberil.cpp

    #include<iostream>
    using namespace std;
    class cycle
    {
    protected:
    int x;
    public:
    cycle(int a)
    {
    x=a;
    }
    };
    class bike
    {
    protected:
    int y;
    public:
    bike(int b)
    {
    y=b;
    }
    };
    class car:public cycle,public bike
    {
    protected:
    float m,n;
    public:
    car(int a, int b, float c, float d):cycle(a), bike(b)
    {
    m=c;
    n=d;
    }
    void display()
    {
    cout<<"*********member initialization example in derived constructor******"<<"\n";
    cout<<"x,y,z,m and n are initialized to"<<x<<" "<<y<<" "<<m<<" "<<n<<"\n";
    }
    };

    int main()
    {
    car g(1,2,3.7,4.7);
    g.display();
    return 0;
    }


    Previous                             Home                               Next

    How to use malloc function in C

    //Program in c to learn malloc function, mallocinc.c
    How to use malloc in C. Simple Program for malloc in C.

    ->malloc function allocates required amount of memory in heap (instead of stack) and
    returns the pointer to the address of allocated block of memory.
    ->We need to type cast the returned pointer by malloc as it returns a void pointer.
    ->memory allocated by malloc in heap is deallocated by free, so that memory in heap can be
      allocated by other malloc functions.
    ->Syntax of malloc in C
    datatype *p;
    p=(datatype *)malloc(sazeof(datatype));
    example of malloc:
    int *p;
    p=(int *)malloc(100*sizeof(int));

    Explanation:
    Here, malloc function allocates 400( 100*size of integer ie 100*4=400 ) bytes in heap and
    assigns the address of the allocated memory in integer pointer p.
    How to use malloc in C.
    Simple Program for malloc in C.
    #include<stdio.h>
    int main
    {
    int *p;
    p=(int *)malloc(sizeof(int));
    *p=10;
    printtf("%d",*p);
    free(p);
    return 0;
    }


    //Program in c to learn malloc function, mallocinc.c
    //Program in c to learn malloc function, mallocinc.c
    //Program in c to learn malloc function, mallocinc.c
    //Program in c to learn malloc function, mallocinc.c

    #include<stdio.h>
    int main()
    {
    char name[5];
    int i,len;
    char *p;
    char *pname[5];
    for(i=0;i<=5;i++)
            {
            printf("Enter a name: ");
            scanf("%s", name);
            len=strlen(name);
            p=(char *)malloc(len-1);
            strcpy(p,name);
            pname[i]=p;
            }

            for(i=0;i<=5;i++)
            {
            printf("%s",pname[i]);
            }
    return 0;
    }

    //How to use malloc function in C, mallocinc.c
    //How to use malloc function in C, mallocinc.c
    //How to use malloc function in C, mallocinc.c
    //How to use malloc function in C, mallocinc.c


    Previous                             Home                               Next

    How to use malloc in C

    //Program in c to learn malloc function, mallocinc2.c
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
    char name[10];
    printf("Enter a Name:");
    scanf("%s",name);
    printf("\n You entered: %s \n", name);
    char *p;
    p=(char *)malloc(sizeof(strlen(name)-1));
    strcpy(p,name);
    printf("Copied string is: %s \n",p);
    return 0;
    }

    Previous                             Home                               Next

    How to use fork in C++

    //Program in c to learn fork,linux1.cpp
    #include<iostream>
    using namespace std;
    int main()
    {
    fork();
    fork();
    fork();
    fork();
    fork();
    cout<<"Forked \n";
    return 0;
    }

    Previous                             Home                               Next

    How to use fork in C++


    //Program in c to learn fork, linux1.c

    #include<iostream>
    using namespace std;
    int main()
    {
    fork();
    fork();
    fork();
    fork();
    fork();
    cout<<"Forked";
    return 0;
    }
    Previous                             Home                               Next

    How to use Linked list in C++

    //Program in cpp to demonstrate linkedlist, linkedlist.cpp
    #include<iostream>
    using namespace std;
    struct node
            {
            public:
            int data;
            node * next;
            };
    class linkedlist
            {
            private:
            node* first;
            public:
            linkedlist()
                    {
                    first=NULL;
                    }
            void getdata(int d);
            void display();
            };
    void linkedlist::getdata(int dd)
            {
            node* newnode=new node;
            newnode->data=dd;
            newnode->next=first;
            first=newnode;
            }
    void linkedlist::display()
            {
            node* current=first;
            while(current!=NULL)
                    {
                    cout<<current->data<<endl;
                    current=current->next;
                    }
            }
    int main()
    {
    linkedlist li;
    li.getdata(10);
    li.getdata(15);
    li.getdata(20);
    li.getdata(18);
    li.getdata(19);
    li.display();
    return 0;
    }
    You may also Like:
    C++ Program for Circular Queue implementation using linked list  
    C++ program for Circular Queue implementation using Array
    C++ program for reversing single Linked list nodes 
    C++ Program for sorted single linked list with class
    C++ Program for BST-Binary Search Tree  

    Previous                             Home                               Next

    How to use Inheritance in C++

    //Program in cpp to demonstrate inheritance, inherit.cpp

    #include<iostream>
    using namespace std;
    class employee
    {
    protected:
    int empid;
    public:
    void getempid(int a)
    {
    empid=a;
    }
    void showempid()
    {
    cout<<"********* Details of employee**********"<<"\n";
    cout<<"Emp No is:"<<empid<<"\n";
    }
    };
    class hike:public virtual employee
    {
    protected:
    float hike1;
    float hike2;
    public:
    void getHike(float h1, float h2)
    {
    hike1=h1;//line 43
    hike2=h2;
    }
    void showHike(void)
    {
    cout<<"Hike in year 1 is :"<<hike1<<"\n";
    cout<<"Hike in year 2 is:"<<hike2<<"\n";
    }
    };
    class promotion:public virtual employee
    {
    protected:
    float promotion_score;
    public:
    void getPromotion(float s)
    {
    promotion_score=s;
    }
    void showPromotionScore(void)
    {
    cout<<"Score in promotion is:"<<promotion_score<<"\n";
    }
    };

    class result:public hike, public promotion
    {
    protected:
    float total;
    public:
    //total=hike1+hike2+promotion_score;
    void display();
    };
    void result::display()
    {
    total=hike1+hike2+promotion_score;
     showempid();
     showHike();
     showPromotionScore();
     cout<<"Total score is :"<<total<<"\n";
    }
    int main()
    {
    result Raheem;
    Manoj.getempid(111);
    Manoj.getHike(9.10, 9.5);
    Manoj.getPromotion(8.2);
    Manoj.display();
    return 0;
    }


    How to use Inheritance in Cpp, C++, C plus plus with example

    Previous                             Home                               Next

    How to use Inheritance in C++ Program


    //Program in cpp to demonstrate inheritance, inh2.cpp

    #include<iostream>
    using namespace std;
    class base
    {
    private:
            void prifun()
            {
            cout<<"\n private Function";
            }

    public:
            void pubfun()
            {
            cout<<"\n public  Function";
            }
    protected:
            void profun()
            {
            cout<<"\n protected Function";
            }
    };
    class derived:protected base
    {
    public:
            void fun()
            {
           //prifun();
            pubfun();
            profun();
            }
    };
    int main()
    {
    base b;
    derived d;
    //b.prifun();
    b.pubfun();
    //b.profun();
    //d.prifun();
    //d.pubfun();
    //d.profun();
    d.fun();
    return 0;
    }
    Previous                             Home                               Next

    How to use Inheritance in C++

    //========================
    //Program in cpp to demonstrate inheritance, inh1.cpp
    //========================
    #include<iostream>
    using namespace std;
    class base
    {
    private:
            void prifun()
            {
            cout<<"\n private Function";
            }

    public:
            void pubfun()
            {
            cout<<"\n public  Function";
            }
    protected:
            void profun()
            {
            cout<<"\n protected Function";
            }
    };
    class derived:public base
    {
    public:
            void fun()
            {
           //prifun();
            pubfun();
            profun();
            }
    };
    int main()
    {
    base b;
    derived d;
    //b.prifun();
    b.pubfun();
    //b.profun();
    //d.prifun();
    d.pubfun();
    //d.profun();
    d.fun();
    return 0;
    }

    How to use functions in C

    //========================
    //Program in C to display functions.c
    //========================
    extern int i;
    int func1()
    {
    i++;
    printf("\n i=%d",i);
    return 0;
    }
    int func2()
    {
    i++;
    printf("\n i=%d",i);
    return 0;
    }

    How to use function pointer in C++

    //========================
    //Program in C to display "function pointer", funcptr.cpp
    //========================
    #include<iostream>
    using namespace std;
    typedef void (* FunctionPointer)(int x, int y);
    void sumFunction(int i, int j)
    {
    cout<<i+j<<"\n";
    }
    void subtractionFun(int m, int n)
    {
    cout<<m-n<<"\n";
    }
    int main()
    {
    FunctionPointer ptr;
    ptr=&sumFunction;
    ptr(1,2);
    ptr=&subtractionFun;
    ptr(2,1);
    return 0;
    }
    How to use function pointer in CPP/CPP
    How to use function pointer in CPP/CPP
    How to use function pointer in CPP/CPP
    How to use function pointer in CPP/CPP

    How to use friend function in C++

    //Program in cpp to demonstrate friend1.cpp
    #include<iostream>
    using namespace std;
    class base;
    class anyClass
    {
    private:
    friend class base;
    anyClass()
    {}
    };
    class base
    {
    public:
            virtual anyClass *createanyClass()
            {
            return new anyClass;
            }
    };
    class derived:public base
    {
    public:
            anyClass *createanyClass()
            {
            //return new anyClass;// base is frnd of another doesnot mean derive                                   //
    class also will be friend  access private
            }
    };
    int main()
    {
    base b;
    derived d;
    b.createanyClass();
    return 0;
    }
    How to use friend function in Cpp/C++
    How to use friend function in Cpp/C++
    How to use friend function in Cpp/C++
    How to use friend function in Cpp/C++

    How to use fork in C++

    //========================
    //Program in cpp for "fork",  fork2.c
    //========================
    #include<iostream>
    using namespace std;
    extern char **environ;
    int main(int argc,char argv[])
    {
    cout<<"Hello World";
    cout<<"First argument is \n "<<argv[1];
    cout<<"Environment is \n "<<environ[0];
    cout<<"Environment is \n "<<environ[1];
    return 0;
    }

    How to use fork in C++

    //========================
    //Program in C++ to learn fork, fork1.cpp
    //========================
    #include<iostream>
    using namespace std;
    int main()
    {
    int pid;
    pid=fork();
    if(pid==0)
    {
    cout<<"Inside Child \n";
    cout<<"Child pid="<<pid;
    }
    else
    {
    cout<<"Inside PARENT \n";
    //cout<<"Parent's Pid="<<ppid;
    }
    return 0;
    }

    Program in C to learn "for loop", for3.c

    //========================
    //Program in C to learn  "for loop", for3.c
    //========================
    #include<iostream>
    using namespace std;
    int main()
    {
    int i=0;
    while(i<10)
            {
            i++;
            continue;
            cout<<i<<"\t ";
            }
    return 0;
    }

    Program in C++ to learn "for loop", for2.cpp

    //========================
    //Program in C++ to learn  "for loop", for2.cpp
    //========================
    #include<iostream>
    using namespace std;
    int main()
    {
    int i=0;
    while(i<10)
            {
            i++;
            cout<<i<<"\t ";
            if(i==5)
            break;
            }
    return 0;
    }

    Program in C to say hello, cdemo.c

    //========================
    //Program in C to say hello, cdemo.c
    //========================
    #include<stdio.h>
    int main()
    {
    printf("\n Hello \n");
    return 0;
    }

    //========================
    //Program in c to display for loop, for1.cpp
    //========================
    #include<iostream>
    using namespace std;
    int main()
    {
    int i, max=10;
    //for(i=1;i<=max;i++);
    for(i=1;i<=max;i++)
            {
            cout<<i<<"\t ";
            }
    return 0;
    }

    Program in C to set nth bit, bitset.c

    //========================
    //Program in C to set nth bit, bitset.c
    //========================
    #include<stdio.h>
    int showbits(int nn)
    {
    unsigned int m;
    m=1<<(sizeof(nn)*8-1);
            while(m > 0)
            {
                    if(nn & m)
                    {
                    printf("1");
                    }
                    else
                    {
                    printf("0");
                    }
            m>>=1;
            }
    }
    int main()
    {
    int number,bits;
    printf("Enter a nomber and bits to set:\n");
    scanf("%d%d",&number,&bits);
    printf("\n You Entered: %d and %d  \n",number,bits);
    showbits(number);
    number=number^(1<<(bits-1));
    printf("\n Number now is: %d \n",number);
    showbits(number);
    printf("\n");
    return 0;
    }

    Program in C to manipulate bits,bitmanip.c

    //========================
    //Program in C to manipulate bits,bitmanip.c
    //========================
    #include<stdio.h>
    int main()
    {
    int n,b;
    printf("\n Enter a number and bit to manipulate: \n");
    scanf("%d%d",&n,&b);
    printf("You entered %d and %d",n,b);
    //Code to Set bth bit in number n
    n=n|(1<<b);
    //to toggle //n=n^(1<<b)
    //to display bth bit// n&(1<<b)
    //to clear bth bit // n& ~(1<<b)
    printf("\n Entered Number after setting %d th bit is %d \n :",b,n);
    return 0;
    }

    Program in C to check bit at nth position, bitcheckatn.c

    //========================
    //Program in C to check bit at nth position, bitcheckatn.c
    //========================
    #include<stdio.h>
    int showbits(int nn)
    {
    unsigned int m;
    m=1<<(sizeof(nn)*8-1);
            while(m > 0)
            {
                    if(nn & m)
                    {
                    printf("1");
                    }
                    else
                    {
                    printf("0");
                    }
            m>>=1;
            }
    }
    int main()
    {
    int number,bits;
    printf("Enter a nomber and bits to see qt bit th position:\n");
    scanf("%d%d",&number,&bits);
    printf("\n You Entered: %d and %d  \n",number,bits);
    showbits(number);
    number=number&(1<<(bits));
    printf("\n Now number is :%d and \n bit at %d  position is: \n",number,bits);
    showbits(number);
    printf("\n");
    return 0;
    }

    Program in C to learn bitwise and operator

    //========================
    //Program in C to learn bitwise and operator
    //========================
    #include<stdio.h>
    int main()
    {
    printf("\n Bit wise and demo \n");
    unsigned int a=60;
    unsigned int b=13;
    unsigned int c=0;
    c=a & b;
    printf("Result to display is: %d \n" ,c);
    return 0;
    }

    Program in C++ to learn array of pointer, arrofptr.cpp

    //========================
    //Program in C++ to learn  array of pointer, arrofptr.cpp
    //========================
    #include<iostream>
    using namespace std;
    #include<string.h>
    #include<ctype.h>
    int main()
    {
    cout<<"\n declaring array of pointer \n";
    char * arrptr[10]={"internet", "net", "Music", "TV"};
    // above arrptr is array of 10 pointers to character elements
    char arrchar[15];
    cout<<"Enter ur favorite time pass: \n";
    cin>>arrchar;
    int i;
    for(i=0;i<4;i++)
    {
            if(!strcmp(*arrptr[i], arrchar))
            {
            cout<<"Your fav time pass is available here"<<"\n";
            break;
            }
    }
    //cout<<"Adress of a is : \n"<<ptr1<<"\n";
    //cout<<"Address of ptr1 is: \n"<<ptr2<<"\n";
    return 0;
    }

    Program in C++ to access array of pointer to object, arrofptr2obj.cpp

    //========================
    //Program in C++ to access array of pointer to object, arrofptr2obj.cpp
    //========================
    //Array of pointer to object of class person
    //pros: Dont need to know the number of objects to be created
    //cons: Need to give a fixed size of array// solution is linked list
    #include<iostream>
    using namespace std;
    class person
    {
    protected:
            char name[50];
    public:
            void setname()
            {
            cout<<"\n Enter Name of the :";
            cin >> name;
            }
            void showname()
            {
            cout<<"\n Name of the person is: ";
            cout<<name;
            }
    };
    int main()
    {
    person* ptrpers[100];//array of pointer to object of class person
    int n=0;
    char option;
    do{
            ptrpers[n]= new person;//creating 1 object run time
            ptrpers[n]->setname();
            n++;
            cout<<"Enter 1 more person's name (y/n)"<<"\n";
            cin>>option;
    }while(option=='y');
    //displaying the names
    for(int j=0;j<n;j++)
            {
            cout<<"\n Person Number:"<<j+1;
            ptrpers[j]->showname();
            }
    cout<<endl;
    return 0;
    }

    Program in C to display array elements by pointer, arreltbyptr.c

    //========================
    //Program in C to display array elements by pointer, arreltbyptr.c
    //========================
    #include<stdio.h>
    int main()
    {
    int i;
    int arr[10]={10,11,12,14,17};
    int *j;
    j=&arr[0];
    for(i=0;i<5;i++)
    {
    printf("\n array elt at position %d  is: %d",i,*j);
    //printf("\n array elt at position %d  is: %d",i,arr[i]);
    j++;
    }
    return 0;
    }

    Program in Cto display array elements using pointer, arrayinfunc.c

    //========================
    //Program in Cto display array elements using pointer, arrayinfunc.c
    //========================
    #include<stdio.h>
    void display(int *, int);
    int main()
    {
    int i;
    int arr[5]={11,22,33,44,55};
    display(&arr[0],5);
    return 0;
    }
    void display(int *j, int n)
    {
    int i;
    for(i=0;i<5;i++)
    {
    printf("\n Elements are:%d",*j);
    j++;
    }
    }

    Program in Cto display array elements

    //========================
    //Program in Cto display array elements
    //========================
    #include<stdio.h>
    void display(int *);
    int main()
    {
            int i;
            int  marks[]={11,10,20,95,98,50,98};
            for(i=0;i<=6;i++)
                    display(&marks[i]);
            //return 0;
    }
    void display(int *m)
    {
            printf("\n %d",*m);
    }

    Program in C to display marks in an array

    //========================
    //Program in C to display marks in array
    //========================
    #include<stdio.h>
    void display(int);
    int main()
    {
            int i;
            int  marks[]={11,10,20,95,98,50,98};
            for(i=0;i<=6;i++)
                    display(marks[i]);
            //return 0;
    }
    void display(int m)
    {
            printf("\n %d",m);
    }

    Program in C to calculate average of marks in an array

    //Program in C to calculate average of marks in an array
    #include<stdio.h>
    int main()
    {
            int i,sum=0,avg;
            int  marks[5];
            printf("\n Enter marks for 5 ppl:\n");
            for(i=0;i<5;i++)
            {
            scanf("%d",&marks[i]);
            }
            for(i=0;i<5;i++)
            {
            sum=sum+marks[i];
            }
            avg=sum/5;
            printf("\n svg is =%d",avg);
    }


    Simples Way to learn Data Structure in C++ CPP or C

    Program in C to demonstrate 2-D array

    //========================
    //Program in C to demonstrate 2-D array
    //========================
    #include<stdio.h>
    int main()
    {
    int arr[4][2]={ {00,01},{10,11},{20,21},{30,31} };
    int i=0;
    for(i=0;i<4;i++)
    printf("\n %3d%3d",arr[i][0], arr[i][1]);
    return 0;
    }

    Program in c to display third element of an array

    //====================
    //Program in c to display Third elt of an array
    //====================
    #include<stdio.h>
    int main()
    {
    int arr[5];
    arr[3]=100;
    printf("\n Third element of array arr is %d \n ",arr[3]);
    return 0;
    }

    20120105

    Starting Up My First Technical Blog

    Starting Up My First Technical Blog:

    This blog will be updated on day to day basis.
    Come and put your comments and your programs.
    This blog will help for quick reference to C
    and C++ Concepts online.