473,320 Members | 2,048 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.

Inheritance downcast query

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::operator=;

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;
}

Nov 29 '06 #1
4 1687
* ja*****@gmail.com:
>
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 <iostreamplus <ostream(and perhaps
<istreamif 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::operator=;
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::operator=( 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::operator=( 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?
Nov 29 '06 #2

Alf P. Steinbach wrote:
* ja*****@gmail.com:

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 <iostreamplus <ostream(and perhaps
<istreamif 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::operator=;

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::operator=( 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::operator=( 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

Nov 29 '06 #3

Alf P. Steinbach wrote:
* ja*****@gmail.com:

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 <iostreamplus <ostream(and perhaps
<istreamif 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::operator=;

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::operator=( 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::operator=( 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

Nov 29 '06 #4
* 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?
Nov 29 '06 #5

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

Similar topics

9
by: Joe | last post by:
Hello All: I work at an insurance company and we are changing the design of an ASP.NET application (for those have followed my postings, we're going to redesign some parts of the application -...
8
by: | last post by:
#include <iostream> using namespace std; class Base { public: Base() {} ~Base() {} };
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
2
by: Alexander Adam | last post by:
Hi! I got a class structure similar to: class Base_Object { ... some functions ... } class Object: public Base_Object,
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
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
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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

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.