OOP Lab Solution 14 02022023 044639pm

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

OBJECT ORIENTED PROGRAMMING LAB

OBJECT ORIENTED
PROGRAMMING LAB
Lab Journal Solution -12
Question:01

Using template feature of C++, program a class ‘Calculator’ with two data members
of the user specified data type. The class should have a constructor to initialize
these data members and the following functions. (Both function definitions are to
be provided outside the class) .
a. A function to add the two data members b. A function to multiply the data
members
From your main program create objects of calculator for integer and float data
members, call these functions and display the results.

CODE:
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include <string.h>
#include <cstring>
using namespace std;
template<typename xyz>
class calculator
{
private:
xyz a;
xyz b;

public:
calculator(){
a = 0;
b = 0;
}
calculator(xyz x, xyz y){
a = x;
b = y;
}
xyz addition();
xyz multiply();

};
template<typename xyz>
xyz calculator<xyz>::addition(){
xyz add;
add = a + b;
return add;

1|Page
}
template<typename xyz>
xyz calculator<xyz>::multiply(){
xyz mul;
mul = a * b;
return mul;
}

int main()
{
calculator<int> C(5,8);
cout << "Addition : " << C.addition();
cout << "\nMultiplication : " << C.multiply();
calculator < float> F(3.5, 7.4);
cout << "\n\nAddition : " << F.addition();
cout << "\nMultiplication : " << F.multiply();
_getch();
return 0;
}

OUTPUT:

2|Page

You might also like