C++: Pointers in class

This entry is part 55 of 61 in the series C++

Pointers in C allow dynamic memory allocation.  In a dynamic variable like a pointer, the compiler will not need to pre-allocate a chunk of memory.  Memory is allocated when needed during run time.

An example of a predefined memory allocation is in declaring an array where a fix set of memory will be allocated for the variable.

The same goes to defining pointers for a class.  The memory space will be allocated dynamically in real time when the program is running.  Now you do not have to know the memory needed in advance.

#include <iostream>
#include <string>

using namespace std;

class Rock {
    int Show();

int Rock::Show() {

    cout << "Wonderwall by Oasis";
    cout << endl;
    return 0;
int main() {

    Rock *ptrRock; // instance as a pointer declaration
    return 0;


Series Navigation<< C++: Declaring an object instance after a class scope
C++: Pointer to access methods in derived classes >>