Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]
  • Table Of Contents Toc
  • Feedback & Rating feedback
Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]

Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]

By : Dorothy R. Kirk
5 (13)
close
close
Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]

Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]

5 (13)
By: Dorothy R. Kirk

Overview of this book

Even though object-oriented software design enables more easily maintainable code, companies choose C++ as an OO language for its speed. Object-oriented programming in C++ is not automatic – it is crucial to understand OO concepts and how they map to both C++ language features and OOP techniques. Distinguishing your code by utilizing well-tested, creative solutions, which can be found in popular design patterns, is crucial in today’s marketplace. This book will help you to harness OOP in C++ to write better code. Starting with the essential C++ features, which serve as building blocks for the key chapters, this book focuses on explaining fundamental object-oriented concepts and shows you how to implement them in C++. With the help of practical code examples and diagrams, you’ll learn how and why things work. The book’s coverage furthers your C++ repertoire by including templates, exceptions, operator overloading, STL, and OO component testing. You’ll discover popular design patterns with in-depth examples and understand how to use them as effective programming solutions to solve recurring OOP problems. By the end of this book, you’ll be able to employ essential and advanced OOP concepts to create enduring and robust software.
Table of Contents (30 chapters)
close
close
1
Part 1: C++ Building Block Essentials
6
Part 2: Implementing Object-Oriented Concepts in C++
13
Part 3: Expanding Your C++ Programming Repertoire
19
Part 4: Design Patterns and Idioms in C++
25
Part 5: Considerations for Safer Programming in C++

Revisiting function basics

A function identifier must begin with a letter or underscore and may also contain digits. The function’s return type, argument list, and return value are optional. The basic form of a C++ function is as follows:

<return type> FunctionName (<argumentType argument1, …>)
{
    expression 1…N;
    <return value/expression;>
}

Let’s review a simple function:

#include <iostream>
using namespace std;   // we'll limit the namespace shortly
int Minimum(int a, int b)
{
    if (a < b)
        return a;
    else
        return b;
}
int main()
{
    int x = 0, y = 0;
    cout << "Enter two integers: ";
    cin >> x >> y;
    cout << "The minimum is: " << Minimum(x, y) << endl;
    return 0;
}

In the preceding simple example, first, a function Minimum() is defined. It has a return type of int and it takes two integer arguments: formal parameters a and b. In the main() function, Minimum() is called with actual parameters x and y. The call to Minimum() is permitted within the cout statement because Minimum() returns an integer value; this value is passed along to the extraction operator (<<) in conjunction with printing. In fact, the string "The minimum is: " is first placed into the buffer associated with cout, followed by the return value from calling function Minimum(). The output buffer is then flushed by endl (which first places a newline character in the buffer before flushing).

Notice that the function is first defined in the file and then called later in the file in the main() function. Strong type checking is performed on the call to the function by comparing the parameter types and their usage in the call to the function’s definition. What happens, however, when the function call precedes its definition? Or if the call to the function is in a separate file from its definition?

In these cases, the default action is for the compiler to assume a certain signature to the function, such as an integer return type, and that the formal parameters will match the types of arguments in the function call. Often, the default assumptions are incorrect; when the compiler then encounters the function definition later in the file (or when another file is linked in), an error will be raised indicating that the function call and definition do not match.

These issues have historically been solved with a forward declaration of a function included at the top of a file where the function will be called. Forward declarations consist of the function return type, function name and types, and the number of parameters. In C++, a forward declaration has been improved upon and is instead known as a function prototype. Since there are many interesting details surrounding function prototyping, this topic will be covered in reasonable detail in the next chapter.

Important note

The specifier [[nodiscard]] can optionally be added to precede the return type of a function. This specifier is used to indicate that the return value from a function must not be ignored – that is, it must be captured in a variable or utilized in an expression. Should the function’s return value consequently be ignored, a compiler warning will be issued. Note that the nodiscard qualifier can be added to the function prototype and optionally to the definition (or required in a definition if there is no prototype). Ideally, nodiscard should appear in both locations.

As we move to the object-oriented sections in this book (Chapter 5, Exploring Classes in Detail, and beyond), we will learn that there are many more details and quite interesting features relating to functions. Nonetheless, we have sufficiently recalled the basics needed to move forward. Next, let’s continue our C++ language review with user defined types.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon