You must Sign In to post a response.
  • Category: Programming

    Use of constructor and destructor functions using C++


    Are you preparing for BCA exam? Want a programming solution for finding sum of two complex numbers and definition of constructor and destructor? on this page our ISC experts shall resolve your query.

    Below is the question which was asked in a question paper of BCA part-III(CS_72) Annual Exam 2015 of Nalanda Open University. This question carries 25 marks and hence require brief explanation and program also.

    Q) Write a C++ program to find the sum of two complex numbers. Define proper constructor and destructor along with methods.
  • Answers

    1 Answers found.
  • Constructor is a new concept provided by OOP. Constructor is a member function of class who's name is same as that of class. This function is called as constructor because it constructs the data members of the object whenever object of the class is created. It is a dynamic initialization of data members of object.
    As constructor constructs the data members of object and memory is allocated for the data members of the object. After execution of program this memory which is allotted by constructor must be released for further use. This task is done by destructor. Destructor is also a member function of a class and its name is also same as class name but a tilde sign is present before function name. Opposite to constructor, destructor is used to destroy the objects constructed by constructor. Destructor is called upon exit from the program or upon exit from the function.

    Below is the program to add two complex numbers using constructor and destructor.
    Please include iostream.h and conio.h header files before class declaration.
    //PROGRAM TO ADD TWO COMPLEX NUMBERS USING CONSTRUCTOR AND DESTRUCTOR.
    #include
    #include
    class complex
    {
    private:
    float real,imag;
    static int c;
    public:
    complex(){++c;}
    complex(float r, float i)
    {
    real=r;
    imag=i;
    ++c;
    }
    void display(void)
    {
    cout< }
    complex sum(complex x)
    {
    complex temp;
    temp.real=real+x.real;
    temp.imag=imag+x.imag;
    return(temp);
    }

    ~complex()
    {
    cout< --c;
    }
    };
    int complex::c=0;
    void main(void)
    {
    class complex c1(1.2,3.4);
    class complex c2(9.8,6.7);
    class complex c3;
    clrscr();
    cout<<"\nFirst complex number is \n";
    c1.display();
    cout<<"\nSecond complex number is \n";
    c2.display();
    c3=c1.sum(c2);
    cout<<"\nAddition of complex number is \n";
    c3.display();
    getch();
    }
    In above program one static member variable is used in a class. Static variable though used in class private visibility but is not part of every object. Only one copy of static variable is made and it is used by all objects of class. In above program this static variable which is c is used to hold the count of object created or destroyed.


  • Sign In to post your comments