Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheritance downcast query

Reply
Thread Tools

Inheritance downcast query

 
 
jayesah@gmail.com
Guest
Posts: n/a
 
      11-29-2006
Hi All,

I have following code. Anyone can please tell me how
object of type class A is converted to Class B ?

#include <iostream.h>
class A
{
public:
void operator=(int i){a=i;}

A operator+(A& obj)
{
A nn;
nn.a = a + obj.a;
cout<<"A: operator + is called\n";
return nn;

}

int a;
};

class B : public A
{
using A:perator=;

public:
B()
{ cout<<"b's simple called\n"; };
int b;
};

int main(void)
{
B bb1,bb2;
bb1=3;
bb2=4;

B bb3;
bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

cout << bb3.a;

return 0;
}

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-29-2006
* http://www.velocityreviews.com/forums/(E-Mail Removed):
>
> I have following code. Anyone can please tell me how
> object of type class A is converted to Class B ?


Yes, but first, about the code.


> #include <iostream.h>


This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
have this header. Use standard <iostream> plus <ostream> (and perhaps
<istream> if input is required).


> class A
> {
> public:
> void operator=(int i){a=i;}
>
> A operator+(A& obj)


That argument should be "A const& obj" unless you're intent on changing
the actual argument object.

As it is you can't call "+" with a constant or temporary right hand side.


> {
> A nn;
> nn.a = a + obj.a;
> cout<<"A: operator + is called\n";
> return nn;
>
> }
>
> int a;
> };
>
> class B : public A
> {
> using A:perator=;


Are you sure you want this assignment operator private?


> public:
> B()
> { cout<<"b's simple called\n"; };


You mean, "default constructor".


> int b;
> };
>
> int main(void)
> {
> B bb1,bb2;
> bb1=3;
> bb2=4;
>
> B bb3;
> bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */


First, there's no conversion from B to A, and this should not compile
(have you actually tried the code?).

Second, if you make the carte blanche "using" statement public it should
compile, because the auto-generated A:perator=( A const& ) is then
made available in B and invoked. To avoid that you just need to be a
bit more specific.

E.g., in class B, "void operator=( int i ){ A:perator=( i ); }"; or
better, use a conventional member function instead of an operator.


> cout << bb3.a;
>
> return 0;
> }
>



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Naresh
Guest
Posts: n/a
 
      11-29-2006

Alf P. Steinbach wrote:
> * (E-Mail Removed):
> >
> > I have following code. Anyone can please tell me how
> > object of type class A is converted to Class B ?

>
> Yes, but first, about the code.
>
>
> > #include <iostream.h>

>
> This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
> have this header. Use standard <iostream> plus <ostream> (and perhaps
> <istream> if input is required).
>
>
> > class A
> > {
> > public:
> > void operator=(int i){a=i;}
> >
> > A operator+(A& obj)

>
> That argument should be "A const& obj" unless you're intent on changing
> the actual argument object.
>
> As it is you can't call "+" with a constant or temporary right hand side.
>
>
> > {
> > A nn;
> > nn.a = a + obj.a;
> > cout<<"A: operator + is called\n";
> > return nn;
> >
> > }
> >
> > int a;
> > };
> >
> > class B : public A
> > {
> > using A:perator=;

>
> Are you sure you want this assignment operator private?
>
>
> > public:
> > B()
> > { cout<<"b's simple called\n"; };

>
> You mean, "default constructor".
>
>
> > int b;
> > };
> >
> > int main(void)
> > {
> > B bb1,bb2;
> > bb1=3;
> > bb2=4;
> >
> > B bb3;
> > bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

>
> First, there's no conversion from B to A, and this should not compile
> (have you actually tried the code?).
>
> Second, if you make the carte blanche "using" statement public it should
> compile, because the auto-generated A:perator=( A const& ) is then
> made available in B and invoked. To avoid that you just need to be a
> bit more specific.
>
> E.g., in class B, "void operator=( int i ){ A:perator=( i ); }"; or
> better, use a conventional member function instead of an operator.
>
>
> > cout << bb3.a;
> >
> > return 0;
> > }
> >

>
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?



its a well compiled code with gcc 3.3.3 on linux.
It also gives expected output i..e 7

 
Reply With Quote
 
Naresh
Guest
Posts: n/a
 
      11-29-2006

Alf P. Steinbach wrote:
> * (E-Mail Removed):
> >
> > I have following code. Anyone can please tell me how
> > object of type class A is converted to Class B ?

>
> Yes, but first, about the code.
>
>
> > #include <iostream.h>

>
> This is not a standard header. E.g., Visual C++ 7.x and higher doesn't
> have this header. Use standard <iostream> plus <ostream> (and perhaps
> <istream> if input is required).
>
>
> > class A
> > {
> > public:
> > void operator=(int i){a=i;}
> >
> > A operator+(A& obj)

>
> That argument should be "A const& obj" unless you're intent on changing
> the actual argument object.
>
> As it is you can't call "+" with a constant or temporary right hand side.
>
>
> > {
> > A nn;
> > nn.a = a + obj.a;
> > cout<<"A: operator + is called\n";
> > return nn;
> >
> > }
> >
> > int a;
> > };
> >
> > class B : public A
> > {
> > using A:perator=;

>
> Are you sure you want this assignment operator private?
>
>
> > public:
> > B()
> > { cout<<"b's simple called\n"; };

>
> You mean, "default constructor".
>
>
> > int b;
> > };
> >
> > int main(void)
> > {
> > B bb1,bb2;
> > bb1=3;
> > bb2=4;
> >
> > B bb3;
> > bb3=bb1+bb2; /* Here, Who converted type Class B to Class A */

>
> First, there's no conversion from B to A, and this should not compile
> (have you actually tried the code?).
>
> Second, if you make the carte blanche "using" statement public it should
> compile, because the auto-generated A:perator=( A const& ) is then
> made available in B and invoked. To avoid that you just need to be a
> bit more specific.
>
> E.g., in class B, "void operator=( int i ){ A:perator=( i ); }"; or
> better, use a conventional member function instead of an operator.
>
>
> > cout << bb3.a;
> >
> > return 0;
> > }
> >

>
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?



its a well compiled code with gcc 3.3.3 on linux.
It also gives expected output i..e 7

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-29-2006
* Naresh:
>
> its a well compiled code with gcc 3.3.3 on linux.
> It also gives expected output i..e 7


It shouldn't compile, so if you actually copied and pasted the code that
compiled for you, then get a better compiler.

Btw., please quote what you're responding to but /no more/. Like
Einstein's "as simple as possible, but no simpler". Also, please don't
quote signatures, and please don't post the same message two or more times.

HTH. & TIA.


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual inheritance downcast C++ 8 04-27-2006 07:36 PM
question concerning downcast jimjim Java 3 03-06-2005 01:07 PM
Downcast Jarmo Java 5 07-22-2004 02:49 PM
Forced downcast - when will it work? Steve Schlesinger C++ 2 02-14-2004 08:17 AM
dynamic downcast Joris Bleys Java 9 08-01-2003 01:33 PM



Advertisments