C++ Core Knowledge To Get The Ability To Read (Continued)

The key topics are: pointer, reference, virtual method and polymorphism.

What is a pointer? It contains the address of variable in the computer’s internal memory – RAM; it itself is represented by a variable in bytes looking like 0x7fff6472e92c. There are two signs to know in depth to fully understand how to use pointer and reference- * and &. put & in front of a variable to reference this variable; when define a new variable by referencing, put a * after the data type.

In above image, we can see p is a pointer to hold the address of x, we can put *ahead of p to “dereference” pointer p or to get the value contained in that address through pointer p. So, cout << *p << endl, we’ll get 25. To look at it from another angle, we can see *p is an alias of x. When &(address) sign and *(dereference) sings are side by side, they cancel out each other.

A pointer can be passed to a function, which basically is to pass a reference that can be changed. According to Derek, “When deciding on whether to use pointers or references // Use Pointers if you don’t want to initialize at declaration, or if // you need to assign another variable // otherwise use a reference”.

// ———- POINTERS ———-
// When data is stored it is stored in an appropriately sized box based
// on its data type

int myAge = 39;
char myGrade = ‘A’;

cout << "Size of int " << sizeof(myAge) << endl;
cout << "Size of char " << sizeof(myGrade) << endl;

// You can reference the box (memory address) where data is stored with
// the & reference operator

cout << "myAge is located at " << &myAge << endl;

// A pointer can store a memory address
// The data type must be the same as the data referenced and it is followed
// by a *

int* agePtr = &myAge;

// You can access the memory address and the data
cout << "Address of pointer " << agePtr << endl;

// * is the dereference or indirection operator
cout << "Data at memory address " << *agePtr << endl;

int badNums[5] = {4, 13, 14, 24, 34};
int* numArrayPtr = badNums;

// You can increment through an array using a pointer with ++ or —
cout << "Address " << numArrayPtr << " Value " << *numArrayPtr << endl;
numArrayPtr++;
cout << "Address " << numArrayPtr << " Value " << *numArrayPtr << endl;

// An array name is just a pointer to the array
cout << "Address " << badNums << " Value " << *badNums << endl;

// When you pass a variable to a function you are passing the value
// When you pass a pointer to a function you are passing a reference
// that can be changed

makeMeYoung(&myAge);

cout << "I'm " << myAge << " years old now" << endl;

// & denotes that ageRef will be a reference to the assigned variable
int& ageRef = myAge;

cout << "ageRef : " << ageRef << endl;

// It can manipulate the other variables data
ageRef++;

cout << "myAge : " << myAge << endl;

// You can pass the reference to a function
actYourAge(ageRef);

cout << "myAge : " << myAge << endl;

Virtual Method’s explanation is ” A virtual function a member function which is declared within base class and is re-defined (Overriden) by derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the function. “

Polymorpism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

class Animal{
public:
void getFamily() { cout << "We are Animals" << endl; }

// When we define a method as virtual we know that Animal
// will be a base class that may have this method overwritten
virtual void getClass() { cout << "I'm an Animal" << endl; }
};

class Dog : public Animal{
public:
void getClass() { cout << "I'm a Dog" << endl; }

};

class GermanShepard : public Dog{
public:
void getClass() { cout << "I'm a German Shepard" << endl; }
void getDerived() { cout << "I'm an Animal and Dog" < getClass();
}

int main(){

Animal *animal = new Animal;
Dog *dog = new Dog;

// If a method is marked virtual or not doesn’t matter if we call the method
// directly from the object
animal->getClass();
dog->getClass();

// If getClass is not marked as virtual outside functions won’t look for
// overwritten methods in subclasses however
whatClassAreYou(animal);
whatClassAreYou(dog);

Dog spot;
GermanShepard max;

// A base class can call derived class methods as long as they exist
// in the base class
Animal* ptrDog = &spot;
Animal* ptrGShepard = &max;

// Call the method not overwritten in the super class Animal
ptrDog -> getFamily();

// Since getClass was overwritten in Dog call the Dog version
ptrDog -> getClass();

// Call to the super class
ptrGShepard -> getFamily();

// Call to the overwritten GermanShepard version
ptrGShepard -> getClass();

return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.