Need assistance regarding accessing the private function

Hi,

I need to ask about accessing the private function, which I am unable to do after multiple attempts. I am new to C++ programming. Your assistance would be of great help. Thanks


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>

using namespace std;

class newclass
{
public:

    int jama (int a, int b)
    {
        int z = a + b;
        return z;
    }

    int zarab (int a, int b)
    {
        int x = a * b;
        return x;
    }

    int hasily()
    {
        int y;
        return y;
    }

private:

    int taqsim (int a, int b) //Unable to get value from this function.
    {
        int y = a / b;
        return y;
    }
};


int main ()
{
    int a = 0;
    int b = 0;
    int c = 0;
    int k = 0;

    cout << "Enter a number: ";
    cin >> a;
    cout << "Enter another number: ";
    cin >> b;

    newclass nc;
    int x = nc.jama(a,b);
    cout << x << endl;

    cout << "Enter a number: ";
    cin >> c;

    newclass nz;
    int j = nz.zarab(c,x);
    cout << j;

    cout << "Enter dividend: ";
    cin >> k;

    newclass nd;
    cout << nd.taqsim(j,k);


    return 0;


}
Last edited on
1
2
3
private:    // <==== LOOK

int taqsim (int a, int b)
Private function can only be access from function of the same class. That's the point of private:. If you want to access a function form another outside of the class (like main()) make it public.

You may take a look here:

https://www.tutorialspoint.com/cplusplus/cpp_class_access_modifiers.htm
Last edited on
@zarrar,
Could you explain why exactly you want to make this function private? (i.e. inaccessible from outside the class). Also, why have you put these functions inside a class anyway? This isn't java. Your class has no data members and nothing to change the state of. There is certainly no purpose to creating multiple newclass objects.

I can't quite work out your line of thinking. Have you programmed in some different language before and are trying to replicate it in C++?

BTW, please put your code within code tags, either manually by typing
[code] and [/code] at beginning and end, respectively, or by selecting all the code and using the first item on the format menu. (Doesn't work directly on first posting, but is easily edited afterwards.)

Your function hasily() can't return a non-initialised value. I assume that it's just a placeholder for now.
Last edited on
@lastchance
I am actually a beginner, and playing with C++, trying to find out what C++ has to offer.
There is nothing particular I am doing but I am doing so. Could you please help me out with this now? Means, if you have got what I am trying to do with this code, can the private function give the output to a public function so that I may use it in main()...?
Why not just make it a public function?
@coder777,
Thanks dear.
zarrar12 wrote:
can the private function give the output to a public function so that I may use it in main()...?


Yes (not that I think this a good solution here).

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
#include <iostream>
using namespace std;

class newclass
{
public:                        // Everything here can be called from outside
   int pub_taqsim( int a, int b )
   {
      return taqsim( a, b );   // pub_taqsim IS a member of the class, so CAN call its private functions
   }

private:                       // Everything here is not (directly) accessible to the outside world
   int taqsim ( int a, int b )
   {
      int y = a / b;
      return y;
   }
};


int main ()
{
   newclass nd;
   int j = 10, k = 3;
   cout << nd.pub_taqsim( j, k ) << '\n';     // Calls pub_taqsim ... which itself calls taqsim
// cout << nd.taqsim( j, k ) << '\n';         // If un-commented, this should produce a compile-time error; taqsim() is private
}


Note that, in c++ (but not all languages), everything in a class is private by default. Everything in a struct is public by default. (At the moment) that is the only difference between them.
Last edited on
@zarrar12,

You have good advice thus far, so I thought I'd introduce what tutorials eventually get to about this.

A class should represent a concept that does some work. It is a metaphor for an actual, physical object in mechanical engineering. If we made cars the way we made software before object oriented languages, we may introduce a bug such that a problem with the radio could blow a tire. That sounds absurd for a real car, but for software built without any kind of separation between components we actually find such bugs where unrelated systems interfere with each other.

In the bargain we gain leverage through organization. Objects (instantiated classes) represent a type (like a physical object or theoretical concept) which react to usage in contexts somewhat "automatically" from the perspective of the consumer of the class. When you make a class, you're the engineer creating all that is inside it. In a car this is like the transmission, as opposed to the suspension, or the brakes.

If you created a "black box" of electronics which performed some technical function, like a Geiger counter that measures radiation, the "box" would have some readout dials, maybe a knob or two to turn, possibly connectors for power, input or output. What is inside is hidden by the enclosure. Users don't need to know what's inside. The "interface" is what is on the outside of the "box". As the engineer creating the "box", you don't want the user to fiddle with the electronics inside.

Classes do this in software. It isn't obvious to new students because they are generally creating the classes and using them. Like the engineer creating that box, there's no reason NOT to open it up and fiddle with the internals.

Yet, once that "box" of electronics is finished and works well, even the engineer doesn't want to HAVE to open the box up to use it. It should be a complete product.

One of the ways C++ allows us to "seal the box" is to make data and functions private. They are part of the interior that the user shouldn't fiddle with. The general subject is called encapsulation.

This isn't the only "thing" about object oriented design you should know, but it is one of the basics. When we can fashion objects of good design we create software that fits together more like a machine than merely a collection of methods and data.

A typical use case is in games. We all recognize that if we have a few numbers A, B and C (be they ints or floats), A = B * C produces obvious results. In a game, however, we may need to perform a transformation by rotating a point in 3D (a vector3) by a quaternion (a structure of 4 values defining orientation - the angles of the object). If B is a vector3, A is a vector3 and C is a quaternion, A = B * C can perform this operation "automatically". The actual process is a complex process of several multiplcations/additions/etc which take 3 values in B and 4 values in C to produce a vector3 of the resulting 3 values. Since the vector3 and quaternion can be classes (instantiated as objects), they "know" how to perform this math when we provide the "*" operator as a function.

This suggests a second layer of the purpose behind objects: it is the way to define relationships between different objects (a vector3 multiplied by a quaternion, for example) and thus selecting methods based on their types.

You don't drive a bicycle the way you drive a car. Even the language is suggestive in that we don't usually say "drive" for a bicycle. Humans naturally think in terms of selection of features, usage and process, based on the "type" of the object. This is implemented in C++ through language to give leverage in a wide variety of ways, and this question brought up at least 2 of them.

@lastchance,

Thanks so much dear... :)
Your solution clarified my concept and resolved the problem.
Topic archived. No new replies allowed.