Specialist Programming C++ CS2001N Semester 1 2008/9.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.1 Introduction 1.2 What is a Computer?
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
Introduction Kingdom of Saudi Arabia Shaqra University
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.1Introduction 1.2What is a Computer?
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Three types of computer languages
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved Computer Organization Six logical units of computer 1.Input unit “Receiving” section Obtains information.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 1 – Introduction to Computers and C++ Programming Outline 1.1Introduction 1.2What is a Computer?
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 1 February 8, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Announcements Starting next week class 6-8 on Thursday Homework 1 on the web  Due January 29 – next class meeting  Homework policy No late assignments.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 EECS230 Course Introduction and a First Program Ying Wu Electrical Engineering and Computer Science Northwestern University
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Chapter 02 (Part II) Introduction to C++ Programming.
1 Introduction to C++ Programming Outline Introduction Machine Languages, Assembly Languages, and High-Level Languages History of C++ C++ Standard Library.
 2003 Prentice Hall, Inc. All rights reserved Introduction Software –Instructions to command computer to perform actions and make decisions Hardware.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
1.2 What is a Computer? Computer Computer programs Hardware Software
Chapter 2 - Introduction to C Programming
Chapter 1 – Introduction to Computers and C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 1 – Introduction to Computers and C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Presentation transcript:

Specialist Programming C++ CS2001N Semester /9

CS2001N – Specialist Programming C++ Module Lecturer: Dr. Yong Xue Office: Room T10-02, Tower Building Surgery hours: Telephone: Direct Line – (020) Ext

Outline of Study Programme Two principal methods of formal teaching: Lectures: Tutorials: At the end of each week the lecture OHPs will be published on WebLearn, and can also be viewed via:

CS2001N – Specialist Programming C++ Lecture & Practical Sessions Semester 1, 2008 / 2009 Lecture Sessions Tuesday: 14:00-16:00 TM1-45 Practical Sessions Tuesday: 16:00-18:00 T3-05 (Eden Grove)

CS2001N – Specialist Programming C++ Level: Intermediate Credit Rating: 15 Prerequisites: A PASS in a programming module – CS1014N ( CS1002N) or its equivalent

CS2001N – Specialist Programming C++ RECOMMENDED TEXTBOOK AND READING LIST Essential textbook: C++ How to Program, 5/E –Harvey M. Deitel, Deitel & Associates, Inc. Paul J. Deitel, Deitel & Associates, Inc. –Print ISBN: , Web ISBN (SafariX): –Publisher: Prentice Hall, Copyright: 2005 Secondary textbook: Absolute C++, 3/e –Walter Savitch –ISBN-10: , ISBN-13: –Publisher: Addison Wesley Higher Education, Copyright: 2008

CS2001N – Specialist Programming C++ ASSESSMENT: Coursework 70%; Examination 30% The final unit mark will be calculated as an aggregation of all the assessment elements.

CS2001N – Schedule Week1: Introduction to Computers and C++ Programming (Chapter 1) Week2: Control structures (Chapter 2) Week3: Functions (Chapter 3 ) Week4: Arrays (Chapter 4) Week5: Pointer and Strings (Chapter 5) Week6: Classes and Data Abstraction (Chapter 6) Week7: Classes: Part II (Chapter 7 and 8) Week8: Object-Oriented Programming: Inheritance and Polymorphism (Chapter 9 and 10) Week9: Templates and C++ Stream Input/Output(Chapter 11 and 12) Week10: File Processing (Chapter 14) Week11: Revision

Lecture Note 1 - Introduction C++ Programming Introduction History of C and C++ C++ Standard Library Structured Programming The Key Software Trend: Object Technology Basics of a Typical C++ Environment General Notes About C++ and This Book Introduction to C++ Programming A Simple Program: Printing a Line of Text Another Simple Program: Adding Two Integers Arithmetic Decision Making: Equality and Relational Operators

Introduction In this course you will learn – C and C++ – Structured programming and object oriented programming

History of C and C++  C++ evolved from C – C evolved from two other programming languages, BCPL and B  C++ “spruces up” C – Provides capabilities for object-oriented programming  Objects are reusable software components that model things in the real world  Object-oriented programs are easy to understand, correct and modify

