Question

using c++ Please create a class "Circle" that - uses template type as data type for...

using c++ Please create a class "Circle" that -

uses template type as data type for private data member radius.

overrides "operator>>" to take a value from user input and assign it to radius', when taking a Circle object as the right operand. (friend function)

overrides "operator<<" to display radius of the circle object and its area, when taking a Circle object as the right operand.(friend function)

overrides binary "operator+" that takes an int parameter to increment the Circle object's radius by the int argument value. (member function)

Please put some test code in main() to make sure everything you have defined works.

You may try to put int and double as the radius data type.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

class Circle {
private:
    double radius;
public:
    friend ostream &operator<<(ostream &out, const Circle &c);
    friend istream &operator>>(istream &in, Circle &c);

    Circle &operator+(int value) {
        radius += value;
        return *this;
    }
};

ostream &operator<<(ostream &out, const Circle &c) {
    out << "Radius: " << c.radius << " and Area: " << 3.14159 * c.radius * c.radius;
    return out;
}

istream &operator>>(istream &in, Circle &c) {
    cout << "Enter radius: ";
    in >> c.radius;
    return in;
}

int main() {
    Circle c;
    cin >> c;
    cout << c << endl;
    c = c + 10;
    cout << c << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
using c++ Please create a class "Circle" that - uses template type as data type for...
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
  • In a function template, a generic data type starts with the key word _____ followed by...

    In a function template, a generic data type starts with the key word _____ followed by a parameter name that stands for the data type. template class T function Which of the following blocks is designed to catch any type of exception? catch(){ } catch(...){ } catch(*){ } catch(exception){ } A friend function can only be a regular stand-alone function and can not be a member of another class. True False A function template is an actual function that the...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • c++ please Create a my pair class class that uses an array to store pairs of...

    c++ please Create a my pair class class that uses an array to store pairs of elements. The data members of the class are first and second. Use a template to allow the class to store pairs of any type so that a pair may be (int, int) or (int, double) or (int string), or any other combination you can think of. Create a member function to display the pair in the format "value, value" with a comma separating them....

  • A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set...

    A. (C++) Write a class declaration named Circle, with a private member variable, radius. Write set and get functions to access the radius variable, and a function named get getArea which returns the area of the circle. The area is calculated as 3.14 * radius * radius. B. Write a constructor to the circle that accepts an argument and assign its value to the radius member variable. C. Add a static member variable that accounts for the currently active circle...

  • in c++ please explain with answer 6. Given the following template function definition, which of the...

    in c++ please explain with answer 6. Given the following template function definition, which of the following is not a valid invocation of the function? template <class T> a. swap(s1,s2); void swap(T& left, T& right) b. swap(int1, int2); { c. swap(ch1, ch2); //implementation d. swap(int1, ch2); } int int1, int2; float flt1, flt2; char ch1, ch2; string s1, s2; 7. Why can you not use the swap template function to swap two complete arrays? template <class T> void swap(T& left,...

  • In C++ Create a class called MyInteger. It should have a field of type pointer-to-int called...

    In C++ Create a class called MyInteger. It should have a field of type pointer-to-int called pInteger. It should have a constructor that takes as a parameter an int - the constructor will then dynamically allocate memory for an int, using pInteger, and assign the parameter's value to that memory. The class should have a destructor that will deallocate that memory when the object is destroyed. You should write a copy constructor that will correctly make a separate copy of...

  • I need help implementing 3 function prototypes with using operator overloading in a class. I have...

    I need help implementing 3 function prototypes with using operator overloading in a class. I have bolded the section I need help with since the cpp file is bigger than I expected. Any comments throughout would be extremely helpful. Time.cpp file #include "Time.h" #include #include #include // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for...

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

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