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