473,320 Members | 2,133 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,320 software developers and data experts.

Template -- Diamond ring Problem

I am Facing Problem while creating object of Diamond Ring problem
solving using Template
Kindly let me known where i am committing ERROR

Thanks
Pallav

#include<iostream.h>
#include<string.h>

template<typename T>
class A
{
private :
T a;

public :
A(T a1):a(a1){}
A(const A<T>& a){}

~A(){}

void Get_Value()const
{cout<<"Value of a :: "<< a <<endl<<"\n";}

};
template<typename T ,typename U>
class B :virtual public A<T>
{
private :
U b;
U c;

public :

B(T a1,U b1):A<T>(a1),b(b1),c(b1){}
B(const B<T,U>& b){}

~B(){}

void Show_Value_B()const
{ cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
}
};

template<typename T ,typename V>
class C :virtual public A<T>
{
private :
V b;
V c;
public :

C(T a1,V b1):A<T>(a1),b(b1),c(b1){}
C(const C<T,V>& c){}

~C(){}

void Show_Value_C()const
{ cout<<"Value of C___ b is :: "<<b <<endl<<"\n";
cout<<"Value of C___ c is :: "<<c <<endl<<"\n";
}
};
template<typename T,typename U ,typename V>
class D :virtual public B<T,U>, virtual public C<T,V>
{
public :
D(T a,U b,V c):B<T,U>(a,b),C<T,V>(a,c){}
D(const D<T,U,V&d){}
~D(){};
};
int main(int argc , char *argv[])
{
D<int,char,floatb(12,'V',17.8);
b.Get_Value();
b.Show_Value_C();
b.Show_Value_B();

return 0;
}
Dec 27 '07 #1
4 2438
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.comwrote:
I am Facing Problem while creating object of Diamond Ring problem
solving using Template
Kindly let me known where i am committing ERROR
read:
[25.9] Where in a hierarchy should I use virtual inheritance?
http://www.parashift.com/c++-faq-lit....html#faq-25.9
>
Thanks
Pallav

#include<iostream.h>
#include<string.h>
#include <iostream>
#include <string>
>
template<typename T>
class A
{
private :
T a;

public :
A(T a1):a(a1){}
A(const T a1) : a(a1) { }
or
A(const T& t) : a(t) { }

If T is any primitive type, like integer, the constructor will work
well with the first case.
If T were a complex user-type, a const reference is much, much better.

I use the second case exclusively unless its specifically prohibited.
In your case, you might prefer an explicit ctor.
A(const A<T>& a){}

~A(){}

void Get_Value()const
{cout<<"Value of a :: "<< a <<endl<<"\n";}
void Get_Value() const
{
std::cout << "Value of a :: " << a;
std::cout << std::endl << std::endl;
}

although that function should be a friend operator<< overload
What if T is not a primitive type?
>
};

template<typename T ,typename U>
class B :virtual public A<T>
template < typename T ,typename U >
class B : public virtual A< T >
{
private :
U b;
U c;

public :

B(T a1,U b1):A<T>(a1),b(b1),c(b1){}
B(const B<T,U>& b){}

~B(){}

void Show_Value_B()const
{ cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
}
};

template<typename T ,typename V>
class C :virtual public A<T>
{
private :
V b;
V c;
public :

C(T a1,V b1):A<T>(a1),b(b1),c(b1){}
C(const C<T,V>& c){}

~C(){}

void Show_Value_C()const
{ cout<<"Value of C___ b is :: "<<b <<endl<<"\n";
cout<<"Value of C___ c is :: "<<c <<endl<<"\n";
}
};
B and C should really be one class
>
template<typename T,typename U ,typename V>
class D :virtual public B<T,U>, virtual public C<T,V>
Don't bother trying to construct a mesh, deal with a diamond first.
B and C are not virtual.
If they were, you wouldn't be able to generate an instance of D.

template < typename T, typename U, typename V>
class D : public B< T, U >, public C< T, V >
{
public :
D(T a,U b,V c):B<T,U>(a,b),C<T,V>(a,c){}
D(const T& a, const U& b, const V& c)
: A< T >(a), // somebody has to do it
B< T, U >(a,b),
C< T, V >(a,c)
{}
D(const D<T,U,V&d){}
~D(){};
};

int main(int argc , char *argv[])
{
D<int,char,floatb(12,'V',17.8);
b.Get_Value();
b.Show_Value_C();
b.Show_Value_B();
if you had op<< available for each class, you could display the entire
mess with one line.
std::cout << b << std::endl;
>
return 0;

}
Dec 27 '07 #2
On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.comwrote:

void šGet_Value() const
{
š std::cout << "Value of a :: š" << a;
š std::cout << std::endl << std::endl;

}

although that function should be a friend operator<< overload
What if T is not a primitive type?
Exactly what we're trying to achieve: print T in the way we don't know
yet but require to exist. That's called `generic programming', ever
heard of that?
Don't bother trying to construct a mesh, deal with a diamond first.
B and C are not virtual.
If they were, you wouldn't be able to generate an instance of D.
What are you talking about? oO If that's the reason why we can't
generate instance of D we couldn't generate instances of B and C as
well then! Let alone that the code above (with a little fixup to D's
constructor) compiles and runs correctly.

