473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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?

Oct 30 '07 #1
3 1784
Hi

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

Markus

Oct 30 '07 #2
JosephLee wrote:
/*
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

Oct 30 '07 #3
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:
JosephLee wrote:
/*
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....html#faq-5.2- Hide quoted text -

- Show quoted text -

Oct 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
4
by: PengYu.UT | last post by:
Hi, Some dynamic polymorphism programs can be converted to the equavalent static polymorphism programs. I'm wondering if there are any generall procedures that I can use to do this conversion. ...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.