Search This Blog

Friday, December 10, 2010

A very first program in c++ with the concept of class

include
using namespace std;

class person
{
private:
   char name[20];
   int age;

public:
    void getdata(void);
    void dislpay(void);
};
void person :: getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void person :: display(void)
{
cout<<"\nName: "<
cout<<"\nAge: "<
}
int main()
{
  person p;

  p.getdata();
  p.display();

  return 0;
}

Differences between object-oriented programming (OOP) and procedure-oriented programming (POP)



                          POP
                             OOP

1. Emphasis on doing things (procedure).

1. Emphasis on data rather than procedure.
2. Programs are divided into what are            known as functions.
2. Programs are divided into what are known as objects.
3. Data move openly around the system from function to function.
3. Data is hidden and cannot be access by external functions.
4. Employs top down approach in the program design.
4. Employs bottom up approach in program design.

Monday, December 6, 2010

Copy constructor

Copy constructor is a type of constructor which constructs an object by copying the state from another object of the same class. Whenever an object is copied, another object is created and in this process the copy constructor gets called. If the class of the object being copied is x, the copy constructor’s signature is usually x::x (const x&).
Let’s take an example to illustrate this:
class Complex
{
int real, img;
public:
Complex (int, int);
Complex (const Complex& source); //copy constructor
};
Complex:: Complex (const Complex& source)
{
this.real = source.real;
this.img = source.img;
}
main ()
{
Complex a (2, 3);
Complex b = a; // this invokes the copy constructor
}
A copy constructor is called whenever an object is passed by value, returned by value or explicitly copied.