/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is Public member Function in C++ ?


All the class members declared under public will be available to everyone.the data members and memeber function defined using access specifier public,can be accessed by other non-member function also.A part from that the member function of a class can access data members whether they are declared as private or public.

Q.) Example of Public Members Function in C++ .
    
#include < iostream>
using namespace std;

// class definition
class Circle {
public:
	double radius;

	double compute_area()
	{
		return 3.14 * radius * radius;
	}
};

// main function
int main()
{
	Circle obj;

	// accessing public data member outside class
	obj.radius = 5.5;

	cout << "Radius is: " << obj.radius << "\n";
	cout << "Area is: " << obj.compute_area();
	return 0;
}

Output
Radius is: 5.5,
Area is: 94.985