Re: Fed up of a concept in c++
Quote:
Originally Posted by daemonakadevil
From outside the class you can't enter the pvt data of that that class but inside you can
In a general context I would understand that as trying to access protected (data)members of an instance of a class. Just as I have shown in my previous example.
Quote:
Originally Posted by daemonakadevil
I want to ask that which syntax to use to enter pvt data of class from outside so compiler will give me error that you can't enter pvt data of that class , not the object of that class.
Now you are talking about data that belongs to the class but not to an instance of a class.
So I guess that you mean private static data.
Code:
class Test {
private: // this time using the private keyword
static int priv_stat_int;
public:
void set_priv(int v );
static void stat_set_priv( int v );
};
int Test::priv_stat_int = 0; // initialisation
void Test::set_priv(int v ) {
priv_stat_int = v; // Ok inside the scope of Test
}
void Test::stat_set_priv( int v ){
priv_stat_int = v; // Ok inside the scope of Test
}
int main() {
Test a;
Test::priv_stat_int = 0; // Error outside of Test ( the scope is ::main )
a.priv_stat_int = 0; // Error outside of Test
a.set_priv(5); // Ok
Test::stat_set_priv(5); // Ok
a.stat_set_priv(5); // Ok
}
But then you are talking about entering data and not accessing data. Are you talking about readonly data ? ( const data ).
Kurt
Re: Fed up of a concept in c++
ok thanks for answering bro , can you please tell me that what does this means :-
In books its written that From outside the class you can't enter the pvt data of that that class but inside you can
I want to ask that which syntax to use to enter pvt data of class from outside so compiler will give me error that you can't enter pvt data of that class , not the object of that class.
Re: Fed up of a concept in c++
Quote:
I want to ask that which syntax to use to enter pvt data of class from outside so compiler will give me error that you can't enter pvt data of that class , not the object of that class.
Place your data under private access (or protected, but usually private is the right choice). In ZuK's example, this was done without explicitly using the private keyword as private access is the default for classes declared with the class keyword.
Re: Fed up of a concept in c++
Sh... I have done it again. I've edited my previous post and not added a new reply.
Sorry.
Kurt
Re: Fed up of a concept in c++
But then you are talking about entering data and not accessing data.
whats the difference btw entering & accessing data of class?
Re: Fed up of a concept in c++
Quote:
Originally Posted by daemonakadevil
But then you are talking about entering data and not accessing data.
whats the difference btw entering & accessing data of class?
From outside the scope of a class you cannot access private data ( or private member functions ) -> you can't read it or write it.
By entering data I would understand to change the data ( just write ).
Kurt
Re: Fed up of a concept in c++
So that means While you're inside the class (for me its while defining class or inside the class definition) you can access the pvt data of class and that's why you can access & enter the pvt data of objects of same class too cause they all are instances of the same class.
its all about your position that where you're declaring instances of classes inside the class or outside?
Bro please don't refuse, can you give me Y!M (so can talk to you there)??
thanks!!
Re: Fed up of a concept in c++
Quote:
Originally Posted by daemonakadevil
its all about your position that where you're declaring instances of classes inside the class or outside?
No it has nothing to do with where you create the instances.
It only depends on where you try to access the private members.
True. if you create an instance of a class inside the scope of that class then you can access its private data too but that is just because then the access happens from insde the scope of that class.
Quote:
Originally Posted by daemonakadevil
Bro please don't refuse, can you give me Y!M (so can talk to you there)??
Sorry. I don't understand
Kurt
Re: Fed up of a concept in c++
Quote:
Originally Posted by daemonakadevil
you're inside the class (for me its while defining class or inside the class definition)
Guess now I understand your mistake.
Inside or outside of the class has nothing to do with the physical position of the code. It's a matter of the scope of the code.
Maybe somebody can explain to you the meaning of scope. Guess that my english is not good enough to do that.
Kurt
Re: Fed up of a concept in c++
ZuK : Sorry. I don't understand
I mean can you please give me your yahoo mail so can talk on yahoo messenger !
Re: Fed up of a concept in c++
Quote:
Originally Posted by daemonakadevil
ZuK : Sorry. I don't understand
I don't know what there is to understand beyond the obvious.
1) Class members with access level as private means that only the class itself can access these private members.
2) Class members with access level as protected means that the class and derived classes have access to those protected members.
3) Class members with access level as public means that anyone can access those public members.
Given this, what is it that you are not understanding?
Regards,
Paul McKenzie
Re: Fed up of a concept in c++
Quote:
Originally Posted by Paul McKenzie
Quote:
Originally Posted by daemonakadevil
ZuK : Sorry. I don't understand
I meant what zuk has written in his 2nd or third last post !
Re: Fed up of a concept in c++
Quote:
Originally Posted by ZuK
Guess now I understand your mistake.
Inside or outside of the class has nothing to do with the physical position of the code. It's a matter of the scope of the code.
Maybe somebody can explain to you the meaning of scope. Guess that my english is not good enough to do that.
Kurt
Paul McKenzie , can you teach me what he wants teach me in this quote pf zuk!
Re: Fed up of a concept in c++
daemonakadevil:
I empathize. Most of us have been at this point in our study at one point, but for some of us it's been many years (for myself since 1987), with respect to the study of C++. We take these understandings for granted, to the point that when it comes to explaining them to someone new to the study, we have some trouble reminding ourselves to detail the basics - they are second nature to us.
When you've been writing phrases like "inside the class" or "outside the class" you are, rather colloquially, referring to the concept of scope - but without much of the understand that scope means to those of us familiar with these concepts, yet 'too' familiar to be able to articulate them meaningfully to you. Let me try....
Scope from ancient times (before objects) :)
Code:
int main(int, char **)
{
int d = function( 10 );
return 0;
}
int function(int b)
{
int z = b * 5;
return z;
}
This trivial, perhaps useless example of a function, uses a local variable z and a parameter b.
In main, where execution begins, BEFORE 'function' is called, the variables in function z have no scope. The function has not executed, so this seems obvious.
When main begins execution (you might think the execution pointer of the CPU has entered inside the opening brace of the function), the variable d will have scope. If you want to get microscopic about this, it gains scope once the CPU has executed the code which gave it room on the stack.
Now, let's assume that execution proceeds at the microscopically slow pace of a debugger we are manually stepping into 'function'. Now, the point of execution reached the line where z = b * 5 and has just executed it.
At this point z is said to be 'in scope'. It has space in memory (the stack), it can be accessed, used, written to, etc.
Note, however, that while the point of execution is 'inside function', the variable d, from the main function, is NOT in scope.
From 'inside' the scope of 'function' we can't reach or use d from the main function. It is not within the scope of the current point of execution.
Now, b, is also in scope. It's space was prepared as a parameter to the function, we can use it, access it, etc.
Note one property here. Although d, from the main function, is not in scope while the point of execution is inside 'function', it's space has not been affected. It exists, in that space was set aside for it before 'function' was called, and that space has not been relinquished. My point is, just because space for something has been allocated doesn't mean it is in scope.
This is why you're thinking in terms of "inside" or "outside" when you wrote earlier about privacy, which I'll get to shortly. Scope does seem to suggest the notion of a 'space' in which things exist, and if you're inside that space they are within scope, but if you're outside that space they are not.
In this previous example, when 'function' returns, the result is assigned to d. At that point in execution, when function has already returned, and execution is assigning the return value to d, the variable d has returned to scope. The variables z and b, which were 'inside function' are no longer in scope (and, they happen to no longer exist, too).
In earlier posts, this is when you were saying "when YOU'RE inside" - well, frankly, most of us do tend to think in terms of having crawled inside the computer, taken up a conductor's baton and proceed to direct the inner workings of the CPU. We tend to anthropomorphize the notions of CPU, variables, functions and the actions of a computer. It does seem to 'come alive'. However, to be clear, when you've been tempted to say "when you're inside the class" - you're really saying something along the lines of "when the point of execution is inside a member function".
Now, onto privacy and scope.
Code:
class test
{
private:
int z;
void ifunction();
public:
int j;
void pfunction();
};
int main(int, char **)
{
test t1;
t1.z = 0; // not allowed, privacy access error
t1.j = 0; // allowed, j is public
t1.ifunction(); // not allowed, privacy access error
t1.pfunction(); // allowed, pfunction is public
return 0;
}
void test::ifunction()
{
z = 0; // allowed
}
void test::pfunction()
{
ifunction(); // allowed, in scope of member function
z = 1; // also allowed
j = 2; // also allowed
}
Here we have code that won't compile, and comments illustrate why. If the 'not allowed' code were removed, the code should compile (I've not checked it, so typos could still be a problem I suppose).
When main begins execution, an instance of test is allocated on the stack, called t1. The instance of t1 is also said to be an object (or a t1 object).
Within the scope of main (that is, when the point of execution is inside main), the instance t1 is in scope. However, it is t1 in scope, not its member functions. That is, the point of execution is in main, and as such t1 is in main's scope. Any members of t1 (which are, by extension, members of the test class) are accessible only if they are declared public. As such, z is not accessible while the point of execution is in main. j is accessible, because it is public. Likewise, ifunction is not accessible because it is declared private.
Now, if the private accesses in the main function are removed such that the code could compile, execution would proceed 'into' pfunction. At the time that that point of execution is in pfunction, the members of t1 (the instance of test upon which pfunction is called) are accessible even if they are private. This is true of both pfunction and ifunction. Note, too, that the only way to call ifunction is from within the execution of another member function of t1.
Note that this is slightly different from the earlier C oriented example. C does not have privacy in it's structures, so the concept never comes to mind. However, in C++, where private and public members can be declared, the concept is relative to scope in this way: Access to private member variables and functions are only available to member functions when they are in scope - that is, when the point of execution is inside a member function.
This is a slight oversimplification. The keyword 'friend' can grant access to other classes, by name, to the private contents of a class, but that is a concept you can advance to once you have this concept better understood.
protected:
There is another level of access you can use with the keyword 'protected'. It is used just like private: is used.
In all the previous discussion, protected variables and functions are exactly like private variables and functions, but there is some difference when you inherit from a class.
Code:
class A
{
private:
int x;
protected:
int y;
public:
int z;
};
class B : public A
{
public:
void function()
{
// notes below refer to this point of execution
}
};
In this configuration, B derives from A. In A, you'll notice that y is protected. When scope is not in a member function of A (I know, there isn't one here, but pretend :) ) then y is not accessible - just as if it were private.
Now, in a member function of B (which is derived from A), y is accessible.
However, in a member function of B, x - which is private - is NOT accessible.
In other words, when you derive from a class, protected is a status that blends private and public concepts. A is a different class than B, it just so happens that B is derived from A. In A, member functions have access to private variables and functions just as before.
In B, member functions have access to A's public and protected variables and functions, but not it's private variables or functions. To all scope outside member functions of A or B, even protected variables and functions are inaccessible. They are 'as if private' to the outside world.
Within B, however, member functions of B are granted access to protected members of A. Not private, just protected.
The 'protected' status has no implication to classes NOT derived from A - it may as well be 'private' to all 'outside' scope.
I've had to handle a number of interruptions, and my time has run short so I'm not able to double check - please ask if this isn't clear and I'll come back.
Re: Fed up of a concept in c++
Just don't have words to say thanks to you, but wanna kiss you ummmhhhhaaaaaa!!!!!
but wanna say thanks for taking you precious time for a idiot n00b .
btw
can i have yahoo msg id?
love you bro , g