You're about to embark on a journey of self-improvement. You're going to learn how to program in C++. C++ is the programming language of choice for professional programmers around the world. C++ is a highly streamlined, powerful language, and learning it is no easy task. This course is designed for beginners, so you don't need any previous programming experience to succeed in this course. However, any programming background you may have will definitely be helpful. So put on your thinking caps and get ready. In this lesson, we'll start with the history of C++. Next, we'll go over the basics of the language. Then, you will write your first program! Later in this lesson, we'll cover a few more topics, then do some more programming.
In Lesson 1, you were introduced to the programming language C++. After today's lesson, the programs you will be able to write will be more complex, and therefore, more valuable to you as a programmer. In today's lesson, we will study the different variable types and modifiers, and examine constants, operators, and expressions. All of the material you learn today will be used in our future lessons and will serve as the building blocks for all the programs you will write in the future.
Now that you know how to create variables, you need to know how to print things to the screen and get input from a user and store it into a variable. There are a few ways to do this, and lots of ways to make special things happen. We'll get to all the special conditions in a later lesson, but right now, we'll cover snagging simple pieces of data from the keyboard and writing simple messages to the screen.
Scope rules are the rules that govern how an object may be accessed by different parts of a program. Thus far, we have touched on the three types of variables: local variables, formal parameters, and global variables. We will now focus on the scope rules that govern these three types of variables and how they relate to functions.
It is often necessary to create a set of several related variables. An array is a collection of variables of the same type that is called by a single name. Arrays can be one-dimensional or multi-dimensional. We will discuss one-dimensional arrays in this section.
Pointers contribute to making C++ the powerful programming language it is. I've talked about them in previous lessons, and pointers will be discussed in many of the subsequent lessons, as their use will be expanded over this course. For now, we will introduce the basic use of pointers and use this lesson as a foundation to build upon.
We will now take a look at a complex data type: the enumeration. Following will be a discussion of the typedef command, which allows the programmer to give the standard data types names of their choosing. Finally, we will discuss bitwide operators, the ? operator, and the , operator. These advanced operators contribute to making C++ the powerful yet flexible programming language it is.
A class defines a new data type. Or rather, redefines an old data type (the struct). These data types may then be used to create objects. An object is not only an instance of a class but may also be any type either predefined or user defined. When you create, or in C++ lingo, instantiate, a class, you create an object. Classes are declared using the keyword -- you guessed it -- class. The members of a class are always included in the class definition. Members include the data and the code that will make the class work.
We will begin this lesson by studying the issues related to passing objects to, and returning objects from, functions. But first, let's look at assigning objects.
This lesson will introduce two important C++ attributes: inheritance and virtual functions. In C++, you can define an object based on another. When a class is derived from another, it inherits the attributes of the class it is derived from -- its base class. Virtual functions are those that have the same parameter list as those in parent and/or descendant functions, allowing for consistent responses within class hierarchies.
Polymorphism, one of the three main attributes of an OOP language, denotes a process by which different implementations of a function can be accessed by the use of a single name. Way back in Lesson 1, we learned that polymorphism also means "one interface, multiple methods."
This document is intended to introduce pointers to beginning programmers in the C programming language. Over several years of reading and contributing to various conferences on C including those on the FidoNet and UseNet, I have noted a large number of newcomers to C appear to have a difficult time in grasping the fundamentals of pointers. I therefore undertook the task of trying to explain them in plain language with lots of examples.
The study of strings is useful to further tie in the relationship between pointers and arrays. It also makes it easy to illustrate how some of the standard C string functions can be implemented. Finally it illustrates how and when pointers can and should be passed to functions.
Well, we have progressed quite a way in a short time! Let's back up a little and look at what was done in Chapter 3 on copying of strings but in a different light.
Well, let's go back to strings for a bit. In the following all assignments are to be understood as being global, i.e. made outside of any function, including main.
Pointers, of course, can be "pointed at" any type of data object, including arrays. While that was evident when we discussed program 3.1, it is important to expand on how we do this when it comes to multi-dimensional arrays.
There are times when it is convenient to allocate memory at run time using malloc(), calloc(), or other allocation functions. Using this approach permits postponing the decision on the size of the memory block need to store an array, for example, until run time. Or it permits using a section of memory for the storage of an array of integers at one point in time, and then when that memory is no longer needed it can be freed up for other uses, such as the storage of an array of structures.
Up to this point we have been discussing pointers to data objects. C also permits the declaration of pointers to functions. Pointers to functions have a variety of uses and some of them will be discussed here.