Unit 4: Object-Oriented Programming
In this unit, you will learn how to design a class, which is an expanded concept of a data structure that can hold both data and functions. An object is an instantiation of a class, so a class would be the type, and an object would be the variable. Next, we will learn how to handle private and protected members of a class, which is important for sound class design. This unit covers a key feature of C++ classes: inheritance. Inheritance allows classes to inherit objects and functions from other classes. In this unit, we will learn how classes can inherit members from more than one class. We will end this unit with the study of polymorphism or the ability to create a variable, a function, or an object that has more than one form. This brings object-oriented methodologies to their full potential.
Completing this unit should take you approximately 8 hours.
Upon successful completion of this unit, you will be able to:
- define and use constructors and deconstructors;
- create overloading operators;
- define and use the keyword "this" and use the static members appropriately;
- design and appropriately use friend functions and classes;
- use class inheritance to design better code;
- explain how polymorphism is achieved through C++ code; and
- create accessors, mutators, and access modifiers.
4.1: Class Design
Classes are what separate the capabilities of C from that of C++. Classes are the object-oriented aspect of programming.
To think about classes, consider writing utensils. A pen, a pencil, and a marker are all writing utensils. However, when a pen uses ink; a pencil uses graphite, and so forth. The color of the pencil or the pen might be different, but their shapes and behaviors are the same. This is what object-oriented programming is about. We can define the characteristics of a writing utensil and then create specific instances of each object based on the unique characteristics of each instance of the object, without having to define their common characteristics each time.This video is a great demonstration of the use of classes.
4.2: Inheritance between Classes
Watch this video on inheritance between classes.
This article demonstrates some examples of inheritance.
Static attributes allow us to store one value for a variable that is consistent through all instances of a class. Read this article to see how this is implemented.
4.3: Polymorphism
This article reviews inheritance and explains polymorphism.
Polymorphism is not easy to grasp. This video provides a solid explanation of the concept.
Operators play an important role in computer programming. By using it, you can change the behavior of an operator based on the types of its arguments.
Review these slides, which summarize the concepts you have been learning thus far.
Unit 4 Exercises
Complete this assignment by copying the text into Eclipse. Compile and run the code, and then complete assignment 6. Create, compile, and run the code to check your understanding of this concept. The instructions for both provide details for running in a command line environment. Ignore this information and create the file in your Eclipse environment as you usually do.