How to Access Class Members Function In C++


Once a class is declared and created, we can create the variables of that class type with the help of the class name. In C++, the variables of a class are known as objects. Declaring an object is similar to declaring a variable of any basic data type. Once the object is declared, the memory space required for that object is allocated to it.
We can declare object by placing them immediately after the close brackets of the class declaration and before the semicolon. We can also declare objects within the main()function. An object invokes its member functions using the dot operator.

How to Access Class Members Function in C++

#include<iostream> using namespace std; class MyClass{ public: void codeverb(){ cout<<"Welcome to Codeverb"; } }; int main(){ MyClass s; s.codeverb(); return 0; }

Output:-

Welcome to Codeverb

Explanation of the program

In this program 's' is an object of class type MyClass.codeverb() is a public member function of the class MyClass. The dot operator used i.e, s.codeverb()means 's' accesses codeverb().