Question

Create a class calc with two data members (num1 & num2). The class should have a...

Create a class calc with two data members (num1 & num2). The class should have a constructor that receives arguments during object creation and sets the values of the members. The class should also have a function called divide() that divides the two numbers and returns the result. The function should throw an exception if the denominator is 0.

Create two objects of the class in the main() function. Use class templates making the first object an int and the second object a float. If an exception is thrown, an error statement should be displayed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <iostream>

using namespace std;

template <typename T>

class Calc

{

private:

T a, b;

public:

Calc();

Calc(T aa, T bb);

T divide();

};

template <typename T>

Calc<T>::Calc()

{

a = b = 1;

}

template <typename T>

Calc<T>::Calc(T aa, T bb)

{

a = aa;

b = bb;

}

template <typename T>

T Calc<T>::divide()

{

if (b == 0)

throw "Divisible by zero";

return a / b;

}

int main()

{

Calc<int> c1(10, 2);

Calc<float> c2(5, 0);

cout << "Result : " << c1.divide() << endl;

try

{

cout << "Result : " << c2.divide() << endl;

}

catch (const char *error)

{

cout << error << endl;

}

return 0;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Create a class calc with two data members (num1 & num2). The class should have a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • QUESTION 3 (20) Create a constructor class Sum that has public integer variables num1 and num2....

    QUESTION 3 (20) Create a constructor class Sum that has public integer variables num1 and num2. Include a default constructor and a method Add that accepts the two integer parameters, calculates and outputs the sum. In your main class Calculate include a main method that prompts the user to enter 2 integer values and create a reference called total for the constructor Sum. Output the values entered by the user and the sum of the values in the command line...

  • Write a class called TemperatureFile. • The class has one data attribute: __filename • Write get...

    Write a class called TemperatureFile. • The class has one data attribute: __filename • Write getter and setter for the data attribute. • Write a “calculateAverage” function (signature below) to calculate and return average temperature of all temperatures in the file. def calculateAverage(self): • Handle possible exceptions Write a main function: • Create a file ('Temperatures.txt’) and then use “write” method to write temperature values on each line. • Create an object of the TemperaureFile with the filename (‘Temperatures.txt’) just...

  • Create an Inventory class with data members for stock number, quantity, and price, and overloaded data...

    Create an Inventory class with data members for stock number, quantity, and price, and overloaded data entry and output operators. The data entry operator function should throw: - An error message, if the stock number is negative or higher than 999 - The quantity, if it is less than 0 - The price, if it is over $100.00 Then perform the following tasks: a. Write a main() function that instantiates an array of five Inventory objects, and accepts data for...

  • Create a class called planet with private members name, percent02, temp. Create a constructor prototype and...

    Create a class called planet with private members name, percent02, temp. Create a constructor prototype and implementation. In the main, create 2 objects of class planet, one with arguments and the other without. Create a function to change name, percent02 and temp after the object has been created and a function that displays the current values of the members. Implement the operations specified above in the main of your program. This is all in C++, please help.

  • Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input...

    Create a CalculatorTest class that contains the main method. 1. Get two double numbers as input and create an object of the ScientificCalculator Class with those two numbers. 2. Then call the 8 operations one by one sequentially and display the result. Code must be written in Java. (Sample input/output: Enter number 1: 2 Enter number 2: 3 Add() result is : 5 Sub() result is: -1 ……. Power() result is: 8 …… sqrtNum1 result is:) The Problem: Given the...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • please HELP!! I have been trying to get help for this problem forever! Create a class...

    please HELP!! I have been trying to get help for this problem forever! Create a class called planet with private members name, percent02, temp. Create a constructor prototype and implementation. In the main, create 2 objects of class planet, one with arguments and the other without. Create a function to change name, percent02 and temp after the object has been created and a function that displays the current values of the members. Implement the operations specified above in the main...

  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • C++ Define the class HotelRoom. The class has the following private data members: the room number...

    C++ Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [3pts]. The constructors and the set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT