answersLogoWhite

0


Best Answer

Any member functions and data members declared as 'private' in a class, can only be accessed directly by functions within the class.

They cannot be accessed directly by derived objects, nor from anywhere outside an object of the class, such as from the Main function.

To access private class members, you must rely on what are called accessor functions. Accessor functions are functions inside the class, either public or protected, which automatically have access to private members.

If a function from Main, or elsewhere outside the class hierarchy, needs access, then you need to use publicaccessor functions. For derived class access, you can use protected accessor functions.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you access private functions of a class from the Main function in Cpp?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When is a friend function compulsory?

A normal member function has the following qualities: 1. Has private access to the class. 2. Is scoped to the class. 3. Can be invoked on an instance of the class. Static functions have the first two qualities only. Friend functions have the first quality only. It therefore follows that friend functions are only compulsory when you need quality 1 only.


How can friend function be implemented?

All class member functions have the following qualities: 1. Private access to the class members. 2. Scoped to the class. 3. Has an implicit 'this' pointer. Static member functions only have the first two qualities while friend functions only have the first quality. Friend functions must be defined outside of the class in which they are declared a friend -- only the function prototype is required in the friend declaration.


What are the privileges granted to friend function?

Friend functions (and classes) have private access to the classes that declare them as friends. Although they have the same access rights as members of the class itself, friends are not themselves members of the class and cannot be inherited.


How does friend operator function differ from member operator function?

In C++, we know that private members cannot be accessed from the outside class.That is a non-member function cannot have an access to the private data of a class.However there could be a situation where we would like two classes to share a particular function.For example consider a case where two classes, manager and scientist have been defined.We would like to use a function income_ tax () to operate on the objects of both these classes.In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:Class ABC{………..Public:……..……..Friend void xyz (void); //declaration};When the function is logically coupled with the class (like your maze connectedness example)When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the header for good example) .


When operator function is declared as friend function?

Ideally, never. Friend functions should only be employed when a function (whether an operator overload or not) requires private access to a class, and it is not otherwise possible to provide a public interface without unduly undermining the class encapsulation. However, as programmer, it is your responsibility to ensure all friend functions adhere to the same class rules (which you yourself define) as do the members of your class, even though friends are not regarded as being members of the class. Ultimately, if you have no control over the friend function implementation, then you must not allow that function to be a friend of your class, as this will seriously undermine the encapsulation.

Related questions

Is it possible to access the private variable in CPP from outside the class?

Private variables can only be accessed from outside of a class by using any public function of that same class. Or this can be accomplished by using Friend functions.


When is a friend function compulsory?

A normal member function has the following qualities: 1. Has private access to the class. 2. Is scoped to the class. 3. Can be invoked on an instance of the class. Static functions have the first two qualities only. Friend functions have the first quality only. It therefore follows that friend functions are only compulsory when you need quality 1 only.


Why friend are used in object oriented programing?

A friend function is a function that cannot be declared a member of a class but which requires private access to that class. For example, a function that operates upon two different classes cannot be a member of both classes, but if the function requires private access to both classes then it has to be a friend to at least one of them.To fully appreciate friend functions, consider that a non-static member function has the following three properties:Has private access to the class.Is scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. All other non-member functions have none of these properties.


How can friend function be implemented?

All class member functions have the following qualities: 1. Private access to the class members. 2. Scoped to the class. 3. Has an implicit 'this' pointer. Static member functions only have the first two qualities while friend functions only have the first quality. Friend functions must be defined outside of the class in which they are declared a friend -- only the function prototype is required in the friend declaration.


What are the privileges granted to friend function?

Friend functions (and classes) have private access to the classes that declare them as friends. Although they have the same access rights as members of the class itself, friends are not themselves members of the class and cannot be inherited.


How does friend operator function differ from member operator function?

In C++, we know that private members cannot be accessed from the outside class.That is a non-member function cannot have an access to the private data of a class.However there could be a situation where we would like two classes to share a particular function.For example consider a case where two classes, manager and scientist have been defined.We would like to use a function income_ tax () to operate on the objects of both these classes.In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:Class ABC{………..Public:……..……..Friend void xyz (void); //declaration};When the function is logically coupled with the class (like your maze connectedness example)When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the header for good example) .


Why private data members or functions are not inherited?

Because that's what private means. Private data members or functions are intended to be usable only in the base class, and the inheriting class can only access protected or public members or functions.


Which is public to all access class or struct or function or variable?

The default access specifier for a class is private. The default access specifier for a struct is public. It does not matter if it is a function or a variable.


When operator function is declared as friend function?

Ideally, never. Friend functions should only be employed when a function (whether an operator overload or not) requires private access to a class, and it is not otherwise possible to provide a public interface without unduly undermining the class encapsulation. However, as programmer, it is your responsibility to ensure all friend functions adhere to the same class rules (which you yourself define) as do the members of your class, even though friends are not regarded as being members of the class. Ultimately, if you have no control over the friend function implementation, then you must not allow that function to be a friend of your class, as this will seriously undermine the encapsulation.


Why we make a function friend in c plus plus?

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&); int m_data; }; void bar(foo& f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


What is friend function and its advantages?

The best way to understand the advantage of friend functions is to consider that a member function has the following properties:1. Private access to the class representation.2. Scoped to the class.3. Can be invoked upon an instance of the class (has a this pointer).A static member function only has the first two properties while a friend function only has the first property. All other function have none of these properties. This gives the programmer fine-grained control over which functions can access which parts of the class representation.