program for displaying average of 2 given numbers
In this, we are going to see how to display Average of Two given Numbers using Constructor and Destructor, in C++.
 


The Code given below can be used in TURBO C++ Compilers: -

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

class number
{
private:
    float x, y, avg;

public:
    number(float num1, float num2);
    int calc();
    int display();
    ~number();
};

number::number(float num1, float num2)
{
    cout << "Constructor is called.\n";
    x = num1;
    y = num2;
}

number::calc()
{
    avg = (x + y) / 2;
}

number::display()
{
    cout << "\nAverage of the entered number is :- " << avg << endl;
}

number::~number()
{
    cout << "\nDESTRUCTOR is called.";
}<<

void main()
{
    clrscr();
    float x, y;
    cout << "Enter 2 number:-\n";
    cin >> x >> y;

    number ag(x, y);
    ag.calc();
    ag.display();

    getch();
}

//..........Coded by YASH ALAPURIA


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;

class number
{
private:
    float x, y, avg;

public:
    number(float num1, float num2);
    int calc();
    int display();
    ~number();
};

number::number(float num1, float num2)
{
    cout << "Constructor is called.\n";
    x = num1;
    y = num2;
}

number::calc()
{
    avg = (x + y) / 2;
}

number::display()
{
    cout << "\nAverage of the entered number is :- " << avg << endl;
}

number::~number()
{
    cout << "\nDESTRUCTOR is called.";
}

int main()
{
    system("cls");
    float x, y;
    cout << "Enter 2 number:-\n";
    cin >> x >> y;

    number ag(x, y);
    ag.calc();
    ag.display();

    return 0;
}

//..........Coded by YASH ALAPURIA


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post