Lesson 1
Building Classes in C++
C++ Course introduction
Welcome to
Building Classes in C++, the second course in the
C++ for C Programmers series.
This course teaches you the basics of working with classes and objects in C++.
It introduces you to one important aspect of object-oriented programming (OOP) which is encapsulation and prepares you to continue exploring OOP techniques and concepts.
After completing this course, you will be able to write C++ programs that:
- Use the
class
construct to define Abstract Data Types (ADTs) and implement encapsulation of data and behavior
- Write member functions that act on member data
- Use constructor and destructor member functions to manage class-defined objects
- Implement useful dynamic data structures such as a dynamically allocated stack, a bounds-checking array, and a singly linked list
- Use an efficient object disposal scheme
Visual C++ provides two ways of building a C/C++ program. The easiest (and most common) way is to build within the Visual C++ development environment.
The other way is to build from a command prompt using command-line tools. In either case, you can create your source files using the Visual C++ source editor or a third-party editor of your choice.
If your program uses a makefile rather than a .vcxproj file, you can still build it in the development environment as an external project.
Understand the essentials of object-oriented programming in C++
This module reviews object-oriented programming in C++. It assumes the reader has prior experience programming in C++ or another language and is, therefore, familiar with control statements for selection and repetition, basic data types, arrays, and functions.
If your first course was in C++, you can skim this chapter for review or just use it as a reference as needed. However, you should read it more carefully if your C++ course did not emphasize object-oriented design.
If your first course followed an object-oriented approach but was in another language, you should concentrate on the differences between C++ syntax and that of the language that you know. If you have programmed only in a language that was not
object-oriented, you will need to concentrate on aspects of object-oriented programming and classes as well as C++ syntax.
We begin the module with an introduction to the C++ environment and the runtime system. Control structures and statements are then discussed, followed by a discussion of functions.
Next we cover the basic data types of C++, called primitive data types. Then we introduce classes and objects. Because C++ uses pointers to reference objects, we discuss how to declare and use pointer variables.
The C++ standard library provides a rich collection of classes that simplify programming in C++. The first C++ class that we cover is the string class.
The string class provides several functions and an operator + (concatenation) that process sequences of characters (strings). We also review arrays in C++. We cover both one- and two-dimensional arrays and C-strings, which are arrays of characters.
Finally we discuss input/output. We also show how to use streams and the console for input/output and how to write functions that let us use the stream input/output operations on objects.