Why C++ Widely used (UNIX, Industry, Playstation) Access to low level registers Object orientated Preparation for graphics modules!

C++ Standard Library  C++ programs – Built from pieces called classes and functions  C++ standard library – Provides rich collections of existing classes and functions for all programmers to use

Structured Programming  Structured programming – Disciplined approach to writing programs – Clear, easy to test and debug, and easy to modify  Multitasking – Many activities to run in parallel

The Key Software Trend: Object Technology  Objects – Reusable software components that model real world items – Meaningful software units  Date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects, etc.  Any noun can be represented as an object – More understandable, better organized and easier to maintain than procedural programming – Favor modularity

Basics of a Typical C++ Environment Phases of C++ Programs: 1.Edit 2.Preprocess 3.Compile 4.Link 5.Load 6.Execute Loader Primary Memory Program is created in the editor and stored on disk. Preprocessor program processes the code. Loader puts program in memory. CPU takes each instruction and executes it, possibly storing new data values as the program executes. Compiler Compiler creates object code (machine language code) and stores it on disk. Linker links the object code with the libraries, creates a.out and stores it on disk Editor Preprocessor Linker CPU Primary Memory Disk The C++ preprocessor obeys commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually include other text files to be compiled and perform various text replacements.

General Notes About C++ and This Book  Book is geared toward novice programmers  Programming clarity is stressed  C and C++ are portable languages – Programs written in C and C++ can run on many different computers

Introduction to C++ Programming  C++ language – Facilitates a structured and disciplined approach to computer program design  Following are several examples – The examples illustrate many important features of C++ – Each example is analyzed one statement at a time.

fig01_02.cpp (1 of 1) fig01_02.cpp output (1 of 1) 1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++. 3 #include 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome to C++!\n"; 9 10 return 0; // indicate that program ended successfully } // end function main Welcome to C++! Single-line comments.Preprocessor directive to include input/output stream header file. Function main appears exactly once in every C++ program.. Function main returns an integer value. Statements end with a semicolon ;. Name cout belongs to namespace std. Stream insertion operator. Keyword return is one of several means to exit function; value 0 indicates program terminated successfully. A Simple Program: Printing a Line of Text

 std::cout – Standard output stream object – “Connected” to the screen – std:: specifies the "namespace" which cout belongs to std:: can be removed through the use of using statements  << – Stream insertion operator – Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) – std::cout << “Welcome to C++!\n”;  \ – Escape character – Indicates that a “special” character is to be output

A Simple Program: Printing a Line of Text There are multiple ways to print text – Following are more examples

1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements. 3 #include 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome "; 9 std::cout << "to C++!\n"; return 0; // indicate that program ended successfully } // end function main Welcome to C++! Multiple stream insertion statements produce one line of output. Unless new line ‘\n’ is specified, the text continues on the same line. fig01_04.cpp (1 of 1) fig01_04.cpp output (1 of 1)

1 // Fig. 1.4: fig01_04.cpp 2 // Printing a line with multiple statements. 3 #include 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome\nto\n\nC++!\n"; 9 10 return 0; // indicate that program ended successfully } // end function main Welcome to C++! Multiple lines can be printed with one statement. fig01_05.cpp (1 of 1) fig01_05.cpp output (1 of 1)

Another Simple Program: Adding Two Integers  Variables – Location in memory where a value can be stored for use by a program – Must be declared with a name and a data type before they can be used – Some common data types are:  int - integer numbers  char - characters  double - floating point numbers – Example: int myvariable;  Declares a variable named myvariable of type int – Example: int variable1, variable2;  Declares two variables, each of type int

Another Simple Program: Adding Two Integers  >> (stream extraction operator) – When used with std::cin, waits for the user to input a value and stores the value in the variable to the right of the operator – The user types a value, then presses the Enter (Return) key to send the data to the computer – Example: int myVariable; std::cin >> myVariable; Waits for user input, then stores input in myVariable  = (assignment operator) – Assigns value to a variable – Binary operator (has two operands) – Example: sum = variable1 + variable2;

