C++ Story

S R T B H P N

C++ Story:

Cover Page
This C++ Story pulls together site pages, presentations on C++ syntax and semantics, and example code to present a brief tutorial on the C++ programming language. Also provided are exercises to test your skills. The story consists of 5 chapters on: basics of C++ syntax and semantics, classes, class relationships and their use to build compound objects, templates and template specialization. The concluding chapter surveys repositories available on this site and provides additional links and references. Below, find instructions on how to navigate through the story, using buttons on the right and/or key presses.
Chapter 1
Chapter 1 focuses on C++ language syntax and semantics before classes. It discusses all the things C++ added to C, describes its type system, points to basic code examples to review, and ends with a discussion of C++ models for compilation, program execution, and use of memory. Finally, there are a set of exercises for you to test your skills. The exercises start out with simple requests with discussion about how you satisfy them. Later exercises require some design on your part.
Language Synopsis
This page provides brief descriptions of the major C++ language features and a few of its many libraries. It also describes a few libraries that I built for CSE687 - Object Oriented Design class work. In the second half, it provides materials on threads, sockets, and how you can build Graphical User Interfaces for C++ code.
C vs C++
This page describes what C++ added to the C language, how they are similar, and where they are different. It winds up discussing the core intent of the C++ language.
C++ Type System
C++ is a relatively strongly typed language, favoring error detection at compile time over run-time. The fundamental (built-in) types are simple, similar to those of other languages like C# and Java. That's what this page presents, along with a few comments about weakly typed languages. We cover later some of the standard library types that are provided by the language environment but not by the language itself.
C++ Basic Demos
This page documents a repository for a number of basic code demonstrations. You will find demonstrations for syntax and semantics of the language excluding classes, in "Basic Demos for C++ syntax". The third group of demos, "Basic Demos for C++ libraries" will help you get started using streams and the STL containers.
C++ Models
This page discusses models for compilation, program execution, and memory allocation. The compilation model will help you understand how C++ compilers and linkers collaborate to build execution images. The structure of C++ packages is driven by the way this build process works. The execution model shows how your programs use stack memory and how it interacts with the windows console. Life-times for the instances your program creates are determined by where they are placed in memory. The Memory model will help you understand how to control those life-times.
Chapter 1 Exercises
This page presents exercises of syntax and semantics for C++ without classes. You will find the first few are simple programming exercises. They get more advanced as you proceed through the set, later exercises requiring some design on your part.
Chapter 2 - Classes
Chapter 2 focuses on C++ classes, especially value types, and provides a number of example and demo projects. When you've finished this chapter you will have a good understanding of the anatomy of C++ classes.
C++ Classes
This page covers anatomy of a typical class. It presents a class with "typical" typical methods. It also holds a pointer to a string (aggregation) to illustrate how construction and assignment work when members are aggregated. Note that this is an unusual arrangement. Normally classes compose string instances. We chose to aggregate this string so you could see how aggregated members are handled in class methods. Compiler generated methods are discussed along with advice about when to use them.
Value Types
STR is a string class designed to provide value semantics. Each method and function of this class is covered, e.g., purpose, declaration, definition, and invocation. Here we disclose all the details about how to build classes to support value-type behavior, e.g., supporting copy and assignment of instances. Note that we do not recommend using STR for strings in your application; instead, you should prefer the std::string class. This is just a very nice vehicle to explain implementation techniques for value types.
Duplicates code
Duplicates locates duplicate files in a specified directory tree. Of interest here is the design of its DataStore class. That uses aliases to assemble containers from the STL into an efficient store for duplicates. It stores each file name and each path only once. You are invited to download the code from its repository and open in Visual Studio. There is quite a lot to explore here. None of the code is very complicated, but quite a few design techniques are used for its implementation.
C++ Basic Demos
Documentation for a repository holding a number of basic code demonstrations is shown here. The middle group "Basic Demos for C++ classes" has a number of demonstrations of class syntax and semantics. The last three demos, from that group, focus on when methods get called, and using constructor initialization sequences to efficiently call appropriate constructors. DemInherit shows a surprising error that can occur if you don't use an initialization sequence for copy constructors. Project demInher2 illustrates the problem, and demInher2b fixes it by using an appropriate initialization sequence.
Chapter 2 Exercises
This page provides a set of exercises for C++ classes, starting with simple, guided, problems, to some requiring design. Exercises 4 and 5 focus on understanding the compiler generated methods: copy, assignment, and destruction, and when to use them and when to supply your own.
Chapter 3 - Relationships
Object Oriented Design consists of using classes and class relationships to provide a logical structure for your program implementations. This chapter focuses on class relationships and on compound objects created by combining classes with one or more of four fundamental relationships: inheritance, composition, aggregation, and using. It is remarkable that armed with only these four relationships we can build code that represents practically anything from an application domain. Cheers for OOD!
Class Relationships
This page dicusses the four Object-Oriented class relationships, e.g., inheritance, composition, aggregation and using. One important consequence of using these relationships is how that affects memory layout of your class instances. Both Inheritance and composition imply memory containment. A derived object contains an instance of its base within its memory foot-print. The same is true for composition; the composer contains in its memory foot-print an instance of the composed. Aggregation and using relationships have no containment consequences.
Inheritance Relationships
In this page we discusses the inheritance relationship, responsible for providing polymorphic substitution and code reuse. The Soldier example used on this page is a nice metaphor for many uses of inheritance. Clearly each derived instance has an "is-a" relationship with its base instance. A Soldier is a person. If you look carefully at the code example, you will see the power of inheritance is often its substitution property: Functions that use pointers or references statically typed to some base class must be able to use objects of classes derived from the base through those pointers or references without any knowledge specialized to the derived classes. This was used in the Soldier::show(Soldier& s) function. We pass any derived instance, Private, Sergeant, or Officer, and the function correctly calles the methods appropriate for that instance. Note that when you see this principle stated in the literature it is often written in a way that applies only to reference types, as in Java and C#.
Compound Objects
Compound objects are combinations of classes bound together using the four class relationships. Of particular importance is the way compound classes are laid out in memory and how they are constructed. Look at the Compound Objects CodeSnap to see how compound objects are constructed (find link at the bottom of the page). You should look carefully at both the CodeSnap and at the TextFinder code in this repository.
TextFinder Code
TextFinder is a nice example of a compound object. Its structure is relatively simple, but its functionality is partitioned into several classes, each of which implements a single responsibility.
Chapter3 - Exercises
Here are exercises for class relationships and compound objects. They range in difficulty from moderate to fairly complex designs and implementations. Of course, the more carefully you design, the simpler an implementation is likely to be.
Chapter4 - Templates
Chapter 4 covers class templates, including specialization, and function templates with overloads. Use of class inheritance and templates provides the most effective path for building flexible software. The ability to be used in many different situations and for different purposes is an extraordinarily useful property for software components. The DirExplorer components, in the FileManager repository, are excellent examples of flexible software components. They use inheritance and templates to gain that flexibility.
Class Templates
Templates are designed to support compile-time substitution. For example, a std::vector<T> can hold a broad range of user-defined types; which one actually held is determined by the type specified for T at compile-time. Look at the TextFinder Repository documentation to see how DirExplorerT<App> is made independent of application specific processing needs by delegating to a composed instance of its App parameter. This page introduces templates. A lot of the details you will need, including specialization, are covered in the Templates presentation, linked below.
STL-Containers
This code provides simple demonstrations for each of the STL containers and adapters. Each demonstration just shows you how to construct, fill, and use a container. Comments at the beginning of each demo describe how the container works.
Templates Techniques
This is the documentation page for a repository with a number of illustrative template code demos. The first three demo folders focus on relatively simple template demonstrations. The last three folders have code that uses template meta-programming. That means that the code uses constructs that configure template processing at compile-time. Variadic templates, for example, use compile-time recursions to support defining template classes and functions that accept an arbitrary finite number of template parameters.
Template Specialization
Template specialization is a very important characteristic of C++ templates. It allows a designer to cover cases that don't work well with the generic definition. This page will eventually contain a lot of material. Until then, you can find examples in the TemplateTechniques demos, and see a good example of why they are important in the design of C++ Properties discussed in the CppProperties.html repository documentation page.
C++ Properties
This repository code provides an implemtation of C++ properties. A number of interesting techniques were used to provide a useable capability. The design description given on this page is a good entry point to learn template design techniques and how template meta programming can provide very powerful features.
Chapter4 - Exercises
Here are a set of exercises for templates. The first three ask you to build useful template classes and require some design on your part. The last uses a custom type traits class, developed for the CppProperties example. This is a good way to ease into a bit of template meta-programming.
Chapter5 - Summary
Chapter 5 concludes this story with a summary of the repositories in this site, followed by a page that provides a lot of resources - books, videos, and links - to help you continue with C++.
Summary of CppRepositiories
This page provides an annotated list of Repositories that has links to documentation pages for each repository in this site. The top menu has a set of dropdown menus that show all the repositories in each of six categories. A quick way to see what code is available is to hover over each of the six categories to see what they contain.
Resources to Continue
An extensive list of articles, code, links, and videos is provided here to help you continue developing C++ skills.
Appendix
References for both beginners and advanced C++ developers.
C++ Survival Guide
This pdf contains a brief survey of basic C++ and library Syntax.
C++11
Survey of additions to C++ in the C++11 standard. This page includes some language syntax that predate the C++11 standard, to provide context for the additions.
C++ Examples
All code examples and project statements referenced anywhere in the site.
C++ References
References for books, videos, websites, and code examples.