EXPERIMENT 2: Program to Understand  constructor and destructor in C++

Constructor:A constructor is kind of member function that initializes an instance                   of its class .A constructor has the same name as the class and no return values.

Download OOPs LAB in pdf: click here for download

Destructor:”Destructor is inverse of constructor function.they are called when object are destroyed.Designate a function as a class detructor by precending the class name with a tilde(~)”

Exp:Write a program in c++ to Program for find lowest using constructor and destructor.

#include<iostream>
using namespace std;

class A
{ int el[5];    int i=0;
public:
A()  // define constructor
{ cout<<”\nConstructor is called”;
cout<<“Enter five element:-“;
for(i=0;i<5;i++)
cin>>el[i];    } //    end of constructor
find_low()    {        int lowest=0;
lowest=el[0];
for(i=0;i<5;i++)
{        if(lowest>el[i])
lowest=el[i];        }
cout<<“lowest no is:-“<<lowest;
}
~A()    //defining destructor
{  cout<<”\nDestructor is called”
}     //end of destructor
};//end of class

int main()

{          A obj1;
obj1.find_low();
return 0;

}

OUTPUT: Constructor is called
Enter five element 5 7 8 1 9
lowest no is 1
Destructor is called

Tagged .

Leave a Reply

Your email address will not be published. Required fields are marked *