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