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