C++ Basics PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an introduction to the C++ programming language. It details the basic concepts and features of C++, including object-oriented programming (OOP).
Full Transcript
1 C++ Basics Introduction to C++ 31 The C language is peculiar because it is a high-level language with many of the features of a low-level language. C is somewhere in between the two extremes of a very high-level language and a low-level language, and therein lies both its strengths and its weakne...
1 C++ Basics Introduction to C++ 31 The C language is peculiar because it is a high-level language with many of the features of a low-level language. C is somewhere in between the two extremes of a very high-level language and a low-level language, and therein lies both its strengths and its weaknesses. Like (low-level) assembly language, C language programs can directly manipulate the computer’s memory. On the other hand, C has the features of a high-level language, which makes it easier to read and write than assembly language. This makes C an excellent choice for writing systems programs, but for other programs (and in some sense even for systems programs) C is not as easy to understand as other languages; also, it does not have as many automatic checks as some other high-level languages. To overcome these and other shortcomings of C, Bjarne Stroustrup of AT&T Bell Laboratories developed C++ in the early 1980s. Stroustrup designed C++ to be a better C. Most of C is a subset of C++ and so most C programs are also C++ programs. (The reverse is not true; many C++ programs are definitely not C programs.) Unlike C, C++ has facilities for classes and so can be used for object-oriented programming. C++14 is the most recent version of the standard of the C++ programming language. It was approved on August 19, 2014, by the International Organization for Standardization. At the time of this writing, the C++14 standard has not yet been published. C++14 consists primarily of small improvements over C++11, while C++11 included significant improvements over the prior version. Newer compilers that can handle C++11 or C++14 are able to compile and run programs written for older versions of C++. However, the C++11 and C++14 standards include new language features that are not compatible with older C++ compilers. This means that if you have an older C++ compiler, then you may not be able to compile and run C++11 or C++14 programs. C++ and Object-Oriented Programming Object-oriented programming (OOP) is a currently popular and powerful programming technique. The main characteristics of OOP are encapsulation, inheritance, and polymorphism. Encapsulation is a form of information hiding or abstraction. Inheritance has to do with writing reusable code. Polymorphism refers to a way that a single name can have multiple meanings in the context of inheritance. Having made those statements, we must admit that they will hold little meaning for readers who have not heard of OOP before. However, we will describe all these terms in detail later in this book. C++ accommodates OOP by providing classes, a kind of data type combining both data and algorithms. C++ is not what some authorities would call a “pure OOP language.” C++ tempers its OOP features with concerns for efficiency and what some might call “practicality.” This combination has made C++ currently the most widely used OOP language, although not all of its usage strictly follows the OOP philosophy. The Character of C++ C++ has classes that allow it to be used as an object-oriented language. It allows for overloading of functions and operators. (All these terms will be explained eventually, so do not be concerned if you do not fully understand some terms.) C++’s connection to the C language gives it a more traditional look than newer object-oriented languages, yet it has 32 CHAPTER 1 C++ Basics more powerful abstraction mechanisms than many other currently popular languages. C++ has a template facility that allows for full and direct implementation of algorithm abstraction. C++ templates allow you to code using parameters for types. The newest C++ standard, and most C++ compilers, allow multiple namespaces to accommodate more reuse of class and function names. The exception handling facilities in C++ are similar to what you would find in other programming languages. Memory management in C++ is similar to that in C. The programmer must allocate his or her own memory and handle his or her own garbage collection. Most compilers will allow you to do C-style memory management in C++ since C is essentially a subset of C++. However, C++ also has its own syntax for a C++ style of memory management, and you are advised to use the C++ style of memory management when coding in C++. This book uses only the C++ style of memory management. C++ Terminology All procedure-like entities are called functions in C++. Things that are called procedures, methods, functions, or subprograms in other languages are all called functions in C++. As we will see in the next subsection, a C++ program is basically just a function called main; when you run a program, the run-time system automatically invokes the function named main. Other C++ terminology is pretty much the same as most other programming languages, and in any case, will be explained when each concept is introduced. A Sample C++ Program Display 1.1 contains a simple C++ program and two possible screen displays that might be generated when a user runs the program. A C++ program is really a function definition for a function named main. When the program is run, the function named main is invoked. The body of the function main is enclosed in braces, {}. When the program is run, the statements in the braces are executed. The following two lines set up things so that the libraries with console input and output facilities are available to the program. The details concerning these two lines and related topics are covered in Section 1.3 and in Chapters 9, 11, and 12. #include using namespace std; The following line says that main is a function with no parameters that returns an int (integer) value: int main( ) Some compilers will allow you to omit the int or replace it with void, which indicates a function that does not return a value. However, the previous form is the most universally accepted way to start the main function of a C++ program. The program ends when the following statement is executed: return 0; This statement ends the invocation of the function main and returns 0 as the function’s value. According to the ANSI/ISO C++ standard, this statement is not required, but many compilers still require it. Chapter 3 covers all these details about C++ functions. functions program Compiling and Running a C++ Program VideoNote int main() return 0; Introduction to C++ 33 User types in 0 on the keyboard. User input is shown in bold. Display 1.1 A Sample C++ Program 1 #include 2 using namespace std; 3 int main( ) 4{ 5 int numberOfLanguages; 6 cout