[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Inheritance

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
luoys
Asked:
luoys
  • 3
2 Solutions
 
List244Commented:
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
 
List244Commented:
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
 
jkrCommented:
>>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
 
List244Commented:
Ah, that is true, I just put C.x and C.y .. because that is all that is there.. lol
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now