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