š š šD(const T& a, const U& b, const V& c)
š š š š š š š š š š š š š š : A< T >(a), // somebody has to do it
imho this doesn't catch the problem. `Somebody' has already `done' it,
even two times (B and C constructors)!
Dec 27 '07 #3
On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gmail.comwrote:
On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.comwrote:
void šGet_Value() const
{
š std::cout << "Value of a :: š" << a;
š std::cout << std::endl << std::endl;
}
although that function should be a friend operator<< overload
What if T is not a primitive type?

Exactly what we're trying to achieve: print T in the way we don't know
yet but require to exist. That's called `generic programming', ever
heard of that?
Uhuh, generic programming supposes that T be printable. What if T is
not a primitive type or a std::string, etc.
So i'll throw your statement right back at you: have *YOU* ever heard
of generic programming?
(think: T must provide an op<<)
>
Don't bother trying to construct a mesh, deal with a diamond first.
B and C are not virtual.
If they were, you wouldn't be able to generate an instance of D.

What are you talking about? oO If that's the reason why we can't
generate instance of D we couldn't generate instances of B and C as
well then! Let alone that the code above (with a little fixup to D's
constructor) compiles and runs correctly.
Thats my mistake
>
š š šD(const T& a, const U& b, const V& c)
š š š š š š š š š š š š š š : A< T >(a), // somebody has to do it

imho this doesn't catch the problem. `Somebody' has already `done' it,
even two times (B and C constructors)!
not if you remove those constructors in B and C, kindof obvious.
Or was i suppose to do what you didn't do - remove A's ctors in B and
C?
Please
Dec 27 '07 #4
On 27 дек, 21:58, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gmail.comwrote:
On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo.comwrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@gmail.comwrote:
void Å¡Get_Value() const
{
Å¡ std::cout << "Value of a :: Å¡" << a;
Å¡ std::cout << std::endl << std::endl;
}
although that function should be a friend operator<< overload
What if T is not a primitive type?
Exactly what we're trying to achieve: print T in the way we don't know
yet but require to exist. Â*That's called `generic programming', ever
heard of that?

Uhuh, generic programming supposes that T be printable. What if T is
not a primitive type or a std::string, etc.
Well, primitive and STL types are not only ones who provide printing.
That's wat i was talking about.
Now i should also mention that yielding a compile-error if T does not
statisfy concept requirements is what STL types do. The OP's class
would do the same.
So, back to original question, `What is if T is not a primitive type?'
- nothing special. OP's written the very thing he should do in that
particular line with printing statement.

Å¡ Å¡ Å¡D(const T& a, const U& b, const V& c)
Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ Å¡ : A< T >(a), // somebody has to do it
imho this doesn't catch the problem. `Somebody' has already `done' it,
even two times (B and C constructors)!

not if you remove those constructors in B and C, kindof obvious.
It seems i dont understand you. Whom did you mean by `somebody'?
Constructor of D? But he can not be referred to as SOMEbody because
he's an only CORRECT one to do it, and that's whai i've been talking
about.
Or was i suppose to do what you didn't do - remove A's ctors in B and
C?
I wasn't even going to do this as OP's problem is with D class, all
the other classes being absolutely correct.
Dec 27 '07 #5

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

Similar topics

5
by: Tony Johansson | last post by:
Hello Experts! It it correct to say that a solution to the diamond problem is to use virtual inheritance with virtual base classes. //Tony
11
by: coinjo | last post by:
I need to write a program which takes a number as an input and prints a diamond of # and $. The number of rows in the shape is equal to the number entered by the user. My program should display the...
11
by: Ben Collingsworth | last post by:
Anyone have some efficient source code for implementing a ring buffer?
3
by: mitchellpal | last post by:
guys.... help me out here... my code is running halfway... how do i complete the other right half....... pp.. the user should input an odd number btw 0 and 20 then the program displays th shape as...
17
by: Yuri CHUANG | last post by:
Hi, I'm the beginner of the CPL.I write a program about the problem of Ring of Josephus,using DoubleLinkList data structure. I'm very confused that I think there is really no error in my...
14
by: jasson118 | last post by:
i am a newber to C++ and have trouble with one of the problem from the book. can anyone able to use nested loops to display a diamond shape with " * ". * *** ***** ******* ********* *******
9
by: weird0 | last post by:
How does C++ and C# solve the Diamond problem? With the help of interfaces that is. Can anyone elaborate ....... Regards
2
by: ndbecker2 | last post by:
On upgrading from gcc-4.1.2 to gcc-4.3, this (stripped down) code is now rejected: #include <vector> #include <iostream> template<typename T, template <typename Aclass CONT=std::vector>...
6
by: Rocketmagnet | last post by:
Hi all, I have been kind of forced (honestly) into writing a class structure which contains a Diamond of Death, and I'm not entirely sure what to do about it. This is a simplified version of my...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.