Anda di halaman 1dari 32

INTRODUCTION TO

C++
Outline
 First Part:
 Introduction to C++
 Steps in C++ Program Development

 IDE

 General Form

 Structures of a Program

 Program Styles
Introduction
 C programming Language
 Developed in 1972 by Dennis Ritchie at Bell
Telephone laboratories
 used primarily as a systems programming language.
a language to write operating systems.
 efficient and flexible.
 Used for re-writing UNIX OS which is written in the
Assembly language before.
 C’s excellent portability allowed UNIX to be
recompiled on many different types of computers,
speeding its adoption.
C++
 Developed by Bjarne Stroustrup at Bell Labs in
1979.
 Extension to C
 Adds many new features to the C language
 Object-Oriented Programming Language(PL).
C++ Program Development
C++ Program Development (Step 1)

 Define the problem that you would like to solve.


 “what” step, where you figure out what you are going
to solve.
 Examples:
 “I want to write a program that will allow me to enter numbers, then
calculates the average.”
 “I want to write a program that generates a 2d maze and lets the
user navigate through it.”
 “I want to write a program that reads in a file of stock prices and
predicts whether the stock will go up or down.”
C++ Program Development (Step 2)

 Determine how you are going to solve the problem.


 “how” step, where you determine how you are going to
solve the problem you came up with in step 1.
 Characteristics of a good Solution:
 They are straightforward.
 They are well documented (especially any assumptions being
made).
 They are built modularly, so parts can be reused or changed later
without impacting other parts of the program.
 They are robust, and can recover or give useful error messages
when something unexpected happens.
C++ Program Development (Step 3)

 Write the program


 Two things needed:
 Knowledge of a programming language
 An editor
 Typical editor designed for coding:
1) Line numbering. (Ex. Error: Line 64)
2) Syntax highlighting and coloring. (Changes the color
of various parts of the program)
3) An unambiguous font. (ex. L and l, I and i, 0 and o)

 Name.cpp – name- filename; cpp- c++ extension


C++ Program Development (Step 4)

 Compiling
 Compiler – use for compiling the program.
 Check the legality of C++ code.
 Task:
1) To check your program and make sure it follows the rules of the
C++ language. If it does not, the compiler will give you an error
to help pinpoint what needs fixing.
2) To convert each file of source code into a machine language file
called an object file. Object files are typically named name.o or
name.obj, where name is the same name as the .cpp file it was
produced from. If your program had 5 .cpp files, the compiler
would generate 5 object files.
C++ Program Development (step 4 cont.)

 Example:
 G++ (g++ -c file1.cpp )

 Turbo c++
 Code::Blocks
C++ Program Development (step 5)

 Linking
 Processof taking all the object files generated by the
compiler and combining them into a single executable
program that you can run.
 Uses a program called LINKER.
C++ Program Development (Step 6)

 Testing and Debugging


 This
is the fun part of programming.
 You are able to run your executable and see
whether it produces the output you were expecting.
 If not, then it’s time for some debugging.
 Debugging – finding and fixing the error.
Integrated Development Environment (IDE)

 contains all of the things you need to develop,


compile, link, and debug your programs.
 Examples:
 VisualStudio C++
 Turbo C++

 Code::Blocks – free, open source, cross-platform

 MinGW

 Bloodshed’s Dev-C++

 Xcode, Eclipse
Integrated Development Environment (IDE)

 Project:
 Stores the names of all the code files we want to
compile, and also saves various IDE settings.
 Tells the compiler and linker which files to compile and
link
 Will not work on different IDE’s.

 Console Project - create programs that can be run from


the DOS or Linux command-line.
 no graphical user interface (GUI) and are compiled into
stand-alone executable files.
Integrated Development Environment (IDE)

 Project (cont):
 Will be added to a “workspace” or a “solution”.
 container that can hold one or more related projects.
 Must create a new workspace when you create a new project to
avoid errors and for easy implementation.
 Build configuration (build target) - collection of project
settings that determines how your IDE will build your project.
 Includes:
 Executable file name
 Directory the executable will be output in
 What directories the IDE will look in for other code and header files,
whether to keep or exclude debugging information, and how highly to
have the compiler optimize your program.
 Just leave it as is.
Integrated Development Environment (IDE)

 Debug configuration
 designedto help you debug your program, and is
