Organizing the Parts of a Program in C++
Find out why you should learn C++, how C++ programs are organized, and understand the benefits of comments and functions in C++.
Although it recently turned 37, the C++ programming language has aged a lot better than some other things that came out in the late 1970s. Unlike disco, oil embargoes, shag carpet, and avocado-colored refrigerators, C++ is still in vogue today. It remains a world-class programming language.
The reason for its surprising longevity is that C++ makes it possible to create fast executing programs with a small amount of code that can run on a variety of computing environments. Today’s C++ programming tools enable the creation of complex and powerful applications in commercial, business, and open source development.
Reasons to Use C++
During the seven decades of the computing age, computer programming languages have undergone a dramatic evolution. C++ is considered to be an evolutional improvement of a language called C that was introduced in 1972.
The earliest programmers worked with the most primitive computer instructions: machine language. These instructions were represented by long strings of ones and zeroes. Assemblers were devised that could map machine instructions to human-readable and manageable commands such as ADD and MOV.
The instructions that make up a computer program are called its source code.
In time, higher-level languages were introduced such as BASIC and COBOL. These languages made it possible for programmers to begin to craft programs using language closer to actual words and sentences, such as Let Average = .366. These instructions were translated back into machine language by tools that were called either interpreters or compilers.
An interpreter-based language translates a program as it reads each line, acting on each instruction.
A compiler-based language translates a program into what is called object code through a process called compiling. This code is stored in an object file. Next, a linker transforms the object file into an executable program that can be run on an operating system.
Because interpreters read the code as it is written and execute the code on the fly, they’re easy for programmers to work with. Compilers require the more inconvenient extra steps of compiling and linking programs. The benefit to this approach is that the programs run significantly faster than programs run by an interpreter.
For many years, the principal goal of programmers was to write short pieces of code that would execute quickly. Programs needed to be small because memory was expensive, and they needed to be fast because processing power also was expensive. As computers have become cheaper, faster, and more powerful and the cost and capacity of memory has fallen, these priorities diminished in importance.
Today, the greatest expense in programming is the cost of a programmer’s time. Modern languages such as C++ make it faster to produce well-written, easy-to-maintain programs that can be extended and enhanced.
Styles of Programming
As programming languages have evolved, languages have been created to cater to different styles of programming.
In procedural programming, programs are conceived of as a series of actions performed on a set of data. Structured programming was introduced to provide a systematic approach to organizing these procedures and managing large amounts of data.
The principal idea behind structured programming is to divide and conquer. Take a task that needs to be accomplished in a program, and if it is too complex, break it down into a set of smaller component tasks. If any of those tasks is still too complicated, break it down into even smaller tasks. The end goal is tasks that are small and self-contained enough to be easily understood.
As an example, pretend you’ve been asked by this publisher to write a program that tracks the average income of its team of enormously talented and understatedly charismatic computer book authors. This job can be broken down into these subtasks:
Find out what each author earns.
Count how many authors the publisher has.
Total all their income.
Divide the total by the number of authors.
Totaling the income can be broken down into the following:
Get each author’s personnel record.
Access the author’s book advances and royalties.
Deduct the cost of morning coffee, corrective eyewear and chiropractic care.
Add the income to the running total.
Get the next author’s record.
In turn, obtaining each author’s record can be broken down into these subtasks:
Open the file folder of authors.
Go to the correct record.
Read the data from disk.
Although structured programming has been widely used, this approach has some drawbacks. The separation of data from the tasks that manipulate the data becomes harder to comprehend and maintain as the amount of data grows. The more things that must be done with data, the more confusing a program becomes.
Procedural programmers often find themselves reinventing new solutions to old problems instead of producing reusable programs. The idea behind reusability is to build program components that can be plugged into programs as needed. This approach is modeled after the physical world, where devices are built out of individual parts that each perform a specific task and have already been manufactured. A person designing a bicycle doesn’t have to create a brake system from scratch. Instead, she can incorporate an existing brake into the design and take advantage of its existing functionality.
This component-based approach became available to computer programmers for the first time with the introduction of object-oriented programming.
C++ and Object-Oriented Programming
C++ helped popularize a revolutionary style of programming with a funny acronym: OOP.
The essence of object-oriented programming is to treat data and the procedures that act upon the data as a single object—a self-contained entity with an identity and characteristics of its own.
The C++ language fully supports object-oriented programming, including three concepts that have come to be known as the pillars of object-oriented development: encapsulation, inheritance, and polymorphism.
When the aforementioned bike engineer creates a new bicycle, she connects together component pieces such as the frame, handlebars, wheels, and a headlight (baseball card in the spokes optional). Each component has certain properties and can accomplish certain behaviors. She can use the headlight without understanding the details of how it works, as long as she knows what it does.
To achieve this, the headlight must be self-contained. It must do one well-defined thing and it must do it completely. Accomplishing one thing completely is called encapsulation.
All the properties of the headlight are encapsulated in the headlight object. They are not spread out through the bicycle.
C++ supports the properties of encapsulation through the creation of user-defined types called classes. A well-defined class acts as a fully encapsulated entity that is used as an entire unit or not at all. The inner workings of the class should be hidden on the principle that the programs which use a well-defined class do not need to know how the class works. They only need to know is how to use it. You learn how to create classes in Hour 8, “Creating Basic Classes.”
Inheritance and Reuse
Now we’re starting to learn a little more about our bike engineer. Let’s call her Penny Farthing. Penny needs her new bicycle to hit the market quickly—she has run up enormous gambling debts to people who are not known for their patience.
Because of the urgency, Penny starts with the design of an existing bicycle and enhances it with cool add-ons like a cup holder and mileage counter. Her new bicycle is conceived as a kind of bicycle with added features. She reused all the features of a regular bicycle while adding capabilities to extend its utility.
C++ supports the idea of reuse through inheritance. A new type can be declared that is an extension of an existing type. This new subclass is said to derive from the existing type. Penny’s bicycle is derived from a plain old bicycle and thus inherits all its qualities but adds additional features as needed. Inheritance and its application in C++ are discussed in Hour 16, “Extending Classes with Inheritance.”
As its final new selling point, Penny Farthing’s Amazo-Bicycle behaves differently when its horn is squeezed. Instead of honking like an anguished duck, it sounds like a car when lightly pressed and roars like a foghorn when strongly squashed. The horn does the right thing and makes the proper sound based on how it is used by the bicycle’s rider.
C++ supports this idea that different objects do the right thing through a language feature called function polymorphism and class polymorphism. Polymorphism refers to the same thing taking many forms, and is discussed during Hour 17, “Using Polymorphism and Derived Classes.”
You will learn the full scope of object-oriented programming by learning C++. These concepts will become familiar to you by the time you’ve completed this full 24-hour ride and begun to develop your own C++ programs.
Disclaimer: You won’t learn how to design bicycles or get out of gambling debt.