C++ Introduction.

Slides:



Advertisements
Similar presentations
Introduction to C++ An object-oriented language Unit - 01.
Advertisements

Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
Module 6: Introduction to C Language ITEI102 Introduction to Programming Structure of C++ Program - Programming Terminologies - Microsoft Visual Studio.
EC-111 Algorithms & Computing Lecture #1 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Introduction to Programming in C++ John Galletly.
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.
INTRODUCTION T.Najah Al_Subaie Kingdom of Saudi Arabia Prince Norah bint Abdul Rahman University College of Computer Since and Information System CS240.
Three types of computer languages
Program Flow Charting How to tackle the beginning stage a program design.
Program Flow Charting How to tackle the beginning stage a program design.
Chapter 1 Introduction to C Programming. 1.1 INTRODUCTION This book is about problem solving with the use of computers and the C programming language.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Chapter Two Introduction to the Programming Language C.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Programming is instructing a computer to perform a task for you with the help of a programming language.
Introduction to C++ Programming
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
COMPUTER PROGRAMMING. Introduction to C++ History Merges notions from Smalltalk and notions from C The class concept was borrowed from Simular67 Developed.
Chapter 01: Introduction to Computer Programming
COMPUTER SCIENCE I C++ INTRODUCTION
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.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
Instructor: Tina Tian. About me Office: RLC 203A Office Hours: Wednesday 1:30 - 4:30 PM or .
Chapter 3: Completing the Problem- Solving Process and Getting Started with C++ Introduction to Programming with C++ Fourth Edition.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” C
Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice:
Introduction to C++ Programming Language
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
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.
INTRODUCTION Kingdom of Saudi Arabia Princess Nora bint Abdul Rahman University College of Computer Since and Information System CS240.
Chapter 1 Introduction Chapter 1 Introduction 1 st Semester 2015 CSC 1101 Computer Programming-1.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 05, 2005 Lecture Number: 4.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CS Computer Science I. BCPL was developed in 1967 as a language for writing operating systems and software compilers In 1970, the creators of the.
CHAPTER 1: INTRODUCTION C++ Programming. CS 241 Course URL: Text Book: C++ How to Program, DETITEL & DEITEL, eighth Edition.
Learners Support Publications Introduction to C++
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 2 C++ Basics.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Software Engineering Algorithms, Compilers, & Lifecycle.
C++ Programming Chapter 1 Programming & Programs.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
Chapter 15 - C++ As A "Better C"
Chapter 1 Introduction 2nd Semester H
Chapter 1.2 Introduction to C++ Programming
Introduction to Computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
Completing the Problem-Solving Process
Computer Programming Techniques Semester 1, 1998
C Programming Hardik H. Maheta.
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
CS IA: Procedural Programming CS IB: Object-Oriented 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
Computer Terms Review from what language did C++ originate?
Presentation transcript:

C++ Introduction

C++ Basics History Programming languages Hello World! Libraries The main method Output

Brief History of C++ Derived from C Adds classes and other features Including the increment operator, ++ Developed by Bjarne Stroustrup at Bell Labs in 1979 Standardized by ANSI-ISO in 1998

Programming Languages C++ is a high level programming language It can be compiled into machine code And executed on a computer Programming languages are formal and lack the richness of human languages If a program is nearly syntactically correct (but not correct) then it will not compile The compiler will not "figure it out"

So Though C++ programs are written in an English like language they are very formal They must be written using correct syntax They must be precise and unambiguous A program is a sequence of instructions that must be followed step by step Each instruction must be correctly specified for the program to function as desired

Processing a C++ Project run-time errors correct errors write in a text editor Source File (.cpp) Debugged Executable compile test Object File (binary) syntax errors link errors linker links files Other Object Files Executable File (binary)

HELLO WORLD! 1st way

Written between /* and */ or following a //. 1 // Fig. 1.2: fig01_02.cpp 2 // A first program in C++ 3 #include <iostream> 4 5 int main() 6 { 7 std::cout << "Welcome to C++!\n"; 8 9 return 0; // indicate that program ended successfully 10 } Comments Written between /* and */ or following a //. Improve program readability and do not cause the computer to perform any action. preprocessor directive Message to the C++ preprocessor. Lines beginning with # are preprocessor directives. #include <iostream> tells the preprocessor to include the contents of the file <iostream>, which includes input/output operations (such as printing to the screen). C++ programs contain one or more functions, one of which must be main Parenthesis are used to indicate a function int means that main "returns" an integer value. More in the next class. A left brace { begins the body of every function and a right brace } ends it. Prints the string of characters contained between the quotation marks. The entire line, including std::cout, the << operator, the string "Welcome to C++!\n" and the semicolon (;), is called a statement. All statements must end with a semicolon. return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally. Welcome to C++!

1.19 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

1.19 A Simple Program: Printing a Line of Text << 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

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

1.20 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

