C++ Resources

Updated on Nov 1  2020



C++OOP Basics

Deborah R. Fowler



OOP for C++  

Posted on Oct 19  2019
Updated Oct 20  2019
 
Overview

There are many resources for programming, but I did find many students drawn toward video presentation. I have created an overview of OOP giving the viewer a quick introduction to OOP and the four principles associated with it as well as walking thru the syntax. A video version is available in full below.



Below is a written description with links to other articles as well.

OOP or Object-oriented programming is an approach to solving programming problems. The solution is thought of in terms of entities that have data and functions. There are multiple terms used for these concepts. An object/instance is the particular entity and it has data/data members/fields/attributes/properties along with member functions/procedures/methods.

The functions in a class all have access to the data of that class so there is no need to pass information in that is already part of the object.

OOP is the favored paradigm over Structured, also called Procedural programming. Many languages support OOP, related to my site I will explore C++ and Python. Vex does not support OOP at this time. If you are interested, there is a comparison of OOP programming languages at
https://en.m.wikipedia.org/wiki/Comparison_of_programming_languages_(object-oriented_programming)

A class is the definition or "blueprint". It defines what the objects will look like. The object is an instance of that type.

Other resources for OOP can be found at

The fundamental idea behind OOP is that the data structures are designed in a way to help a programmer think of their solution in terms of related entities. It is a plan and not just data and functions put together. There are four main features related to OOP.

EncapsulationMISSING IMAGE wrap your data and functions together as a unit. A class becomes a "blue print" of what your object will contain.
An object or instance of a class is where the term Object Oriented Programming (OOP) stems from. The idea of encapsulation is to protect or hide the data. We can also assign access rules to restrict who can and cannot see the data. There are three in C++, public, private and protected.

Abstraction is the idea that you are thinking about your solution at a higher level. I have heard it described as "don't sweat the details".

Inheritance is similar to a linux directory tree structure and is often drawn as a tree. A class can inherit properties from another. The child class (derived) inherits the properties of the parent (base) class.

Polymorphism is the ability to have the program determine which operation you want to use based on what the object type is. In particular the type of data being "pointed" to. We will describe pointers below. This leads to more elegant code.


CLASS SYNTAX

We define a class called Muppet which has energy and the ability to talk.
MISSING IMAGE


CONSTRUCTORS

A constructor is a function that is called automatically when an instance is made. It is commonly used to initialize data. In the example below, any instance will start with energy level 20.
It is identified by naming it the same name as the class.

MISSING IMAGE

In addition to the above syntax, C++ provides another way of setting the values of member data. This will come in handy later on with inheritance when we have derived classes calling base class constructors (more about those terms when we describe inheritance.
The syntax would look at follows:
MISSING IMAGE
If there were more than one value a comma is used to separate them

MISSING
        IMAGE


Getter and Setter functions

With the notion of access levels (right now we are using public) we have functions that are referred to as getter and setter functions. These are NOT like a constructor in that they are not special except by convention. Data can be marked as private, in which case only the object of the class can access it so function are defined to allow restricted access to "get" or "set" the data inside the class function only. This provides more control.

We will return to the concept of access levels when we visit inheritance.


DESTRUCTORS ~

Similar to the concept of constructors, there are also destructors that automatically are called when an object is deleted. Here is an example destructor.
It is identified by naming it the same as the class but with a ~ prefix.

MISSING IMAGE

This works with the syntax we have used thus far but the object is only going away when we end our program. There is a very easy way to delete during runtime and that is if we have a pointer to the object.
Creating our objects using the "new" function makes this easy.
The new function is described below under the pointers section.

Next up - Pointers