Just a noob question but can access specifiers like Public, Private and Protected be placed inside functions rather than structures and classes only?
Like placing Private scope inside the main function?
Also,
What if I declare a local class inside say int main with Private members, so will the function int main be able to access those?
And, what if the object of a class is a local object, can the function say int test be able to access the private members of that class?
#include <iostream>
namespace test
{
int myAccess()
{
private:
int a;
std::cin >> a;
return a;
}
}
int main()
{
std::cout << test::myAccess;
}
////
test.cpp|6|error: expected primary-expression before 'private'|
test.cpp|8|error: 'a' was not declared in this scope|
You are asking some interesting questions but did you try writing any code to test your ideas in the first instance? Often times the compiler error messages (if any) can help you find out yourself what works and what doesn't
edit: compiler error keyword searches also help at times to find answers by and for yourself
mingw32-g++.exe -Wall -fexceptions -g -c "D:\C++\accessspecifier with main func\main.cpp" -o obj\Debug\main.o
D:\C++\accessspecifier with main func\main.cpp: In function 'int main()':
D:\C++\accessspecifier with main func\main.cpp:11:22: error: 'endterdisplay' was not declared in this scope
endterdisplay(a);
^
though I am not sure if this is like a syntax type of error or the answer to my question (new to C++..still learning) so I turned to this place to confirm
Good, that's the first one and the error messages you got are in line with what I'd received and you have some explanations now about the usage of access specifications. Read the link I sent as well, its quite detailed.
Moving on to your second query, local class within int main &c, have you tried anything similar to above on that yet and, if so, what sort of error messages are you receiving (if any)?
The default access specifier for a class is private, hence why you can't access x::a, in the context of an object. Though, you can have accessors and mutators to get and set the values of private member variables. The process of purposefully restricting the access of member variables/functions is known as encapsulation and is one of the fundamental concepts in object oriented programming.
#include <iostream>
usingnamespace std;
class x
{
public:
int get_a() const { return a; }
int set_a( int i ) { a = i; }
private:
int a;
};
int main()
{
x ob; int n{};
cout << "Enter number:- ";
cin >> n;
ob.set_a( n );
cout << ob.get_a() << '\n';
}
What if I declare a local class inside say int main with Private members, so will the function int main be able to access those?
OP: your subsequent post did not cover this particular point and, if you are still interested, here's a description where the local class in defined inside a function (not int main() directly) and then accessed through int main().
The trick is to have the local class inherit from a (abstract) base class and have the function that scopes the derived class return pointer(s) to the base class: