Needed solved errors

Hai the following program is executed some errors are display. Kindly solve this


#include<iostream.h>
Class A
{
private:
int a1;
public:
int a2;
protected:
int a3;
};
class B:public A
{
public:
void func( )
{
int b1,b2,b3;
b1=a1;
b2=a2;
b3=a3;
}
};
void main( )
{
B der;
der.a3=0;
der.fun( );
}
}

You should read about the member access control and uodate your program yourself.
Firstly,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream.h>
Class A
{
private:
int a1;
public:
int a2;
protected:
int a3;
};
class B:public A
{
public:
void func( )
{
int b1,b2,b3;
b1=a1;
b2=a2;
b3=a3;
}
};
void main( )
{
B der;
der.a3=0;
der.fun( );
}
}


Now, you have a problem. Your class B is inheriting only the public and protected members from your class A. (You cannot inherit the private member).
Thus, you cannot call the void func() (which is in class B), as it tries to read the variable a1, which is private to class A.

Hope I could help,
~ Raul ~
Last edited on
@jumper007
(You cannot inherit the private member).


All members of the base class are inherited irrespective of the access control.

It is not members that are inherited. It is the class that is inherited.
Last edited on
vlad from moscow wrote:
All members of the base class are inherited irrespective of the access control.

+1. Actually, the function DOES exist. He just cannot access it because it's private.

Someone in the Future wrote:
Hey, SGH, How can you say that the function exists if You cannot use it?

Simple: If the base class uses this function it is loaded and fully enabled to use it!
@vlad from moscow
That's what I said. The class B inherits class A. By inheriting class A (base class) in class B (derived class), you also inherit all it's functions/variables which are under the public or protected members.
Last edited on
@jumper007
@vlad from moscow
That's what I said. The class B inherits class A. By inheriting class A (base class) in class B (derived class), you also inherit all it's functions/variables which are under the public or protected members.


One more: all members are inherited irrespective of the access control.

The word all includes all public, protected and private members.
I really don't understand what you mean. Apart the one error I mentioned, the program would have the following effect: b2 = a2 and b3 = a3. That's because the inheritance is done correctly, and that gives the derived class the access to both the public and protected members of the base class, giving it the possibility to use the data.

PS: You have repeated the words "all members are inherited irrespective of the access control" more than 2 times. If you are sure that you are right, please explain that in a proper way. Either way, this should be enough:

Buckys C++ Programming Tutorials - 52 - Inheritance:
http://www.youtube.com/watch?v=gq2Igdc-OSI&feature=player_embedded
Buckys C++ Programming Tutorials - 53 - protected Members:
http://www.youtube.com/watch?v=DHAAy4GJ684&feature=player_embedded
Last edited on
I do not know even how to explain. I can only repeat the phrase that

It is not members that are inherited. It is the class that is inherited


Consider the example

1
2
3
4
5
6
7
8
9
10
11
12
13
class A
{
public:
   A( int i = 0 ) : x( i ) {}
   int get() const { return x; }
private:
   int x;
};

class B: public A
{
   using A::A;
};

Though class B has no direct access to A::x nevertheless it includes x and can get its value using public method A::get.

Also consider statement

std::cout << sizeof( B ) << std::endl;

It shows that member A::x was inherited by class B.
Last edited on
Oh.. I see that now. Sorry, I just didn't see that before. Well, thanks for pointing it out. :)
Topic archived. No new replies allowed.