Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I am facing a problem in inheritance.

i make


if make class a {
protected:
int i;
}
class b:public a{
projected:
int j;
}
main(){
a * obj=new a()
a.i ? // not display
}

why it is not display ?
can you please explain?
Posted
Comments
pasztorpisti 7-Apr-13 4:08am    
Hey, the google results for "C++ inheritance tutorials" is terrible. If searched for one that seems to be acceptable. Read a few chapters from it, play around with your c++ compiler/try things and if something remains unclear then come back with SPECIFIC questions. Here you go: http://www.learncpp.com/#Chapter11

The question has nothing to do with inheritance; you never use inheritance in your code sample: in the main function, the class b is totally ignored.

The problem is protected. After all, read any C++ manual about the access specifiers. To make something accessible from outside some class, make it public; protected allows access only to derived types.

—SA
 
Share this answer
 
v2
Comments
ravi1989h 7-Apr-13 0:13am    
sorry ..there is is misprintring..
if make class a {
protected:
int i;
}
class b:public a{
projected:
int j;
}
main(){
b * obj=new b()
b.i ? // not display
}

why b->i is not display
Sergey Alexandrovich Kryukov 7-Apr-13 0:26am    
What difference would it make? Same thing. I already answered.
—SA
pasztorpisti 7-Apr-13 4:05am    
+5, however I'm not surprised that OP doesn't understand the fundamentals of inheritance in C++. I search for "C++ inheritance tutorials" with google and the first page of results is terrible - If you read them you forget how to do things even if you already know them. :-)
Sergey Alexandrovich Kryukov 7-Apr-13 12:00pm    
Thank you.

As to "inheritance tutorial": this is quite explainable. Only a very stupid author would try to write "inheritance tutorial" because "inheritance" is not a self-containing topic at all. It could be OOP tutorial, C++ tutorial, etc. Or better yet, not a tutorial, but a book on programming. OOP can be explained in one good article of decent size, but it requires prerequisite knowledge.

As I can see, many beginners have hard time because they are looking for too easy time: try to find a small piece of knowledge which is totally useful if taken without connection with others, or trying to do things without mastering prerequisites. Too many people trying OOP (but actually not doing any OOP) without understanding how a variable work (which is not as trivial as many think)...

—SA
pasztorpisti 7-Apr-13 12:59pm    
So true. The page I linked is also a bigger tutorial, not just one about inheritance.
Within main(), a.i is invisible. a is accessible because it's a variable declared within main(), but if i is declared either protected or private, you can't access it.

protected means that the class and all derived classes from the class, a in this case, will gain access to the specified member. Non-deriving classes and global functions will not have access to protected members of the class in question.

So, because main is not a class deriving from a and is in fact a global function, you can not access the protected member i.

I hope this answer isn't too late, and I hope you do some research. This isn't too tough of a concept.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900