Access specifiers

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?
1
2
3
4
 int main(){
     Private:
       int a;
       //so on... 


Thanks!
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?
Access specifiers in C++ have 2 contexts: (a) member-access-specifier of a class/struct or union and (b)
define the accessibility of inherited members of the subsequent base specifiers
... http://en.cppreference.com/w/cpp/language/access

So, in short, the answer is no:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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
Last edited on
@gunnerfunner
I did try it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

void enterdisplay(int);

int main(){
  Private:
      int a;
  Public:
      endterdisplay(a);

return 0;

}

void enterdisplay(int n){
cout<<"Enter value:- ";
cin>>n;
cout<<"Value is:- "<<n;
}


I did get an error
1
2
3
4
5
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)?
@gunnerfunner
yup the code for the second question is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

using namespace std;

class x{
    int a;

};
int main(){
    x ob;
cout<<"Enter number:- ";
cin>>ob.a;
cout<<ob.a;
return 0;

}


output is:-
1
2
3
4
5
6
7
8
9
10
11
12
D:\C++\ascend odd\main.cpp:6:9: error: 'int x::a' is private
     int a;
         ^
D:\C++\ascend odd\main.cpp:12:9: error: within this context
 cin>>ob.a;
         ^
D:\C++\ascend odd\main.cpp:6:9: error: 'int x::a' is private
     int a;
         ^
D:\C++\ascend odd\main.cpp:13:10: error: within this context
 cout<<ob.a;
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace 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:
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>

constexpr auto PI = 3.14;

class Shape
{
    public:
    virtual double showRadius() const = 0;
};

std::vector <std::unique_ptr<Shape>> getCircles()
{
    class Circle : public Shape
    {
        private:

        double m_radius;

        public:
        Circle (const double& radius) : m_radius (radius) {}
        virtual double showRadius ()const {return m_radius;}
    };
    std::vector <std::unique_ptr<Shape>> circles;

    std::unique_ptr<Shape> firstCircle(new Circle(4));
    circles.push_back(std::move(firstCircle));
    std::unique_ptr<Shape> secondCircle(new Circle(5));
    circles.push_back(std::move(secondCircle));

    return circles;
}

int main()
{
    for (auto& elem : getCircles())
    {
        std::cout << elem -> showRadius() << "\n";
    }
}

Output
1
2
4
5


edit: to show circle radius, not area
Last edited on
Topic archived. No new replies allowed.