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.
The 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.
The 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.
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.
Linked 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.
Similar 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.
This is an example on how to use this linked list class.
Stacks and queues are both data structures that hold multiple elements, like an array or a linked list. In fact, both stacks and queues are often created using a linked lists object similar to example in the Linked Lists tab. The difference between a stack and a queue is the order in which they follow to add and remove elements.
Stacks follow the order called LIFO, which stands for Last In First Out. This means that the most recent element added to the stack is also the first element to be removed. A real world application that uses this conceptual logic is a stack of pancakes; typically the last pancake to be added on top of the stack is the first one to be eaten.
Queues follow the order called FIFO, which stands for First In First Out. This means that the least recent element added to the stack is the first element to be removed. A real world application that uses this conceptual logic is a line of people; typically the person who has been in the queue the longest is the next person to be served.
In C++, stacks and queues typically have these class functions in order to operate:
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.
These resources are online texts that the library has paid for and can be accessed with your PVAMU login from on or off campus. Click below to access the record and then access the full-text.
________________________________
These resources are online texts that the library has paid for. If there's a full-text that PVAMU hasn't purchased the rights to that you think we should have in our collection, please email me at mcmassaro@pvamu.edu and I'll do my best to acquire it.
________________________________
To access PVAMU's databases, first go to pvamu.edu/library. From here, scroll down until you see "Databases A to Z." From the "Databases A to Z" page, you can view and sort all our databases by Subject, Type, and Vendor.
Computer Science II (Course & Lab) is designed to be give students a further understanding of coding and C++. Concepts covered include:
This guide will cover definitions and examples of all above listed concepts as well as library and online resources that will be useful for PVAMU computer science students.
________________________________
Please contact me at mcmassaro@pvamu.edu if there's a resource you believe belongs on this guide or that you want PVAMU to acquire. As a collection development librarian for the engineering and computer science departments, I can purchase resources that advance student learning. Also remember to request an IR course for a presentation on library resources for your course.
John B. Coleman Library |
Library Hours
|