How to use pointer within a Class in C++ Programming

Posted By: GOVERNOR - 4:54:00 PM

Pointers can be used inside a class for the following reasons:
  • Pointers save memory consumed by objects of a class.
  • Pointers can be used to allocate memory dynamically i.e., at run time.

Following program demonstrates the use of pointers within a class:

In the above program marks is a pointer inside the class Student. It is used to allocate memory dynamically inside the get_marks() function with the help of new  operator.

Local Classes


A class which is declared inside a function is called a local class. A local class is accessible only within the function it is declared. Following guidelines should be followed while using local classes:
  • Local classes can access global variables only along with scope resolution operator.
  • Local classes can access static variables declared inside a function.
  • Local classes cannot access auto variables declared inside a function.
  • Local classes cannot have static variables.
  • Member functions must be defined inside the class.
  • Private members of the class cannot be accessed by the enclosing function if it is not declared as a friend function.

Below example demonstrates a local class:

About GOVERNOR

Create your own MMO and MMORPG game server or find free MMORPG servers. Download the latest MMORPG server files or sell your game gold and mmorpg account. We also have programming tutorials , graphic design tutorials, gaming discussions and offer hardware and software support.

0 comments:

Post a Comment

Copyright © 2015 TECHNICORE

Designed by Shared By Way Templates