Convert template to inheritance

J

JosephLee

/*
Below you will find the declaration and the definition of the class
Format
that can be used to manipulate the ostream. Although the class is
only
capable of setting the width and precision, further functionality
could
be
added.
The class is implemented as a template class. Your task is to
rewrite it
for two types (int and double) without templates using
inheritance.
*/

template<class T>
struct Format {
typedef ostream& (*Func)(ostream&, const T&, int, int);
Format(Func func, const T &ref, int width, int precision);
static ostream& fmt(ostream &, const T&, int, int);

Func func_; // pointer to format function
const T &ref_; // reference to object
int width_; // desired field width
int precision_; // desired precision
};

template<class T> ostream&
operator<<(ostream &s, const Format<T> &rhs);

template<class T> Format<T>
fmt(const T &ref, int width, int precision = -1);

template<class T>
Format<T>::Format(Format<T>::Func func,
const T &ref,
int width,
int precision)
: func_(func),
ref_(ref),
width_(width),
precision_(precision)
{
}

template<class T>
ostream&
Format<T>::fmt(ostream &s, const T &ref, int width, int precision)
{
int w = s.width(abs(width));
int p = (precision != -1) ? s.precision(precision) :
s.precision();
if (width < 0)
s.setf(ios::left, ios::adjustfield);
s << ref;
s.width(w);
s.precision(p);
return s;
}

template<class T>
ostream&
operator<<(ostream &s, const Format<T> &rhs)
{
return rhs.func_(s, rhs.ref_, rhs.width_, rhs.precision_);
}


template<class T>
Format<T>
fmt(const T &ref, int width, int precision)
{
return Format<T>(Format<T>::fmt, ref, width, precision);
}


// The program:
main()
{
double pi = acos(-1);
cout << "easy as " << fmt(pi, 5, 2) << " ..." << endl;
cout << "easy as " << fmt(pi, -10) << " ... " << endl;
}

// will print the following output:
// easy as 3.1 ...
// easy as 3.14159 ...
/*
Anyone has some idea of that?
 
R

red floyd

JosephLee said:
/*
Below you will find the declaration and the definition of the class
Format
that can be used to manipulate the ostream. Although the class is
only
capable of setting the width and precision, further functionality
could
be
added.
The class is implemented as a template class. Your task is to
rewrite it
for two types (int and double) without templates using
inheritance.
*/

[redacted]

Good news!!!! Your problem has been addressed here before. You can find
information at

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
 
J

JosephLee

Hehe, actually it is not homework.
Somebody gave me a test. And I failed on this problem. This problem
puzzled me
because there is function pointer involved.
Class Format(){};
class intFormat :public Format{};
class DoutbtFormat :public Format{}

How to design these classes so that
// The program:
main()
{
double pi = acos(-1);
cout << "easy as " << fmt(pi, 5, 2) << " ..." << endl;
cout << "easy as " << fmt(pi, -10) << " ... " << endl;


}


JosephLee said:
/*
Below you will find the declaration and the definition of the class
Format
that can be used to manipulate the ostream. Although the class is
only
capable of setting the width and precision, further functionality
could
be
added.
The class is implemented as a template class. Your task is to
rewrite it
for two types (int and double) without templates using
inheritance.
*/
[redacted]

Good news!!!! Your problem has been addressed here before. You can find
information at

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2- Hide quoted text -

- Show quoted text -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top