Solved

# Inheritance

Posted on 2006-05-20
188 Views
Class A
{
int x;
}

Class B
{
int y;
}

Class C : public A, B
{
int z;
}

A *aa;
B *bb;
C cc;

aa = &c;
bb = &c;

Please can you tell  me  - do aa and bb point to the same address? If yes or no....why?
0
Question by:luoys

LVL 8

Expert Comment

Luoys, that code will not run for many reasons.
First:
class is ALWAYS lowercase.
Second:
Each class ends with a ;
Third:
You can not set a variable (aa) equal to a class (c)

With all that said, they should not point to the same address.  aa should point to C.x
bb should point to C.y  Because class A only has one member as to Class B, and they
are different members than eachother, so the address should be different.
0

LVL 8

Assisted Solution

class A
{
public:
int x;
}; //Note semicolon

class B
{
public:
int y;
};//Note semicolon

class C : public B, public A //Note BOTH public access
{
public:
int z;
}; //note semicolon

int main()
{
A *aa;
B *bb;
C cc;

aa = &cc; //Points to C.x
bb = &cc; //points to c.y
}
0

LVL 86

Accepted Solution

>>Please can you tell  me  - do aa and bb point to the same address? If yes or no....why?

A little correctio to what List244 wrote - the members aren't involved here, but the pointers will be different since they point to different aspects of 'C' - 'aa' will point to an 'A' describing the 'A' aspect and 'bb' will point to a 'B' for the same reason. You can easily check that:

A *aa;
B *bb;
C cc;

aa = &cc; //Points to 'A'
bb = &cc; //points to 'B'

printf("aa: 0x%8.8x\n", aa);
printf("bb: 0x%8.8x\n", bb);
0

LVL 8

Expert Comment

Ah, that is true, I just put C.x and C.y .. because that is all that is there.. lol
0

## Featured Post

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article locâ€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.