Connecting Tech Pros Worldwide Help | Site Map

Convert template to inheritance

  #1  
Old October 30th, 2007, 05:05 PM
JosephLee
Guest
 
Posts: n/a
/*
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?

  #2  
Old October 30th, 2007, 05:25 PM
Markus Moll
Guest
 
Posts: n/a

re: Convert template to inheritance


Hi

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

Markus

  #3  
Old October 30th, 2007, 05:45 PM
red floyd
Guest
 
Posts: n/a

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

  #4  
Old October 30th, 2007, 06:45 PM
JosephLee
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
template vector, incomplete type error imutate@hotmail.co.uk answers 13 October 2nd, 2006 02:05 PM
How to convert from dynamic polymophism to static polymophism? PengYu.UT@gmail.com answers 4 November 9th, 2005 08:35 PM
Templates and Inheritance Jürgen Kaminski answers 7 July 22nd, 2005 08:46 PM
trying to understand Inheritance, Polymorphism and templates sapropel answers 1 July 22nd, 2005 11:56 AM