CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

Introduction to C Programming
Chapter 2: Basic Elements of C++
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Introduction to C Programming
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
High-Level Programming Languages: C++
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Introduction to Programming
Control Structures (B) Topics to cover here: Sequencing in C++ language.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
24 4/11/98 CSE 143 Stream I/O [Appendix C]. 25 4/11/98 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 15 - C++ As A "Better C"
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Introduction to C++ Systems Programming.
Basic Elements of C++.
CSE 143 Introduction to C++ [Appendix B] 4/11/98.
Chapter 2 part #3 C++ Input / Output
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 part #3 C++ Input / Output
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]

CSC 143A 2 Introduction to C++  C++ is a superset of C.  (Almost) any legal program in C is also a legal C++ program.  The core of C++ (basic types, variables, operations, and statements) works the same as in C.

CSC 143A 3 Introduction to C++ (cont.)  Major changes in C++:  A “Better C”  Support for Data Abstraction (user- defined types)  Support for Object-Oriented Programming  We'll introduce the later two gradually

CSC 143A 4 A Simple C++ Program // A first C++ Program // Print a greeting message #include int main( ) { cout << "Welcome to CSC143!" << endl; return 0; }  //- comments extend from // to end of line  Operator << writes the value of the right argument to the output stream on the left, here cout - the screen.  endl ends a line of output and ensures that it is displayed Right Now!.

CSC 143A 5 A Second C++ Program // Read two integers and print their sum. #include int main( ) { int i, j; cout << "Please enter a number: "; cin >> i; cout << "Please enter another number: "; cin >> j; cout << "The sum of " << i << " and " << j << " is " << i + j << endl; return 0;}

CSC 143A 6 Second C++ Program (cont)  Operator >> reads a value from the stream that is its left argument (here cin, the keyboard) and stores it in the variable given as its right argument.  The >> and << operators can be strung together to read or write several items in a single statement.

CSC 143A 7 A “Better C”  cin and cout for stream input and output (plus cerr )  Reference parameters  New comment style  Relaxed placement of declarations  Symbolic constants  A real logical (Boolean) type - bool  Enumerated types

CSC 143A 8 Two Styles of Comments  Old C-style comments /* This is a comment */  Double-slash comments (comment extends from the // to the end of the line) int id;// student ID number  Which form is better?

CSC 143A 9 Declarations Go Anywhere  C++ declarations can appear anywhere a normal statement can: void something (int x) { if (x == 10) x = x / 2; int y; // Declaration can occur here... }  Common usage: for -loop index variables for (int k = 0; k < 100; k++) { // k is only defined inside this loop }

CSC 143A 10 Struct declaration  Recall in C typedef struct { /* definition of struct */ } myStruct;  In C++, it is simpler struct myStruct { // definition of struct };

CSC 143A 11 Symbolic Constants  Explicit support for constants const double PI = ;  Do not use #define … #define PI  Why not? Because #define is strictly textual substitution.  Explicit constants allow compile-time type checking and scope analysis using same rules obeyed by (non-const) variables (e.g. you can look up the value of PI while running the debugger)

CSC 143A 12 New bool type  C++ bool has two legal values: true and false  bool, true and false are reserved words  Direct implementation of the "Boolean" concept bool isBigNumber (double d) { if (d > 30e6) return true; else return false; }  Not supported in early C++ compilers (one reason you want to have a recent version)

CSC 143A 13 int vs. bool  Under the hood, a bool value is represented as an int ; bool and int values are usually interchangeable (for backward compatibility).  Use bool where Boolean values are natural int i; bool b; b = (mass >= 10.8); //value is true or false if (b)...//OK while (b && !(i < 15))...//OK  Avoid: i = b; //marginally OK: value is 0 or 1 i = true;//OK, but bad style b = i;//ill-advised (warning)  cout <<  displays 0 or 1 for bool values

CSC 143A 14 Enumerated Types  User-defined type whose constants are meaningful identifiers, not just numbers enum Color { RED, GREEN, BLUE };  Declare like other types; use like other integer types Color skyColor;... switch (skyColor) { case RED:... case GREEN:... case BLUE:... }

CSC 143A 15 Input/Output Concepts  Concepts should be review!  New syntax, but same fundamental concepts  input vs. output, read vs. write  conversion between characters in a file and C/C++ data values (types) in a program  Other concepts that we will return to later:  file; file name vs. file variable  open; close  end-of-file

CSC 143A 16 Stream I/O  The basic C++ I/O library is built around the concept of streams.  both for keyboard/monitor and for files  Old C-style printf, scanf, etc. library is still available, but….  Mixing the two is bad news  You must use only stream I/O in CSC143

CSC 143A 17 What is a Stream? A stream is just a sequence of characters: Welcome to anOutputStream Output cursor Smythe, J anInputStream Input cursor 64 C++ Program input via >> operator output via << operator

CSC 143A 18 Well-Known Streams  Global streams defined in iostream.h :  cin : standard input stream (usually keyboard)  cout : standard output stream (usually screen)  cerr : standard error stream (also usually directed to the screen)  Programs can open other streams to/from files and other devices. We will cover this later, after gaining experience with simple console I/O.

CSC 143A 19 Stream Output Operation For output streams, << is the "put to" or "insertion" operator #include … int count = 23; cout << "Hello, World!" << ‘\n’; // endl: same as '\n', but flushes output cout << "The count is " << count << endl;

CSC 143A 20 Stream Input Operation For input streams, >> is the “get from” or "extraction" operator #include … int x, ID; char Name[40]; cin >> x; cin >> Name >> ID; // Can read multiple items on one line // Note: no &’s as with scanf

CSC 143A 21 How Stream Input Works Input stream characters are interpreted differently, depending on the data type: int ID; char Name[40]; char ch; cin >> ID; //interprets as integer cin >> ch; //reads a char, skipping whitespace cin >> Name; //interprets as character string, //skipping leading whitespace and //stopping at trailing whitespace

CSC 143A 22 Stream Advantages  Type Safety  No more dangers of mismatched % -types scanf("%s", &someInt); // oops! scanf("%d", someInt); // boom!  Readability  Easier to interpret than a sequence of % -types scanf("%d%c%d", &intOne, &oneChar, &intTwo); versus cin >> intOne >> oneChar >> intTwo;  Generality  Can extend >> and << to work with user- defined data types.

CSC 143A 23 Parameters  What does this print? #include … // Double the value of k void dbl(int k) { k = 2 * k; } int main( ) { int n = 21; dbl(n); cout << n << endl; return 0; }  Output:

CSC 143A 24 Reference Parameters  Use & in parameter declaration to make the parameter an alias for the argument. // Double the value of k void dbl(int & k) { k = 2 * k; } int main( ) { int n = 21; dbl(n); cout << n << endl; return 0; }  Output:

CSC 143A 25 C++ Parameters  Value parameters (as in C):  Passes a copy of the actual argument (except arrays)  Assignment to parameter in function doesn’t change actual arguments (except arrays)  Reference parameters:  The parameter is an alias for actual argument  Achieves same effect as pointer parameters without needing explicit use of & and *.  Assignments to parameter changes argument.  We will use pointers in data structures later. Avoid them for now.