generally the one you will use when developing your
programs.
General Form
Preprocessor directives(ex. #include, #define)
Main function heading (ex. Int main())
{
declarations; (ex. int x;)
executable statements; (ex. Cout<<x;)
}
Structure of a Program
 Preprocessor Directives
 Commands that gives instructions to the C++
preprocessor
 Preprocessor – modify the text of a C++ program before it
is compiled.
 Begins with the number symbol (#)
 #include and #define – most commonly used

 Libraries – collection of useful function and symbol


 Has a standard header file whose name ends with symbol .h
 Ex: string.h, math.h, stdio.h, iostream.h
Structure of a Program
 Standard Libraries – provides additional
functionality for the user.
 Ex. Iostream library – contains functionality for writing
to the screen and getting input from a console user.
Structure of a Program
 Uses of #include and #define:
 #include – gives a program access to the library.
 Causes the predecessor to insert definitions from a standard
header file into a program before compilation.
 Ex: #include<iostream> is used by cout<< and cin>>
operations
 << - output operator
 >> - input operator

 Can occur anywhere in a file.


Structure of a Program
 Uses of #include and #define:
 #define – changes all occurrences of the identifier.
 #define LIMIT 100, #define PI 3.14
 Symbolic constants.
 Can occur anywhere in the program.
 It affects only the lines in the file that
comes after it.
 Normally placed at the beginning of the file.
 Identifiers must be written in CAPITAL LETTERS
 Quoted strings are not modified (ex. “PI”)
Structure of a Program
 Functions – collection of statements that executes
sequentially.
 Every C++ program must contain a special function
called main.
 When the C++ program is run, execution starts with the
first statement inside of function main.
 Functions are typically written to do a very specific job.
 Ex: Max() , CalcGrade()
 Hint:It’s a good idea to have your main() function live in
a .cpp file with the same name as your project.
Structure of a Program
 Format:
main()
{
function body
}
 Example:
main()
{
cout<<”Hello, World”;
}
Structure of a Program
 Statements and expressions
 Statements - most common type of instruction in a
program
 smallestindependent unit in the language
 convey the compiler that we want to perform a task.
 terminated by a semicolon(;)
Structure of a Program
 Types of Statement:
 Declaration Statement
 It tells the compiler that what you’ve declared(ex. Int x) is a
variable.
 Variables – used to provide a name for a value that can vary.
 must be declared before they are used.
 Assignment Statement - Used for assigning values in a
variables (ex. x=5)
 Output Statement – displaying the result to the screen

 Return Statement -sends a value back to the operating


system that indicates whether it was run successfully or not.
 A return type of void means the function does not return a value. A return
type of int means the function returns an integer value to the caller.
Ex:
//does not return a value to the caller
void returnNothing(){
// function does not return a value so no return statement is needed
}

// int means the function returns an integer value to the caller


Int return5(){
return 5; // this function returns an integer, so a return statement is needed
}
Structure of a Program
 Expression - is a mathematical entity that evaluates
a value.
 Ex. 2+3 = 5
 Involves:
 Values (ex. 2), variables(ex. x), operators(ex. +) and
functions (return an output value based on some input value).
 Can be singular (such as 2, or x) or;
 Compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3))
Structure of a Program
 Syntax and syntax errors
 Syntax - rules about how your programs must be
constructed in order to be considered valid.
 Compiler - is responsible for making sure your program
follows the basic syntax of the C++ language.
 Syntax error – violation of the rules.
Program Styles
 Spaces in a program
 Can improved the styles in a program.
 Required between consecutive words in a program line
 Compiler ignore extra blanks between words and
symbols
 Leave a blank space after the comma and before and
after operators such as * . – and =.
 Indent the body of the main function and insert blank
lines between sections of the program.
 Do not insert blank spaces where they do not belong.
Program Styles
 Comments
 // - single line
 /* <statement> */ - multiple lines

 Used for describing the purpose of the program, the


used of identifiers and the purpose of each program
step.
 Helps read and understand the program.

 Ignores by the compiler and are not translated into


machine language.
Program Styles
 Comments
 Each program begin with a header section that consists of:
 Programmer’s Name
 Date of the current version
 Purpose of the program
 Ex:
/*
*Programmer: Elizabeth T. Mamasig
* Date Completed: Sept.1, 2013
*Instructor: Prof. Michael Batan
*Compute grades of a students.
*/
End of Part 1

Anda mungkin juga menyukai