473,699 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<(ostr eam &s, const Format<T&rhs);

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

template<class T>
Format<T>::Form at(Format<T>::F unc func,
const T &ref,
int width,
int precision)
: func_(func),
ref_(ref),
width_(width),
precision_(prec ision)
{
}

template<class T>
ostream&
Format<T>::fmt( ostream &s, const T &ref, int width, int precision)
{
int w = s.width(abs(wid th));
int p = (precision != -1) ? s.precision(pre cision) :
s.precision();
if (width < 0)
s.setf(ios::lef t, ios::adjustfiel d);
s << ref;
s.width(w);
s.precision(p);
return s;
}

template<class T>
ostream&
operator<<(ostr eam &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>(Forma t<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 1800
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.d udewrote:
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
2819
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 { }; template <typename Base> class Tpl : protected Base {
3
2671
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... Given: #include <string> // Parent class
1
1557
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 following. In this case is the derived class a class template and the base class a class template. Both the base class and the derived class will be instansiated with some
5
2080
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 works fine but there in one thing that I'm unsure about and that is the inheritance statement. What difference is it if I have this construction class CheckedArray : public Array<T>
4
2511
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. Best wishes, Peng
13
4558
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 essense of what I'm trying to do: //-------code------- #include <vector>
2
2493
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: VertexValueTyp& vertex(int i); that dereferences vertices internally in case VertexTyp is a pointer
9
3462
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. //------------------------------------------------------------------ // my regular glass class A { }; // my templated class
32
2720
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 issue. A noddy mixin layer example should illustrate the issue... class Base { protected: int m_Field;
0
8687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9174
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8914
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7750
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.