3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++

Slides:



Advertisements
Similar presentations
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Advertisements

COSC 120 Computer Programming
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
Three types of computer languages
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
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.
Chapter 02 (Part III) Introduction to C++ Programming.
Week 1 Algorithmization and Programming Languages.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Basic Program Construction
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
Brief Version of Starting Out with C++ Chapter 1 Introduction to Computers and Programming.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Chapter 11  Getting ready to program  Hardware Model  Software Model  Programming Languages  Facts about C++  Program Development Process  The Hello-world.
Objective Write simple computer program in C++ Use simple Output statements Become familiar with fundamental data types.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Structure of Simple C++ Program Chapter 1 09/09/13.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Lecture 4 Computer Programming // sample C++ program #include using namespace std; int main() { cout
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Lecture 3: Getting Started & Input / Output (I/O)
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Great way to learn is by example so fire up Visual Studios C (at home make sure you custom install with C++ - no longer default) by Deborah R. Fowler.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
CMPT 201.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Completing the Problem-Solving Process
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Beginning C++ Programming
Computer Programming Lecturer: Ir Dr Frank H.F. LEUNG
Basic Elements of C++ Chapter 2.
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 2 What you have learned last week? Built (compiling and linking) a simple C++ program under the Visual Studio environment. – Source code, object code, and executable code Encountered syntax errors. – Compile-time errors are useful. – Run-time errors Debugging process – Debugging all kinds of errors

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 3 What you have learned last week? Executed the program in the “console mode.” – Many command-line applications are known as console applications. Installed Visual Studio at your own PC. Reading

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C Revisit HelloWorld

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 5 A simple program #include int main() { std::cout << "Hello World!" << std::endl; return 0; }

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 6 Elements of a C++ program A main () function – A program consists of many functions. – It is expected to return an integer value. Inputs and outputs – Standard output stream ( std::cout ) – Standard input stream ( std::cin ) – Insert a new-line character ( std::endl ) Standard library – It is a collection of classes and functions, such as iostream. Data type: character strings and the new-line character Operators: << is an insertion operator. Statements

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 7 #include int main() { std::cout << "Hello World!" << std::endl; return 0; } preprocessor The actual program Preprocessor: Instruct the compiler on how to compile the program Will not generate machine codes Start with a pound ( # ) symbol Actual program: Every C++ program must have the main() function It is the beginning point of every C++ program Preprocessor and Program Codes Run before compiling

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 8 Preprocessor #include When compiling a file, we need to obtain the definitions of some terms in the program codes. These definitions are recorded in some header files. These files are shipped with the compiler or other resources. #include tells the compiler where to find the header files and insert this file to that location of the program e.g. #include tells the compiler it should get the file iostream through the default path. e.g. #include "myfile" tells the compiler it should get the file myfile in the current folder.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 9 Program Codes int main() { std::cout << "Hello World!" << std::endl; return 0; } The basic element of a program is function. A function is composed of: 1. Return Type 2. Function name 3. Input parameters 4. Program codes enclosed by the opening and closing braces Note: The meaning of std::cout is checked in iostream Think from the point of view of the compiler.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 10 main() Call main() Return 0 Means everything fine on executing main() as it is the last statement. The main() function is the beginning point of a program. When executing a program, the operating system will first call the main() function of this program. If the above main() function executes successfully, it should return an integer 0 to the operating system.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 11 int main() { std::cout << "Hello World!" << std::endl; return 0; } Send the string Hello World! to std::cout – the standard output, defined in iostream Return an integer 0 to the operating system In console mode, the standard output is just the console, or the Command prompt window. In C++, character string is represented by a sequence of characters enclosed by “ ”. std::endl means newline (or Enter), also defined in iostream. Program Codes

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 12 std::cout and std::endl means that we are referring to the cout and endl of the std namespace The std namespace is defined in iostream. Namespace – A new feature of C++ Designed to help programmers develop new software components without generating naming conflicts. Naming conflict – A name in a program that may be used for different purposes by different people. cout and endl are not a part of C++, people can use these two names for any purpose; not necessarily referring to the standard output and newline. Namespaces Folders and files concept in XP

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 13 #include namespace myns { int cout=0; //Integer variable } //No semi-colon int main() { std::cout << myns::cout << std::endl; return 0; } We can have our own cout by putting it in a namespace defined by ourselves In fact, another definition of cout can be found in iostream. The result of this program is a number 0 shown on the standard output. This cout refers to the standard output This cout refers to the number 0

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 14 #include namespace myns { int cout=0; } int main() { std::cout << cout << std::endl; return 0; } That’s why using cout without the namespace is an error, because the system does not know which cout you are referring to. 

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 15 It may be a bit cumbersome to write the namespace every time. A short form is to use the using statement. #include using namespace std; int main() { cout << "Hello World!" << endl; return 0; } All names that are not a part of C++ will belong to the namespace std, unless otherwise stated. No need to put std in front of cout and endl

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 16 #include using namespace std; int main() { cout << "Hello there.\n"; cout << "Here is 5: "<< 5 << "\n"; cout << "endl writes a new line to the screen."; cout << endl; cout << "Here is a very big number:\t" << << endl; cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl; cout << "Here's a fraction:\t\t" << (float) 5/8 << endl; cout << "And a very very big number:\t"; cout << (double) 7000*7000 << endl; cout << "Replace Frank with your name...\n"; cout << “Frank is a C++ programmer!\n"; return 0; } We can also print integers, floating- point numbers or even combination of string and integers in standard output \n - Another way to show newline \t – Add a tab character Another line escape sequence Ex. 3.1a

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 17 Result

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 18 Comments A program needs to be well commented to explain the important points of the program. Adding comments in the program will not affect the program execution but only improve readability. Comments can be added in two ways: #include using namespace std; int main() { /* Text between these two marks are comments. */ cout << "Hello World!\n"; return 0; // Text after that are also comments }

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 19 Exercise 3.1a a. Build the program in p. 16. Note the output on the console. b. Add one statement to the program which will show your name and height (in cm) in a single sentence. The name should be shown as a character string. The height should be shown as an integer. c. Use the comment symbols /* and */ to comment the statements from line 4 to line 8 (inclusive). Is there any change to the results output?

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 20 Although a single main() function is enough for any C++ program, it’s bad to do everything by a single function. C++ allows nesting of functions to facilitate "divide and conquer" of jobs. The main() can call other functions to help it complete a task. When a function is called, the program branches off from the normal program flow. When the function returns, the program goes back to where it left before. More on Functions

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 21 Mr. A wants to decorate his house Call his friend B to help mowing Return him the mowed lawn Call his friend C to help painting Return him the painted house Call a function is just similar to asking somebody to help! 1 2 3

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 22 #include using namespace std; //function DemonstrationFunction() //show a useful message void DemonstrationFunction() { cout << "In Demonstration Function\n"; cout << "Print one more line\n"; } //function main - prints out a message, then //calls DeomonstrationFunction, then shows //the second message. int main() { cout << "In main\n"; DemonstrationFunction(); cout << "Back in main\n"; return 0; } Return nothing A function is called They must be the same A function is defined

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 23 #include using namespace std; //function DemonstrationFunction() // show a useful message void DemonstrationFunction() { cout << "In Demonstration Function\n"; cout << "Print one more line\n"; } //function main - prints out a message, then //calls DeomonstrationFunction, then shows //the second message. int main() { cout << "In main\n"; DemonstrationFunction(); cout << "Back in main\n"; return 0; } The execution sequence is like that

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 24 To let the called function really help the main(), sometimes parameters are passed from main() to the called function. After finishing the computation, the function should pass back the results to main(). It can be achieved by the return statement. main() Branch to function(a,b) return c Passing Parameters to Function

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 25 Mr. A wants to decorate his house Call his friend B to help mowing and give him a mowing machine Return him the mowed lawn Call his friend C to help painting and give him the paint brush Return him the painted house If you want your friend to help, you'd better give him the tool! 1 2 3

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 26 #include using namespace std; int Add (int x, int y) {cout << "In Add(),received "<<x<<" and "<<y<<"\n"; return(x+y); } int main() { cout << "I'm in main()!\n"; int a,b,c; cout << "Enter two numbers: "; cin >> a; cin >> b; cout << "\nCalling Add()\n"; c = Add(a,b); cout << "\nBack in main().\n"; cout << "c was set to " << c; cout << "\nExiting...\n\n"; return 0; } Add() is called with two parameters Add() will return an integer x+y back to main() c holds the return value of Add() Input parameters need to declare type - the same as those in the calling function

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 27 Exercise 3.1b a. Build the program in the last slide. Note the output on the console. b. Modify main() to calculate the square of c. Add one more function called Square() to achieve this. The Square() function will take the square of the parameter that is passed to it. It will return the result in the form of an integer back to the calling function.

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 28 Reading Hours 2-3 in 24 hours

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 29 Acknowledgement The slides are based on the set developed by Dr. Frank Leung (EIE).

3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 30 Some references – –