C++ Basics

C++ Basics - CS 115 Chapter 1 Basic Elements of C+ Basic...

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

View Full Document Right Arrow Icon
CS 115 Chapter 1 Basic Elements of C++
Background image of page 1

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 2 Basic Elements of C++ z Basics of a C++ Program z Data Types z Arithmetic Operators z Operator Precedence z Expressions z Type Conversions
Background image of page 2
Basic Elements of C++ 3 Basics of a C++ Program z Function main (Revisited) z Subprograms z Special Symbols z Word Symbols z Identifiers
Background image of page 3

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 4 Basics of a C++ Program z Function main z C++ Program must have a function main z Execution begins from main z Only one main function is allowed z Main is not equal to main
Background image of page 4
Basic Elements of C++ 5 Basics of a C++ Program z Function main #include <iostream> using namespace std; int main() { cout << “main is the door to the C++ program!” << endl; return 0; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 6 Basics of a C++ Program z Subprograms or Functions z A function is a collection of statements z A C++ program has one or more functions z Predefined functions are prewritten for specific tasks ± E.g. Math and String functions.
Background image of page 6
Basic Elements of C++ 7 Function E.g. int display() { cout << “Func display: Have a nice day!”; return 0; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 8 Predefined Functions E.g. int display() { cout << "Absolute value of -5 = " << abs(-5) << endl; return 0; }
Background image of page 8
Basic Elements of C++ 9 Put It All Together #include <iostream> #include <math> using namespace std; int display() { cout << "display: Absolute value of -5 = " << abs(-5) << endl; return 0; } int main() { cout << "main: Hello!" << endl; display(); cout << "main: So long!" << endl; return 0; }
Background image of page 9

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 10 Basics of C++ Program z Special Symbols +-*/ .;?, <= != == >=
Background image of page 10
Basic Elements of C++ 11 Basics of C++ Program z Word Symbols z E.g. int, float, double, char, const, void, return z Also, known as reserved words or keywords z They are in lowercase z They cannot be redefined
Background image of page 11

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 12 Basics of C++ Program z Identifiers z E.g. Names of variables, constants, functions, etc. z Identifier consists of: z Letters z Digits z Underscore (_) z Must begin with a letter or underscore z It is case sensitive: display and DiSpLaY are different
Background image of page 12
Basic Elements of C++ 13 Basics of C++ Program z Identifiers z Predefined Identifiers z E.g. cout and cin z They can be redefined by programmer z Caution: Programmer may get confused due to redefinition
Background image of page 13

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 14 Basics of C++ Program z Identifiers z Illegal use of identifiers: z 2sum – Can’t begin with a digit z Wow! – Can’t use exclamation z One+two – special symbol not allowed z Display 1 – space not allowed
Background image of page 14
Basic Elements of C++ 15 Basic Elements of C++ z Basics of a C++ Program z Data Types z Arithmetic Operators z Operator Precedence z Expressions z Type Conversions
Background image of page 15

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 16 Data Types C++ Data Types Simple Structured Pointers
Background image of page 16
Basic Elements of C++ 17 Data Types z Simple Data Types Simple Data Types Integral Floating-Point Enumeration
Background image of page 17

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

View Full DocumentRight Arrow Icon
Basic Elements of C++ 18
Background image of page 18
Image of page 19
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CS 115 taught by Professor Ryon during the Fall '06 term at NJIT.

Page1 / 60

C++ Basics - CS 115 Chapter 1 Basic Elements of C+ Basic...

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

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