Connecting Tech Pros Worldwide Help | Site Map

Convert template to inheritance

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 30th, 2007, 04:05 PM
JosephLee
Guest
 
Posts: n/a
Default Convert template to inheritance

/*
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, 04:25 PM
Markus Moll
Guest
 
Posts: n/a
Default 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, 04:45 PM
red floyd
Guest
 
Posts: n/a
Default 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, 05:45 PM
JosephLee
Guest
 
Posts: n/a
Default 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 -

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.