Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed

Slides:



Advertisements
Similar presentations
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Advertisements

 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.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
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.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Simplest program and Data Output Sen Zhang. The simplest program looks like a single person company! void main() { // this starts a comment line // here.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to C++ Programming
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 3 Getting Started with 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.
Creating your first C++ program
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
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:
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)
Control Structures (B) Topics to cover here: Sequencing in C++ language.
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
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Learners Support Publications Introduction to C++
Introduction to C++.  Computers: CPU, Memory & Input / Output (IO)  Program: Sequence of instructions for the computer.  Operating system: Program.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
LESSON 2 Basic of 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.
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)
Bill Tucker Austin Community College COSC 1315
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.
Programming what is 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
Topic Pre-processor cout To output a message.
CMPT 201.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
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.
Computing Fundamentals
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Beginning C++ Programming
Basic Elements of C++ Chapter 2.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Programming Funamental slides
Programming Funamental slides
Introduction to C++ Programming
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Basics
COMS 261 Computer Science I
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:

Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed CP Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed

Basics Comments in C/C++ Preprocessor Directives Name Spaces Header Files Program Entry Point Return Type

Basics … Programming Practices Identifiers Keywords Constants Variables Algorithms Expressions Statements

Basic C++ Program Structure /********************************************************** * Header Comments **********************************************************/ pre-processor directives global declarations int main() { declarations and executable statements return 0; }//end block of main // comments required by some organizations!

#include <iostream.h> //declares standard I/O library /***************************************************** * Sum two numbers ******************************************************/ #include <iostream.h> //declares standard I/O library int main() //must have one and only one function named main { int number1, number2; //declare two integer variables cout << “enter two integers” << endl; //prompt for input cin >> number1 >> number2; //input values for two variables from keyboard cout << number1 + number2; //output sum of two numbers to the screen return 0; }//end block of main

Our First C++ Program Enter this program, then compile and run it. */ /* Program #1 - A first C++ program. Enter this program, then compile and run it. */ #include <iostream.h> // main() is where program execution begins. int main() { cout << “Hello, world!"; return 0; } Write on the blackboard too.

Dissection of Program Comments have two forms in C++ //Single line comments /*Multi-line comments*/ /* Program #1 - A first C++ program. Enter this program, then compile and run it. */ This is a comment (C style) Ignored by the compiler i.e. can write anything and the compiler won’t see it at all Multiline or single or mixed with code. Why use comments?

Dissection of Program C++ style comment // main() is where program execution begins. C++ style comment Compiler ignores the whole line If multiline comments, use // on each line

Dissection of Program int main() { program statements return 0; } Every C++ program has to have this Function; execution begins here even if other functions there; operating system calls main( ) and main returns to it int means returns an integer; () means takes no arguments; {…} signify start and end of function Variations on it e.g. main( ); int main(void); and more

Dissection of Program cout << “Hello, world!"; Think of cout as the monitor screen (console output) << gives direction of data; Put to or Insertion operator Prints the entire string within quotes to the screen This whole line is a statement C/C++ Statement that ends with a ;

Dissection of Program #include <iostream> Tells preprocessor to include contents of the text file iostream with your source code before compiling iostream has details about things that handle input and output e.g. cout iostream is called an include file or header file This is called an include directive or preprocessor directive Can also use <iostream.h>; then no need to use the namespace construct on the next slide. Older version

Dissection of Program using namespace std; Namespaces are regions where your function and variable names are recognized The standard library is defined in a namespace called std. E.g. cout part of std namespace; similarly all others in iostream Two functions with same names = confused compiler Declare each in its own namespace namespacemale::naseem namespacefemale::naseem std::cout Or mynamespace::cout

#include files <filename.h> //Standard C library header file <filename> //Standard C++ library files Files found in the directory defined by the INCLUDE environment variable “myfile.h” //Your files Files found in the current directory

Simple I/O cin cout streams input from standard input uses the >> operator (input operator, stream extraction operator) cout streams output to standard output uses the << operator (output operator, stream insertion operator)

Algorithms A series of instructions in a specific order leading to a solution Analogous to recipe, directions, procedure, routine Anyone knows the origin of the word Algorithm? Ninth century Persian mathematician and astronomer Al-Khowarizmi Algebra also derived from the name of his book.

Rise&Shine Algorithm Any comments? Get out of bed Take off pajamas Take a shower Get dressed Eat breakfast Carpool to work Any comments?

A Variation of Rise&Shine Get out of bed Take off pajamas Get dressed Take a shower Eat breakfast Carpool to work Any comments?