76 387
Assignments Done
98,1%
Successfully Done
In May 2019

Answer to Question #50557 in C++ for zexy2521

Question #50557
Create a class template for a class that holds an object. The template should provide a
standard data input function that begins with a generic warning message to enter data
carefully. The template also should include a standard output function that issues a
generic “Here’s the data you requested” message. Write a main()function that tests your
template class with an integer and two programmer-designed classes. Save the file as
StandardizedInAndOut.cpp.
Expert's answer
// StandardizedInAndOut.cpp -- template class that holds an object and classes for testing.
#include <iostream>
#include <string>
//-----------------------------------------
// Class that holds an object
// T should be a basic type or
// should have an overloaded extraction (>>) and insertion (<<) operators
template<typename T>
class object_holder
{
private:
T m_object;
public:
void input();
void output() const;
};
template<typename T>
void object_holder<T>::input()
{
std::cout << "Please, enter data carefully:" << std::endl;
std::cin >> m_object;
}
template<typename T>
void object_holder<T>::output() const
{
std::cout << "Here's the data you requested: " << std::endl;
std::cout << m_object << std::endl;
}
//-----------------------------------------
//-----------------------------------------
// Classes for tesing object_holder
class person
{
private:
int m_age;
std::string m_name;
public:
friend std::ostream &operator;<<(std::ostream &out;, const person &data;);
friend std::istream &operator;>>(std::istream &in;, person &data;);
};
std::ostream &operator;<<(std::ostream &out;, const person &data;)
{
out << "Name: " << data.m_name << ", "
<< "Age: " << data.m_age;
}
std::istream &operator;>>(std::istream &in;, person &data;)
{
std::cout << "Enter name: ";
in >> data.m_name;
std::cout << "Enter age: ";
in >> data.m_age;
}
class coordinates
{
private:
double m_x;
double m_y;
public:
friend std::ostream &operator;<<(std::ostream &out;, const coordinates &data;);
friend std::istream &operator;>>(std::istream &in;, coordinates &data;);
};
std::ostream &operator;<<(std::ostream &out;, const coordinates &data;)
{
out << "(" << data.m_x << ", " << data.m_y << ")";
}
std::istream &operator;>>(std::istream &in;, coordinates &data;)
{
std::cout << "Enter x: ";
in >> data.m_x;
std::cout << "Enter y: ";
in >> data.m_y;
}
//-----------------------------------------
int main()
{
using std::cout;
using std::endl;
cout << "Tesing object_holder with an integer: " << endl;
object_holder<int> int_holder;
int_holder.input();
int_holder.output();
cout << endl;
cout << "Testing object_holder with person class: " << endl;
object_holder<person> person_holder;
person_holder.input();
person_holder.output();
cout << endl;
cout << "Testing object_holder with coordinates class: " << endl;
object_holder<coordinates> coordinates_holder;
coordinates_holder.input();
coordinates_holder.output();
cout << endl;
return 0;
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions