Members of Class in C++
Types of members inside a class
- The class can have two kind of members
- Static or Class Members
- Non-Static or instance member
Class members are shared members that anybody can access using the class name and do not require an object. Use static keyword to declare such members. Use scope resolution operator (::) to access such members.
Non-static or instance members always need an object to call them using dot operator (.)
All class or static members are stored in heap memory. Note that all objects are also stored in heap memory. On the other hand all the non-static or instance members and functions are stored in stack memory
We will understand this by an example. Let’s create a class num2 having two fields a and b, also a constructor to initialize these fields and a method as product() which returns product of a and b. Let’s also create another method as factorial() which takes a number as argument and returns the factorial of that number.
Using this pointer
- this pointer refers the current object of a class
- It is mainly used when parameter name and field names are same