C++Intro - A Brief Introduction to C++ ECE250 Algorithms...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: A Brief Introduction to C++ ECE250 Algorithms and Data Structures (Slides are prepared based on notes from Prof. Harder) Introduction to C++ Brief Introduction to C++ 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 150 2 Introduction to C++ Brief Introduction to C++ A Brief Introduction to C • In this talk we will see: – The similarities between C# and C++ – Some differences, including: lobal variables and functions • Global variables and functions • The preprocessor, compilation, namespaces • Printing – Concluding with • Classes, templates • Pointers • Memory allocation and deallocation 3 Introduction to C++ ontrol Statements Control Statements • All control statements are similar if ( expression ) { statement } else if ( expression ) { statement while ( expression ) { } else { statement statement } } for ( expression ; expression; expression ) { statement do { } statement while ( xpression ; 4 } while ( expression ); Introduction to C++ perators Operators • O perators have similar functionality for built-in datatypes : – assignment = – arithmetic + ‐ * / % * / % += ‐ = *= /= %= – autoincrement ++ – autodecrement ‐‐ – logical && || ! – relational == != < <= >= > omments * */ co e ts / / // to end of line 5 Introduction to C++ perators Operators • As are these: – bitwise & | ^ ~ &= |= ^= it shifting < >> – bit shifting << >> <<= >>= 6 Introduction to C++ rrays Arrays • Accessing arrays is similar: const int ARRAY_SIZE = 10; // prevents reassignment int array[ARRAY_SIZE]; array[ ] = 1; for ( int i = 1; i < ARRAY_SIZE ; ++i ) { array[i] = 2*array[i – 1] + 1; } Recall that arrays go from to ARRAY_SIZE – 1 7 Introduction to C++ unctions Functions • Function calls are similar, however, the are not required to be part of a class: #include <iostream> sing namespace std; using namespace std; // A function with a global name int sqr ( int n ) { return n*n; } int main () { // the entry point is always the function main () { // y p y f cout << "The square of 3 is " << sqr (3) << endl; return 0; } 8 Introduction to C++ ++/C# Differences C / C # Differences • This slide introduces four additional differences between C++ and C#: – Including header files (the preprocessor) he file is the base of compilation – The file is the base of compilation – Namespaces – Printing 9 Introduction to C++ he C++ Preprocessor The C Preprocessor • 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 compiler 10 Introduction to C++ he C++ Preprocessor 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...
View Full Document

This note was uploaded on 01/26/2010 for the course ECE ECE250 taught by Professor Ajitsingh during the Winter '10 term at Waterloo.

Page1 / 84

C++Intro - A Brief Introduction to C++ ECE250 Algorithms...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online