This OER is designed to give readers a further understanding of coding and C++. Concepts covered include:
Pointers are variables that store the memory address of a value. In order to get the memory address of a variable, use the & operator. But when declaring a pointer variable, use the * operator. Below is a code snippet demonstrating the difference between declaring normal variables and declaring pointers. With a basic understanding of pointers, we can begin to discuss its applications.
Classes are user-defined data types that are the main force behind object oriented programming in C++. Object oriented programming (OOP) is based on the concept of objects. An object is an instance or declaration of a class in C++ and it will typically hold a combination of methods (functions) and attributes (variables). This section of the guide will walk through a basic class I made in order to learn not only the basics of a class, but also good practices and methodologies to keep in mind. Attributes, Data Hiding, and AbstractionThe first part of making a class is defining what attributes it has. For this simple example, we will make an animal class that will have the attributes of its scientific name and its length in inches. Notice that we are simply declaring that this class has these attributes and are not initializing these attributes with values yet. More on that in the Constructor section of this tab. Something else important to note about these attributes is that they are private. This means that functions or algorithms outside of those declared within the class cannot alter these values. Making your class attributes private follows good object oriented programming methodologies, such as abstraction and data hiding. Data hiding can be defined as an object oriented programming technique that hides internal data of the object from other parts of the program. This also follows the concept of encapsulation, which promotes OOP by binding together the data and methods of an object, thus preventing outside interference and misuse. By making your attributes private and only modifiable by the object itself, you are following good OOP practices by using the concepts of data hiding. Abstraction can be defined as only extracting relevant information to hide the complexity of the object. Privatizing our class attributes not only prevents misuse of data via data hiding, but it also makes the object appear less complex to the user via abstraction. MethodsThe second and last part about making a class is defining its functions. Since we are following good OOP practices, we will need methods that will help us modify and help us access our private attributes from objects we create. We can also add more methods beyond these for added utility. We will also need constructors to define our attributes upon object creation. Notice that these methods are public; this is because we want to be able to create objects from our class and call these methods with each of our objects.
Defining and Using an Object
Let's combine what we learned from Pointers and Classes and make a linked list object. But first, let's define what a linked list is. NodesLinked lists are data structures that holds multiple elements, similar to an array, which have two key elements in every element (or node):
Before we make a linked list class, we need to define a struct with those two key attributes in order to create a node. One element of a linked list object will be a node, which holds data and the position in memory (using a pointer) of the next element or node. Linked List Class: Create and PrintSimilar to the animal class in our previous example, we are following good OOP practices by defining our variables (which are both Node pointers) for our class privately. The head and the tail are used to keep track of where we are in the linked list during linked list operations.
Next, we will go over the methods listed in this linked list. This linked list only has the ability to construct itself (initialized at NULL), add nodes to it, and print the nodes in order from head to tail. This linked list class doesn't have the ability to remove nodes or to check how many nodes are in this linked list, but it's worthy noting that a typical linked list would have the ability to do so and even more. This is just for a brief example.
Defining and Using a Linked ListThis is an example on how to use this linked list class.
Inheritance in C++ is when a new class is made that derives the properties from an older class. This makes creating and maintaining classes easier because we don't have to write completely new data members. A class that is derived from another class is called a sub class, while the class that the sub class is deriving from is the super class. Polymorphism is when you change the behavior of the super class with the sub class. In other words, if you create a method in a sub class that has the same name and parameters as a method in its super class, an object derived from the sub class will still prioritize its method despite it inheriting a method of the same name from its super class. Below is an example of one super class and two sub classes to help understand these definitions: Please note that I took away certain methods from Animal (relative to the Classes tab of this guide) simply to make the lessons of inheritance and polymorphism more clear. As this class stands, it is incomplete due to not having any modifying methods, among other attributes and methods. In this example, Animal is the super class while Chinchilla and Kookaburra are the sub classes. Both Chinchilla and Kookaburra inherit the private variables that Animal has, as well as its constructor and getScientificName() method. However, each class has a method called diet, which indicates the food that each animal eats. This is where polymorphism comes in. Do you think that Kookaburra and Chinchilla will inherit the Animal diet() method, or will it use its own? If you guessed that it will use its own, you are correct! This is essentially polymorphism in action. We can see that the sub class of Kookaburra not only inherited the method getScientificName(), but also inherited the super class Animal's constructor because its scientificName defaulted to Animalia upon object initialization. We can also see polymorphism in action due to the Chinchilla object prioritizing its own method of diet() over despite being a sub class of Animal. |