473,748 Members | 9,596 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 1803
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
2824
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
2673
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
1562
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
2083
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
2516
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
4562
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
2496
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
3470
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
2731
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
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
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...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
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
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
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
2215
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.