1.20 Another Simple Program: Adding Two Integers = (assignment operator) Assigns value to a variable Binary operator (has two operands) Example: sum = variable1 + variable2;

Hello World 2nd way #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; }

Libraries A program may need to use functions included in other libraries The built in libraries of C++, or Libraries developed by users To use library functions a program must import the library

Including Files The #include directive is used to insert a library file into a program The library file contains declarations (names) of all of the functions in the library There are two general forms of the #include directive #include <library> The file is part of the C++ language #include "library" The file location should be specified by the programmer #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; }

Preprocesser Directives The # indicates a command for the C++ preprocessor Known as a preprocessor directive Such commands do not end in a semi-colon The C++ preprocessor copies the contents of the included file into the program Replacing the line containing the directive Such files contain function headers

User Libraries Unlike standard libraries the preprocessor needs the location of user libraries Such libraries are given in quotes ("") The location of the file must be specified by the programmer Locations are given relative to the directory containing the source file File extensions should be given e.g. #include "myfile.h"

Namespaces (briefly) Two different programmers might use the same name for some function or class Which would result in ambiguity Namespaces deal with this issue A namespace collects name definitions And ensures that duplicate names do not exist The std namespace contains names defined in many standard C++ libraries #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; }

Main Function A C++ program is made up of a number of functions A function is self contained part of a program Every C++ application has a main function The main function is called when the executable file is run The main function is made up of two parts Its header: int main() Its body: the code in {}s #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; }

More About {}s The main function's body starts with an opening { And ends with a closing } These curly brackets are used to indicate a body of code Belonging to a function, or Loop, or Decision

Screen Output cout is a function in the iostream library It outputs to the screen The << operator is often called the insertion operator "Hello World!" is a string Words contained in double quotes ("") are treated as arbitrary text by the compiler not program instructions #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; }

Semi-Colons A semi-colon indicates the end of a C++ command By convention most C++ commands are written on one line However, the newline character does not indicate the end of a command Don't forget semi-colons Omitting them usually prevents the program from compiling

Return Statement The return statement ends a function More correctly, ends the function's invocation It does not have to be the last line of a function But typically is the last line of a main function The type of the returned value should match the function's return type In this case 0 is an integer (or int) More on this later ... #include <iostream> using namespace std; int main(){ cout << "Hello World!"; return 0; }

Summary Include libraries required for your program The iostream library is required for standard input and output Specify the std namespace Write a main function That implements the solution's algorithm The body of the function is contained in {}s Statements in function bodies end with a ;

Reserved Words A reserved word is part of the C++ language A special word that gives an instruction to the compiler e.g. int and using Reserved words are typically highlighted in a special colour. Blue is highlighted in Visual Studio

Comments The Hello World program did not include any documentation Programs should include English explanations that describe what the program is doing These are called comments C++ comments can be specified in two ways Beginning with // and ending at the end of the line Beginning with /* and ending with */ Comments are ignored by the compiler

Hello World Program // A first C++ program // Prints "Hello World" // Include libraries #include <iostream> using namespace std; // main function to solve the problem int main(){ cout << "Hello World!"; //prints Hello World return 0; }

Temperature Conversion

Requirements Write a program to convert Celsius to Fahrenheit Input is to be received from the keyboard Output is to be displayed on the screen Conversion algorithm F = C × 9  5 + 32

#include <iostream> using namespace std; // Converts Celsius to Fahrenehit int main() { // Declare variables double celsius = 0; double fahrenheit = 0; // Get celsius input cout << "Enter the Celsius value: "; cin >> celsius; // Calculate fahrenheit fahrenheit = celsius * 9 / 5 + 32; // Display output cout << celsius << " Celsius = " << fahrenheit << " Fahrenheit"; return 0; }

Variables The example includes the user of variables Variables are used to store data The value stored in the variable can be changed using the assignment operator, = In the example both variables are doubles A double is a numeric data type Used to represent floating point numbers

C++ Identifiers User created names must only contain Letters, both upper and lower case Digits (0 to 9) The underscore character (_) Identifiers must begin with a letter or _ By convention variables usually start with a lower case letter C++ is case sensitive

Naming Stuff Identifiers should be descriptive to make code easier to understand Names should be a reasonable length, and Should convey information about the purpose of the identifier Consider a variable to store a rectangle's height height, or rect_height, or rectHeight are fine ht, or, even worse, a, are not clear variableForTheHeightOfTheRectangle is too long

Legal Identifiers? cycle A!star int Score xc-y race#1 my_variable Int  A!star  (!) int  (reserved word) Score xc-y  (-) race#1  (#) my_variable  Int  (but horrible) cmpt128variable 3rdVariable  (starting 3)

Variables and Types To recap, a variable name should Be legal Only contain a-z, A-Z, 0-9 or _ Not be a reserved word Start with a lower case letter Convey meaning The type of a variable should be defined