-
December 4th, 2011, 07:47 AM
#1
learning about publicly and privately derived classes and access specifiers
Hi
Please have a look on the code below. The statement in red is giving all those errors. I'm learning inheritance. So, it means whatever I'm trying to do isn't allowed but what is so bad about it that it's illegal to do? Could you please help with it? Thank you.
Code:
// pubpriv.cpp
// tests publicly- and privately-derived classes
#include <iostream>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////
class A //base class
{
private:
int privdataA;
protected:
int protdataA;
public:
int pubdataA;
};
////////////////////////////////////////////////////////////////
class B : public A //publicly-derived class
{
public:
int pubdataB = pubdataA;
public:
void funct()
{
int a;
//a = privdataA;
a = protdataA;
a = pubdataA;
}
};
////////////////////////////////////////////////////////////////
class C : private A
{
public:
void funct()
{
int a;
//a = privdataA;
a = protdataA;
a = pubdataA;
}
};
////////////////////////////////////////////////////////////////
int main()
{
int a;
A objA;
objA.pubdataA = 5;
B objB;
//a = objB.privdataA;
//a = objB.protdataA;
a = objB.pubdataB;
C objC;
//a = objC.privdataA;
//a = objC.protdataA;
//a = objC.pubdataA;
cout << a << endl;
system("pause;");
return 0;
}
Errors:
Code:
23|error: 'A::pubdataA' cannot appear in a constant-expression|
23|error: ISO C++ forbids initialization of member 'pubdataB'|
23|error: making 'pubdataB' static|
23|error: ISO C++ forbids in-class initialization of non-const static member 'pubdataB'|
||=== Build finished: 4 errors, 0 warnings ===|
-
December 4th, 2011, 08:10 AM
#2
Re: learning about publicly and privately derived classes and access specifiers
Initialization like this:
Code:
class B : public A //publicly-derived class
{
public:
int pubdataB = pubdataA; // the syntax is not C++, members allowed to be initialized in c-tor only
must be
Code:
class B: public A
{
public:
int pubdataB;
B(): A(), pubdataB(A::pubdataA){} // explicit initialization in c-tor
Best regards,
Igor
-
December 4th, 2011, 08:28 AM
#3
Re: learning about publicly and privately derived classes and access specifiers
This is now possible with the new standard of c++ now called c++11 which was just finalized in August this year. c++11 is still being implemented in compilers. Gcc 4.7 and Clang 3.0 supports this new feature.
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
December 4th, 2011, 09:14 AM
#4
Re: learning about publicly and privately derived classes and access specifiers
Thank you, Mr. Igor, and Joeman for the information.
Originally Posted by Igor Vartanov
Initialization like this:
Code:
class B : public A //publicly-derived class
{
public:
int pubdataB = pubdataA; // the syntax is not C++, members allowed to be initialized in c-tor only
must be
Code:
class B: public A
{
public:
int pubdataB;
B(): A(), pubdataB(A::pubdataA){} // explicit initialization in c-tor
I understand what you are saying, therefore I have changed the listing but didn't really follow your suggested syntax for c-tor because I'm not familiar with that.
Please have a look on the parts in red. I wanted "a" to display value of "5" because this is the value pubdataA had: . Where am I going wrong? Please help me with it.
Code:
// pubpriv.cpp
// tests publicly- and privately-derived classes
#include <iostream>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////
class A //base class
{
private:
int privdataA;
protected:
int protdataA;
public:
int pubdataA;
};
////////////////////////////////////////////////////////////////
class B : public A //publicly-derived class
{
public:
int pubdataB;
public:
B() : pubdataB(A::pubdataA)
{/*empty body*/}
void funct()
{
int a;
//a = privdataA;
a = protdataA;
a = pubdataA;
}
};
////////////////////////////////////////////////////////////////
class C : private A
{
public:
void funct()
{
int a;
//a = privdataA;
a = protdataA;
a = pubdataA;
}
};
////////////////////////////////////////////////////////////////
int main()
{
int a;
A objA;
objA.pubdataA = 5;
B objB;
//a = objB.privdataA;
//a = objB.protdataA;
a = objB.pubdataA;
C objC;
//a = objC.privdataA;
//a = objC.protdataA;
//a = objC.pubdataA;
cout << a << endl;
system("pause;");
return 0;
}
Code:
2359128
Press any key to continue . . .
-
December 4th, 2011, 09:24 AM
#5
Re: learning about publicly and privately derived classes and access specifiers
You miss the fact that B objB definition never means it has something common with A objA. The B to A inheritance establishes relation among classes (types) but not objects (class instances). So when you assign some value to objA.pubdataA (change objA internals), objB internals remain untouched. Which seems quite logical.
Last edited by Igor Vartanov; December 4th, 2011 at 09:27 AM.
Best regards,
Igor
-
December 4th, 2011, 10:48 AM
#6
Re: learning about publicly and privately derived classes and access specifiers
Thank you, Mr. Igor.
So, what am I doing in this c-tor?
Code:
public:
B() : pubdataB(A::pubdataA)
{/*empty body*/}
Could you please tell me? What's the purpose of assigning an instance of B a data member of class A which is a base class? Thank you
Best wishes
H
-
December 5th, 2011, 12:32 AM
#7
Re: learning about publicly and privately derived classes and access specifiers
The purpose is to do exactly what I understood from your code:
Code:
class B : public A //publicly-derived class
{
public:
int pubdataB = pubdataA;
At B class instance initialization assign to its member B::pubdataB the value of its A::pubdataA inherited member. According to your later comments it seems I got your idea completely wrong. Now, to make a more successful iteration in this discussion, you need to explain yourself more thorough I believe.
Best regards,
Igor
-
December 5th, 2011, 10:30 AM
#8
Re: learning about publicly and privately derived classes and access specifiers
Thank you for the reply.
What I was trying to do wasn't possible. Suppose you see someone's code written the way I have it. To me, this part
Code:
B() : pubdataB(A::pubdataA)
{/*empty body*/}
would only make sense when A:: pubdataA is some static or static const member of class A, e.g.
Code:
static const int pudataA = 5;
because otherwise A:: pubdataA would contain some garbage and there is no point in assigning a garbage value to some other data member.
What do you think? Please help me know. Thank you for the help.
Best wishes
H
-
December 5th, 2011, 10:49 AM
#9
Re: learning about publicly and privately derived classes and access specifiers
A::pubdataA in the scope of class that inherits to A means: the scope for searching pubdataA variable must be A. And compiler will see whether the member is static or not. In your case it's not, but still accessible from B because of public inheritance.
I wonder, were you able to find a good book on C++ basics?
Best regards,
Igor
-
December 5th, 2011, 02:24 PM
#10
Re: learning about publicly and privately derived classes and access specifiers
Originally Posted by Igor Vartanov
A::pubdataA in the scope of class that inherits to A means: the scope for searching pubdataA variable must be A. And compiler will see whether the member is static or not. In your case it's not, but still accessible from B because of public inheritance.
I wonder, were you able to find a good book on C++ basics?
Thank you, Mr. Igor.
Yes, I have a book. Don't know if it's a good one. I believe my questions are now less theoretical; at least now I first write a code myself, then ask questions about it. In the past, I was going in reverse direction - first asking theoretical questions, then writing a code. Thanks a lot for the help.
With best wishes
H
-
December 5th, 2011, 03:52 PM
#11
Re: learning about publicly and privately derived classes and access specifiers
because otherwise A:: pubdataA would contain some garbage and there is no point in assigning a garbage value to some other data member.
Well, you can see in my snippet that B c-tor calls A c-tor first, and that one can initialize pubdataA to be no garbage. So there is a point, if you do the things right.
Last edited by Igor Vartanov; December 5th, 2011 at 03:58 PM.
Best regards,
Igor
-
December 5th, 2011, 04:18 PM
#12
Re: learning about publicly and privately derived classes and access specifiers
Hi again
If I really want to get rid of garbage value, then I would simply do this:
Code:
public:
B() : pubdataB(0)
{/*empty body*/}
. To me, as a beginner,
Code:
pubdataB(A::pubdataA)
would make sense when pubdataA is a static member.
Thank you.
-
December 6th, 2011, 02:36 AM
#13
Re: learning about publicly and privately derived classes and access specifiers
Look, I gave you the code just because of replicating yours. To me as a not beginner the code makes sense not much too. I just answered your question.
While you did not answer mine. What was your idea of int pubdataB = pubdataA;? If you answer this, maybe I will suggest some very different code.
BTW, the code may be pubdataB(pubdataA) alright. Does this look better for a beginner?
Last edited by Igor Vartanov; December 6th, 2011 at 02:41 AM.
Best regards,
Igor
-
December 6th, 2011, 05:47 AM
#14
Re: learning about publicly and privately derived classes and access specifiers
Originally Posted by Igor Vartanov
Look, I gave you the code just because of replicating yours. To me as a not beginner the code makes sense not much too. I just answered your question.
While you did not answer mine. What was your idea of int pubdataB = pubdataA;? If you answer this, maybe I will suggest some very different code.
BTW, the code may be pubdataB(pubdataA) alright. Does this look better for a beginner?
Hi
I did answer you but perhaps it wasn't a direct answer. Whatever I was trying to do wasn't possible as you, probably unknowingly, told me. See, in the post quoted below, I myself appear confused as to what this code otherwise might mean. It's clear now and you have helped me. That's all matters. Thank you.
Best wishes
H
Originally Posted by heights
Thank you, Mr. Igor.
So, what am I doing in this c-tor?
Code:
public:
B() : pubdataB(A::pubdataA)
{/*empty body*/}
Could you please tell me? What's the purpose of assigning an instance of B a data member of class A which is a base class? Thank you
Best wishes
H
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|