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<iostre am.h>
#include<string .h>
template<typena me T>
class A
{
private :
T a;
public :
A(T a1):a(a1){}
A(const A<T>& a){}
~A(){}
void Get_Value()cons t
{cout<<"Value of a :: "<< a <<endl<<"\n"; }
};
template<typena me 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()c onst
{ cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
}
};
template<typena me 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()c onst
{ cout<<"Value of C___ b is :: "<<b <<endl<<"\n";
cout<<"Value of C___ c is :: "<<c <<endl<<"\n";
}
};
template<typena me 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,floa tb(12,'V',17.8) ;
b.Get_Value();
b.Show_Value_C( );
b.Show_Value_B( );
return 0;
} 4 2462
On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.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<iostre am.h>
#include<string .h>
#include <iostream>
#include <string>
>
template<typena me 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()cons t
{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<typena me 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()c onst
{ cout<<"Value of B___ b is :: "<<b <<endl<<"\n";
cout<<"Value of B___ c is :: "<<c <<endl<<"\n";
}
};
template<typena me 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()c onst
{ 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<typena me 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,floa tb(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;
}
On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.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)!
On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gm ail.comwrote:
On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.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
On 27 дек, 21:58, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 27, 4:46 am, Pavel Shved <Pavel.Sh...@gm ail.comwrote:
On 27 ÄÅË, 12:25, Salt_Peter <pj_h...@yahoo. comwrote:
On Dec 27, 2:55 am, Pallav singh <singh.pal...@g mail.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 shape for
both even and odd value of size. For example if user enters number 7
the program should print following shape.
#
#$#
#$#$#
#$#$#$#
|
by: Ben Collingsworth |
last post by:
Anyone have some efficient source code for implementing a ring buffer?
|
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 shown....i.e
two half diamonds...e.g if no. is 3 output;
* *
* * *
#include <stdio.h>
void star(int num);
|
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
code.But,the compiler sometimes show some strange errors,sometimes can
pass through with an unknown trouble when it is running,and no
result.So,could anyone give me some help? Thank you very much!
Here is my code:...
| |
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 "
* ".
*
***
*****
*******
*********
*******
|
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
|
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>
class Ring {
|
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 class structure:
class entity
{
public:
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |