Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert template to inheritance

JosephLee
Guest
 
Posts: n/a
#1: Oct 30 '07
/*
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 Tostream&
operator<<(ostream &s, const Format<T&rhs);

template<class TFormat<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?


Markus Moll
Guest
 
Posts: n/a
#2: Oct 30 '07

re: Convert template to inheritance


Hi

JosephLee wrote:
Quote:
Anyone has some idea of that?
What is your problem with it?

Markus

red floyd
Guest
 
Posts: n/a
#3: Oct 30 '07

re: Convert template to inheritance


JosephLee wrote:
Quote:
/*
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-lit...t.html#faq-5.2

JosephLee
Guest
 
Posts: n/a
#4: Oct 30 '07

re: Convert template to inheritance


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;


}


On Oct 30, 12:39 pm, red floyd <no.s...@here.dudewrote:
Quote:
JosephLee wrote:
Quote:
/*
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.
*/
>
Quote:
[redacted]
>
Good news!!!! Your problem has been addressed here before. You can find
information at
>
http://www.parashift.com/c++-faq-lit....html#faq-5.2- Hide quoted text -
>
- Show quoted text -

Closed Thread