Showing posts with label constructor. Show all posts
Showing posts with label constructor. Show all posts

Monday, 9 June 2014

More about Constructors

                     Characteristics of Constructors

*They should be declared in the public section.
*They are invoked automatically when the objects are created.
*They do not have return types, not even void and they cannot return values.

*They cannot be inherited, though a derived class can call the base class constructor.
*Like other C++ functions, Constructors can have default arguments.
*Constructors can not be virtual.

*We can not refer to their addresses.

*An object with a constructor (or destructor) can not be used as a member of a union.

*They make ‘implicit calls’ to the operators new and delete when memory allocation is required.



.
They should be declared in the public section

Sunday, 8 June 2014

Types of Constructor

                There are three types of constructors in c++.
  1) Default constructor
   2)parametric constructor
   3)Copy constructor


Have a look on this simple program.


#include<conio.h>
#include<iostream>

using namespace std;

class student
{
private:
int marks;
double percentage;
public:
student()  // default constructor
{
marks=0;
percentage=0.0;
}
student(int a, double b)  // Parametric constructor
{
marks = a;
percentage = b;
}
student( student &s)  // copy constructor
{
marks=s.marks;
percentage=s.percentage;
}
void show()
{
cout<<"\n marks ="<<marks;
cout<<"\n percentage ="<<percentage;
}
};

int main()
{
student s1;  // calling default constructor
s1.show();
student s2(95,95.6);  // calling parametric constructor
s2.show();
student s3(s2);  // calling copy constructor
s3.show();
getch();
return 0;
}


Parametric Constructor: In order to initialize various data elements of different objects with different values when they are created. C++ permits us to achieve this objects by passing argument to the constructor function when the object are created . The constructor that can take arguments are called parameterized constructors



Copy constructor: A constructor is a constructor that creates a new object using an existing object of the same class and initializes each data member of newly created object with corresponding data member of existing object passed as argument. since it creates a copy of an existing object so it is called copy constructor.

 

Wednesday, 4 June 2014

Constructor in Derived Class

 
#include<iostream>
#include<conio.h>
using namespace std;
class alpha
{
    int i;
    public:
        alpha(int z)
        {
            i=z;
        }   
        void show_1()
        {
            cout<<i<<endl;
        }
};
class beta
{
    int j;
    public:
        beta(int x)
        {
            j=x;
        }
        void show_2()
        {
            cout<<j<<endl;
        }
};
class gamma : public beta , public alpha
{
    int m,n;
    public:
        gamma(int a, int b, int c, int d):alpha(a), beta(b)
        {
            m=c;
            n=d;
           
        }
        void show_3(void)
        {
            cout<<"value of m is"<<"     "<<m<<endl;
            cout<<"value of n is"<<"     "<<n<<endl;
        }
};
int main()
{
    gamma g(5,10,75,20);
    g.show_1();
    g.show_2();
    g.show_3();
    getch();
    return 0;
}