1 // Fig. 1.6: fig01_06.cpp 2 // Addition program. 3 #include 4 5 // function main begins program execution 6 int main() 7 { 8 int integer1; // first number to be input by user 9 int integer2; // second number to be input by user 10 int sum; // variable in which sum will be stored std::cout << "Enter first integer\n"; // prompt 13 std::cin >> integer1; // read an integer std::cout << "Enter second integer\n"; // prompt 16 std::cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully } // end function main Enter first integer 45 Enter second integer 72 Sum is 117 fig01_06.cpp (1 of 1) fig01_06.cpp output (1 of 1) Declare integer variables.Use stream extraction operator with standard input stream to obtain user input. Concatenating, chaining or cascading stream insertion operations. Stream manipulator std::endl outputs a newline, then “flushes output buffer.”

Arithmetic  Arithmetic calculations – Use * for multiplication and / for division – Integer division truncates remainder  7 / 5 evaluates to 1 – Modulus operator returns the remainder  7 % 5 evaluates to 2  Operator precedence – Some arithmetic operators act before others (i.e., multiplication before addition)  Be sure to use parenthesis when needed – Example: Find the average of three variables a, b and c  Do not use: a + b + c / 3  Use: (a + b + c ) / 3

Arithmetic Arithmetic operators: + Addition - Subtraction * Multiplication / Division Integer division truncates remainder 7 / 5 evaluates to 1 % Modulus operator returns remainder 7 % 5 evaluates to 2

Arithmetic Rules of operator precedence – Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first – Multiplication, division, modulus applied next Operators applied from left to right – Addition, subtraction applied last Operators applied from left to right

Decision Making: Equality and Relational Operators  if structure – Test conditions truth or falsity. If condition met execute, otherwise ignore  Equality and relational operators – Lower precedence than arithmetic operators  Table of relational operators on next slide

Decision Making: Equality and Relational Operators

using statements – Eliminate the need to use the std:: prefix – Allow us to write cout instead of std::cout – To use the following functions without the std:: prefix, write the following at the top of the program using std::cout; using std::cin; using std::endl;

1 // Fig. 1.14: fig01_14.cpp 2 // Using if statements, relational 3 // operators, and equality operators. 4 #include 5 6 using std::cout; // program uses cout 7 using std::cin; // program uses cin 8 using std::endl; // program uses endl 9 10 // function main begins program execution 11 int main() 12 { 13 int num1; // first number to be read from user 14 int num2; // second number to be read from user cout << "Enter two integers, and I will tell you\n" 17 << "the relationships they satisfy: "; 18 cin >> num1 >> num2; // read two integers if ( num1 == num2 ) 21 cout << num1 << " is equal to " << num2 << endl; if ( num1 != num2 ) 24 cout << num1 << " is not equal to " << num2 << endl; 25 Notice the using statements. using statements eliminate need for std:: prefix. Can write cout and cin without std:: prefix. if structure compares values of num1 and num2 to test for equality. If it is true, the body is executed. If not, body is skipped. fig01_14.cpp (1 of 2)

26 if ( num1 < num2 ) 27 cout << num1 << " is less than " << num2 << endl; if ( num1 > num2 ) 30 cout << num1 << " is greater than " << num2 << endl; if ( num1 <= num2 ) 33 cout << num1 << " is less than or equal to " 34 << num2 << endl; if ( num1 >= num2 ) 37 cout << num1 << " is greater than or equal to " 38 << num2 << endl; return 0; // indicate that program ended successfully } // end function main Enter two integers, and I will tell you the relationships they satisfy: is not equal to is greater than is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: is equal to 7 7 is less than or equal to 7 7 is great than or equal to 7 fig01_14.cpp output (2 of 2)

Questions – week 1 1.Write a program to input and convert temperature in degrees centigrade to corresponding temperature in degrees fahrenheit. 0 F = 9.0 / 5.0 x 0 C Pizza is sold small medium (10” diameter) and large (12” diameter). Create a program that computes the size (area) of the pizza and its cost per square inch. (Area = x diameter 2 / 4.0) (Costs July 2003, Medium = £4-50, Large = £5-50) 3. A line in 2D space is defined by its end points (x 1,y 1 ) and (x 2,y 2 ) where slope = (y 1 - y 2 ) / (x 1 - x 2 ) Write a program to input the two points, calculate the slope and test to avoid infinite gradient condition.