Question? Leave a message!

A Brief Introduction to C++

A Brief Introduction to C++
Dr.SethPatton Profile Pic
Dr.SethPatton,United Kingdom,Teacher
Published Date:22-07-2017
Website URL
ECE 250 Algorithms and Data Structures A Brief Introduction to C++ Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.A Brief Introduction to C++ 2 A Brief Introduction to C++ We will provide a brief overview of C++ Many of the statements in C++ are very similar to C – It is assumed you remember these from ECE 150A Brief Introduction to C++ 3 A Brief Introduction to C++ In this topic we will see: – The similarities between C and C++ – Some differences, including: • Global variables and functions • The preprocessor, compilation, namespaces • Printing – Concluding with • Classes, templates • Pointers • Memory allocation and deallocation A Brief Introduction to C++ 4 Control Statements All control statements are similar if ( statement ) // ... else if ( statement ) // ... while ( statement ) else // ... // ... for ( int i = 0; i N; ++i ) // ... do // ... while ( statement );A Brief Introduction to C++ 5 Operators Operators have similar functionality for built-in datatypes: – Assignment = – Arithmetic + - / % += -= = /= %= – Autoincrement ++ – Autodecrement – Logical && – Relational == = = = – Comments / / // to end of line – Bitwise & &= = = – Bit shifting = =A Brief Introduction to C++ 6 Arrays Accessing arrays is similar: const int ARRAY_CAPACITY = 10; // prevents reassignment int arrayARRAY_CAPACITY; array0 = 1; for ( int i = 1; i ARRAY_CAPACITY; ++i ) arrayi = 2arrayi – 1 + 1; Recall that arrays go from 0 to ARRAY_CAPACITY – 1 Definition: The capacity of an array is the entries it can hold The size of an array is the number of useful entries A Brief Introduction to C++ 7 Functions Function calls are similar, however, the are not required to be part of a class: include iostream using namespace std; // A function with a global name int sqr( int n ) return nn; int main() cout "The square of 3 is " sqr(3) endl; return 0; A Brief Introduction to C++ 8 C++/C Differences We will look at categories of differences between C++ and C: – Including header files (the preprocessor) – The file is the base of compilation – Namespaces – PrintingA Brief Introduction to C++ 9 The C++ Preprocessor C++ is based on C, which was written in the early 1970s Any command starting with a in the first column is not a C/C++ statement, but rather a preprocessor statement – The preprocessor performed very basic text-based (or lexical) substitutions – The output is sent to the compilerA Brief Introduction to C++ 10 The C++ Preprocessor The sequence is: file (filename.cpp) → preprocessor → compiler (g++) Note, this is done automatically by the compiler: no additional steps are necessary At the top of any C++ program, you will see one or more directives starting with a , e.g., include iostreamA Brief Introduction to C++ 11 The C++ PreprocessorA Brief Introduction to C++ 12 Libraries You will write your code in a file such as Single_list.h where you will implement a data structure This file will be included in our tester file Single_list_tester.h with a statement such as: include "Single_list.h" The file Single_list_int_driver.cpp then includes the tester file: include "Single_list_tester.h"A Brief Introduction to C++ 13 Libraries You will note the difference: include iostream include "Single_list.h" The first looks for a file iostream.h which is shipped with the compiler (the standard library) The second looks in the current directoryA Brief Introduction to C++ 14 Libraries In this class, you will put all code in the header file This is not normal practice: – Usually the header (.h) file only contains declarations – The definitions (the actual implementations) are stored in a related file and compiled into an object fileA Brief Introduction to C++ 15 The C++ PreprocessorA Brief Introduction to C++ 16 The C++ Preprocessor With all these includes, it is always necessary to avoid the same file being included twice, otherwise you have duplicate definitions This is done with guard statements: ifndef SINGLE_LIST_H define SINGLE_LIST_H template typename Type class Single_list ///... ; endifA Brief Introduction to C++ 17 The C++ Preprocessor This class definition contains only the signatures (or prototypes) of the operations The actual member function definitions may be defined elsewhere, either in: – The same file, or – Another file which is compiled into an object file We will use the first methodA Brief Introduction to C++ 18 The File as the Unit of Compilation Another difference is the unit of compilation In C, the class was the basis of compiling executable code: class TestProgram public static void Main() System.Console.WriteLine( "Hello World" ); The existence of a function with the signature public static void Main(); determines whether or not a class can be compiled into an executableA Brief Introduction to C++ 19 The File as the Unit of Compilation In C/C++, the file is the base unit of compilation: – Any .cpp file may be compiled into object code – Only files containing an int main() function can be compiled into an executable The signature of main is: int main () // does some stuff return 0; The operating system is expecting a return value – Usually 0A Brief Introduction to C++ 20 The File as the Unit of Compilation This file (example.cpp) contains two functions includeiostream using namespace std; int sqr( int n ) // Function declaration and definition return nn; int main() cout "The square of 3 is " sqr(3) endl; return 0;