Skip to main content
main-content
Top

About this book

Beginning C++ is a tutorial for beginners in C++ and discusses a subset of C++ that is suitable for beginners. The language syntax corresponds to the C++14 standard. This book is environment neutral and does not presume any specific operating system or program development system. There is no assumption of prior programming knowledge.

All language concepts that are explained in the book are illustrated with working program examples. Most chapters include exercises for you to test your knowledge. Code downloads are provided for examples from the text and solutions to the exercises and there is an additional download for a more substantial project for you to try when you have finished the book.

This book introduces the elements of the C++ standard library that provide essential support for the language syntax that is discussed. While the Standard Template Library (STL) is not discussed to a significant extent, a few elements from the STL that are important to the notion of modern C++ are introduced and applied.

Beginning C++ is based on and supersedes Ivor Horton’s previous book, Beginning ANSI C++.

Table of Contents

Chapter 1. Basic Ideas

Abstract
I’ll sometimes have to make use of things in examples before I have explained them in detail. This chapter is intended to help when this occurs by giving you an overview of the major elements of C++ and how they hang together. I'll also explain a few concepts relating to the representation of numbers and characters in your computer. In this chapter you'll learn:
Ivor Horton

Chapter 2. Introducing Fundamental Types of Data

Abstract
In this chapter, I’ll explain the fundamental data types that are built into C++. You’ll need these in every program. All of the object-oriented capability is founded on these fundamental data types, because all the data types that you create are ultimately defined in terms of the basic numerical data your computer works with. By the end of the chapter, you’ll be able to write a simple C++ program of the traditional form: input – process – output.
Ivor Horton

Chapter 3. Working with Fundamental Data Types

Abstract
In this chapter, I expand on the types that I discussed in the previous chapter and explain how variables of the basic types interact in more complicated situations. I also introduce some new features of C++ and discuss some of the ways that these are used. In this chapter you’ll learn
Ivor Horton

Chapter 4. Making Decisions

Abstract
Decision-making is fundamental to any kind of computer programming. It's one of the things that differentiates a computer from a calculator. It means altering the sequence of execution depending on the result of a comparison. In this chapter, you’ll explore how to make choices and decisions. This will allow you to validate program input and write programs that can adapt their actions depending on the input data. Your programs will be able to handle problems where logic is fundamental to the solution. By the end of this chapter, you will have learned:
Ivor Horton

Chapter 5. Arrays and Loops

Abstract
An array enables you to work with several data items of the same type using a single name, the array name. The need for this occurs often - working with a series of temperatures or the ages of a group of people for example. A loop is another fundamental programming facility. It provides a mechanism for repeating one or more statements as many times as your application requires. Loops are essential in the majority of programs. Using a computer to calculate the company payroll, for example, would not be practicable without a loop. There are several kinds of loop, each with their own particular area of application. In this chapter, you'll learn:
Ivor Horton

Chapter 6. Pointers and References

Abstract
The concepts of pointers and references have similarities, which is why I have put them together in a single chapter. Pointers are important because they provide the foundation for allocating memory dynamically. Pointers can also make your programs more effective and efficient in other ways. Both references and pointers are fundamental to object oriented programming.
Ivor Horton

Chapter 7. Working with Strings

Abstract
This chapter is about handling textual data much more effectively and safely that the mechanism provided by a C-style string stored in an array of char elements. In this chapter, you’ll learn:
Ivor Horton

Chapter 8. Defining Functions

Abstract
Segmenting a program into manageable chunks of code is fundamental to programming in every language. A function is a basic building block in C++ programs. So far every example has had one function, main(), and that has typically used functions from the Standard Library. This chapter is all about defining your own functions with names that you choose.
Ivor Horton

Chapter 9. Lambda Expressions

Abstract
This chapter is dedicated to lambda expressions that provide a capability similar to that of a function. In this chapter you will learn:
Ivor Horton

Chapter 10. Program Files and Preprocessing Directives

Abstract
This chapter is more about managing code than writing code. I'll discuss how multiple program files and header files interact, and how you manage and control their contents. The material in this chapter has implications for how you define your data types, which you'll learn about starting in the next chapter.
Ivor Horton

Chapter 11. Defining Your Own Data Types

Abstract
IN THIS CHAPTER, I’ll introduce one of the most fundamental tools in the C++ programmer’s toolbox: classes. I’ll also present some ideas that are implicit in object-oriented programming and show how these are applied.
Ivor Horton

Chapter 12. Operator Overloading

Abstract
IN THIS CHAPTER, you’ll learn how to add support for operators such as add and subtract to your classes so that they can be applied to objects. This will make the types that you define behave more like fundamental data types and offer a more natural way to express some of the operations between objects. You’ve already seen how classes can have function members that operate on the data members of an object. Operator overloading enables you to write function members that enable the basic operators to be applied to class objects.
Ivor Horton

Chapter 13. Inheritance

Abstract
IN THIS CHAPTER, you’re going to look into a topic that lies at the heart of object-oriented programming: inheritance. Inheritance is the means by which you can create new classes by reusing and expanding on existing class definitions. Inheritance is also fundamental to making polymorphism possible, and polymorphism is a basic feature of object-oriented programming. I'll discuss polymorphism in the next chapter, so you what you'll learn there is an integral part of what inheritance is all about. There are subtleties in inheritance that I'll tease out using code that shows what is happening.
Ivor Horton

Chapter 14. Polymorphism

Abstract
POLYMORPHISM IS SUCH A POWERFUL feature of object-oriented programming that you’ll use it in the majority of your C++ programs. Polymorphism requires you to use derived classes, and the content of this chapter relies heavily on the concepts related to inheritance in derived classes that I introduced in the previous chapter.
Ivor Horton

Chapter 15. Runtime Errors and Exceptions

Abstract
EXCEPTIONS are used to signal errors or unexpected conditions in a program. Using exceptions to signal errors is not mandatory, and you’ll sometimes find it more convenient to handle them in other ways. However, it is important to understand how exceptions work, because they can arise out of the use of standard language features such as the new operator and the dynamic_cast operator and exceptions are used extensively within the standard library.
Ivor Horton

Chapter 16. Class Templates

Abstract
You learned about templates that the compiler uses to create functions back in Chapter 8; this chapter is about templates the compiler can use to create classes. Class templates are a powerful mechanism for generating new class types automatically. A significant portion of the Standard Library is built entirely on the ability to define templates, particularly the Standard Template Library, which includes many class and function templates.
Ivor Horton

Chapter 17. File Input and Output

Abstract
THE C++ LANGUAGE has no provision for input and output. The subject of this chapter is the input and output (I/O) capabilities that are available in the Standard Library, which provides support for device-independent input and output operations. You’ve used elements of these facilities to read from the keyboard and output to the screen in all the examples so far. In this chapter I’ll expand on that and explain how you can read and write disk files. By the end of this chapter, you’ll have learned:
Ivor Horton
Additional information