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