Please help!!!!
Write a program to show the working of constructor and destructor.
Answers
Answered by
5
Heya friend!!
#include<iostream.h>
#include<conio.h>
class add
{
private:
int num1 , num2 , num3 ;
public:
add ( int = 0 , int = 0);
void sum();
void display ();
~ add (void);
};
add :: ~ add (void)
{
num1 = num2 = num3 = 0;
cout<<"\n After the final execution, me, the object"<< "has entered in the"<<"\n destructor to destroy myself \n" ;
}
add :: add (int n-1 , int n2)
{
num1 = n1;
num2 = n2;
num3= 0;
}
void add :: sum ()
{
num3 = num1 + num2;
}
void add :: display ()
{
cout<<"\n The sum of two numbers is"<<num3<<endl;
}
void main()
{
add obj 1, obj 2(5), obj 3(10,20);
clrscr();
obj1. sum ();
obj2.sum ();
obj3.sum();
cout<<"\n Using obj1 \n";
obj1.display();
cout<<"\n Using obj2 \n";
obj2.display();
cout<<"\n Using obj3 \n";
obj3.display();
getch();
}
HOPE IT HELPS......
#RUHANIKA
#include<iostream.h>
#include<conio.h>
class add
{
private:
int num1 , num2 , num3 ;
public:
add ( int = 0 , int = 0);
void sum();
void display ();
~ add (void);
};
add :: ~ add (void)
{
num1 = num2 = num3 = 0;
cout<<"\n After the final execution, me, the object"<< "has entered in the"<<"\n destructor to destroy myself \n" ;
}
add :: add (int n-1 , int n2)
{
num1 = n1;
num2 = n2;
num3= 0;
}
void add :: sum ()
{
num3 = num1 + num2;
}
void add :: display ()
{
cout<<"\n The sum of two numbers is"<<num3<<endl;
}
void main()
{
add obj 1, obj 2(5), obj 3(10,20);
clrscr();
obj1. sum ();
obj2.sum ();
obj3.sum();
cout<<"\n Using obj1 \n";
obj1.display();
cout<<"\n Using obj2 \n";
obj2.display();
cout<<"\n Using obj3 \n";
obj3.display();
getch();
}
HOPE IT HELPS......
#RUHANIKA
computer11:
thank you
Answered by
0
#include <iostream.h>
#include<conio.h>
class MyClass
{
public:
int x;
MyClass(); // constructor
~MyClass(); // destructor
};
// Implement MyClassconstructor.
MyClass::MyClass()
{
x = 10;
}
// Implement MyClass destructor.
MyClass::~MyClass()
{
cout<< "Destructing ...\n";
}
void main()
{
clrscr();
MyClass ob1;
MyClass ob2;
cout <<ob1.x<< " " <<ob2.x<<"\n";
getch();
}
output:
10 10
#include<conio.h>
class MyClass
{
public:
int x;
MyClass(); // constructor
~MyClass(); // destructor
};
// Implement MyClassconstructor.
MyClass::MyClass()
{
x = 10;
}
// Implement MyClass destructor.
MyClass::~MyClass()
{
cout<< "Destructing ...\n";
}
void main()
{
clrscr();
MyClass ob1;
MyClass ob2;
cout <<ob1.x<< " " <<ob2.x<<"\n";
getch();
}
output:
10 10
Similar questions