Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

COSC 120 Computer Programming
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
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.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Three types of computer languages
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
Basic Elements of C++ Chapter 2.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
CS1044 – Intro. To Programming in C++ Dr. Craig A. Struble.
COMPUTER SCIENCE I C++ INTRODUCTION
By Dr. Awad Khalil Computer Science & Engineering Department
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2 Overview of C++ Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using.
Introduction of C++ language. C++ Predecessors Early high level languages or programming languages were written to address a particular kind of computing.
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
1 CS161 Introduction to Computer Science Topic #3.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
CSC1201: PROGRAMMING LANGUAGE 2 Aseel Al Hadlaq 2nd Term
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 8/31/05CS150 Introduction to Computer Science 1 Hello World!
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
 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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
C++ First Steps.
Chapter 1.2 Introduction to C++ Programming
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: Introduction to computers and C++ Programming
What Actions Do We Have Part 1
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
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.
Procedural Programming
CS150 Introduction to Computer Science 1
Course websites CS201 page link at my website: Lecture slides
Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Review of C++ Language Basics
What Actions Do We Have Part 1
Chapter 2: Overview of C++
Presentation transcript:

Introduction to C++ Introduced by Bjarne Stroustrup of AT&T’s Bell Laboratories in mid-1980’s Based on C C++ extended C to support object-oriented programming

Procedural Programming Earlier programming approach intuitive task based Problem was broken into manageable sub-tasks Code was written to accomplish each sub-task Resulting “functions” assembled to solve overall problem Like a recipe

Object Oriented Programming Newer programming approach Data based rather than task based Abstract objects are created representing the data elements to be worked with Each object is self-contained Storing both data and procedures for manipulating the data Details hidden from the user – i.e. a “black-box” Objects are manipulated to solve a given problem Advantage: “black-box” localizes software changes Large software projects are more manageable and supportable

C++ Language Elements Comments Compiler directives Function main /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } Comments Compiler directives Function main Declaration statements Executable statements Example: miles.cpp

Comments /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } // symbols indicate a line comment - apply to just the rest of the line Block comments start with /* and end with */ - apply to as many lines as you like Used to describe the code in English or provide non-code information E.g. to include the name of the program or the author’s name

#include Compiler directive /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } Compiler directive Includes previously written code from a library into your program E.g. #include <iostream> has operators for performing input and output within the program The compiler knows where to locate libraries enclosed in <> Libraries allow for code reuse

using namespace std; Indicates to compiler that this program uses objects defined by a standard namespace called std. i.e. a portion of the iostream library Ends with a semicolon Follows #include directives in the code Must appear in all programs (in order to use cin and cout, as shown) /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }

Function main() Exactly one main function per program /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; } // end of main function Exactly one main function per program A function is a collection of related statements that perform a specific operation int indicates the return type of the function ( ) indicates no special information passed to the function by the operating system

Types of Statements All C++ statements end with “;” A single statement may span multiple lines Two main types of statements Declaration statements describe the data the function needs: const float KM_PER_MILE = 1.609; float miles, kms; (note: this is all one line!) Executable statements specify actions the program will take cout << “Enter the distance in miles: “; cin >> miles; /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }

Reserved Words (Keywords) Have special meaning in C++ Cannot be used for other purposes /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }

Identifiers Names for data (variables) and objects to be manipulated by the program Must begin with a letter or underscore (not recommended) Consist only of letters, digits, and underscores Cannot be reserved word Upper and lower case significant miles.cpp cin, cout, kms, km_per_mile, miles, std, endl /* miles.cpp - convert distance in miles to kilometers. */ #include <iostream> // class for stream input/output using namespace std; // use the standard namespace int main() // start of main function { const float km_per_mile = 1.609; // 1.609 km in a mile float miles= -1, // input: distance in miles kms; // output: dist in kilometers // Get the distance in miles. // Note: does not check if miles is valid!!! (i.e. >= 0) cout << "Enter the distance in miles: "; cin >> miles; // Convert the distance to kilometers. kms = km_per_mile * miles; // Output the distance in miles. cout << "The distance in miles is: " << miles << endl; // Output the distance in kilometers. cout << "The distance in kilometers is: " << kms << endl; return 0; }