330 likes | 500 Views
CMPT 128: Introduction to Computing Science for Engineering Students. Introduction to the basic components of C and C++ programs. Introduction. Computer program : an ordered sequence of instructions whose objective is to accomplish a task .
E N D
CMPT 128: Introduction to Computing Science for Engineering Students Introduction to the basic components of C and C++ programs
Introduction • Computer program:an ordered sequence of instructions whose objective is to accomplish a task. • Programming:process of planning and creating a program • Programming Language: a set of symbols, special words (semantics), and rules for using those symbols and special words (syntax) to create statements to implement the ordered sequence of instructions to accomplish a task
Problem solving • When we try to solve a problem using programming we should always begin by breaking the problem into parts. • Each part describes a process that solves part of the problem (or an object that is part of the problem) • Each part can be implemented as a function within a program to solve the problem (or a class)
Programs and Libraries • Programs are divided into smaller modules called functions. • Functions can • be separately tested and reused by many programs • Use other functions • Be used by other functions • Become part of libraries
Programs • Every program has a main function. • The main function controls the highest level of abstraction of the program. • The main function may call other functions to complete lower level tasks (like evaluating an exponential) • Many commonly needed functions are supplied in libraries as part of the C and or C++ language, some less commonly needed functions are available as additional add on libraries
C and C++ • C is a subset of C++ • C codes will run inside a C++ application • All C libraries are available in C++ • C++ contains many extensions of C • C++ is object oriented and allows classes • C++ libraries that are based on classes cannot be used in C
The Components of a Program • Each C or C++ program includes a series of functions or program units. • Every C++ application program includes a main function, sometimes called a main program • There may be other functions used by the main program. • Functions from C and C++ libraries • Functions written by the user • For your first programs we will illustrate only a main function and some functions from libraries
Hello World: your first C++ program // My first C++ program // make the computer print Hello world // connect to any necessary libraries #include <iostream> // required, will be explained later in the course using namespace std; // the main function, implements the algorithm to solve the problem // the main function may use functions input an output functions from the iostream // library. cout is one of these functions. int main ( ) { cout << "Hello, world!“ << endl; return 0; }
Hello World: your first C program /* My first C++ program */ /* make the computer print Hello world */ /* connect to any necessary libraries */ #include <stdio.h> /* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the iostream library. cout is one of these functions. */ int main ( ) { fprintf( stdout, “Hello World \n” ); return 0; }
Comments • Comments are essential to clearly explain the purpose of your code and the methods of implementation used • When you use another programmer’s code • When another programmer tries to use your code • When you try to use your own code after months or years • Comments make it is much easier to figure out how things work in existing code. • Comments are not “executed”
Good Comments • Do give additional information not obvious from the code // This program calculates the first 20 primes … • Do not translate code into English // Add A to B (BAD COMMENT) C = A+B
C++ Comments: syntax • Comments begin // and end at the end of the line • Comments can appear on their own line • // This is a comment • Comments can appear on the same line as an expression • A = b + c; // add b to c and store the result in A
Hello World: your first program // My first C++ program // make the computer print the string “Hello world” // connect to any necessary libraries #include <iostream> using namespace std; // the main function, implements the algorithm to solve the problem // the main function may use functions from included libraries int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world return 0; }
C Comments: syntax • Comments begin /* and end */ • Comments can appear on their own line or on their own block of lines • /* This is a C comment it continues until it is closed */ • Comments can appear on the same line as an expression • A = b + c; /* add b to c, put the result in A */
Hello World: your first C program /* My first C program */ /* make the computer print Hello world */ /* connect to any necessary libraries */ #include <stdio.h> /* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the iostream library. cout is one of these functions. */ int main ( ) { fprintf( stdout, “Hello World \n” ); return 0; }
Libraries • A main function, or any other function may wish to use functions in the built in libraries of the C or C++ language or in user developed or application libraries • To use such functions our program must indicate that the libraries are being used and provide a way to access those functions
Hello World: C++ // My first C++ program // make the computer print the string “Hello world” // connect to any necessary libraries #include <iostream> // iostream is a library used to read and write using namespace std; // the main function, implements the algorithm to solve the problem // the main function may use functions from included libraries // the iostream library includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world return 0; }
Important • In our first C++ program we are using the iostream library. • This library is used to read from the keyboard and write to the screen • Any C++ program that reads or writes information should include the iostream library • Later we will look at the details of what we can do with this library. (much more)
Hello World: your first C program /* My first C program */ /* make the computer print Hello world */ /* connect to any necessary libraries */ #include <stdio.h> /* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the iostream library. cout is one of these functions. */ int main ( ) { fprintf( stdout, “Hello World \n” ); return 0; }
Important • In our first C program we are using the stdiolibrary. • This library is used to read from the keyboard and write to the screen • Any C program must use the stdio library • Any C++ program may use either the stdiolibrary or the iostream library
Example: Standard Libraries (C,C++_) • #include <stdio.h> • The # at the beginning of the line indicates that the command is for the C++ preprocessor (a preprocessor directive) • The Cor C++preprocessor puts the contents of the file stdio.hinto your program instead of the line of code above then passes the resulting code to the compiler • If the preprocessor directive to include the library is not part of your program, your program will not be able to use the functions in the library
Using Libraries in C and C++ • A C or C++ program that wants to use libraries starts with one or more include directives • #include <Library_Reference> • #include “Library_Reference” //(details later) • Here Library_Referenceindicates the name of a file containing a list of all the functions in the library.
Syntax of include directive #include <Library_Reference> #include “Library_Reference” • “ “ indicates that the files location should be completely specified by the programmer. • < > indicates that the file is part of the C or C++ language itself and can be located by the preprocessor, compiler and linker without programmer intervention.
Hello World: C++ // My first C++ program // make the computer print the string “Hello world” // connect to any necessary libraries #include <iostream> // iostream is a library used to read and write using namespace std; // not needed for C // the main function, implements the algorithm to solve the problem // the main function may use functions from included libraries // the iostream library includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world }
Name Spaces using namespace std • Indicates you wish to use the standard namespace • In large advanced programs it is possible that you want to use additional namespaces to prevent naming conflicts. • For this course you will always use the standard namespace and you will always have this line in your programs
Hello World: C++ // My first C++ program // make the computer print the string “Hello world” // connect to any necessary libraries #include <iostream> // iostream is a library used to read and write using namespace std; // the main function, implements the algorithm to solve the problem // the main function may use functions from included libraries // the iostream library includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world return 0; }
Creating a C or C++ main function int main ( ) { Body of the main function return 0; } • The start of the main method is indicated by the statement int main ( ) • The body of the main method (the code that accomplishes the task of the main method) is enclosed in { }. • The convention used in this course in to put each of the { } on a separate line.
Hello World: C++ // My first C++ program // make the computer print the string “Hello world” // connect to any necessary libraries #include <iostream> // iostream is a library used to read and write using namespace std; // the main function, implements the algorithm to solve the problem // the main function may use functions from included libraries // the iostream library includes read and write functions like cout int main ( ) { cout << "Hello, world!“ << endl; // Prints Hello world return 0; }
Body of the C++ program • For our first program the body consists of a single line of code cout << "Hello, world!“ << endl; • cout prints to the screen • Everything inside the “ “ is printed • endl means move to the start of the next line before printing anything else
Simple examples of C++ cout • Print the word OUCH to the screen cout << “OUCH”; • Move to the beginning of a new line then print the sum of 6.5 and 3.5 to the screen cout << endl << 6.5 + 3.5;
Hello World: your first C program /* My first C program */ /* make the computer print Hello world */ /* connect to any necessary libraries */ #include <stdio.h> /* the main function, implements the algorithm to solve the problem the main function may use functions input an output functions from the iostream library. cout is one of these functions. */ int main ( ) { fprintf( stdout, “Hello World \n” ); return 0; }
Body of the C program • For our first program the body consists of a single line of code fprintf( stdout, “Hello World \n” ); • fprintfprints to the screen • Everything inside the “ “ is printed except control characters preceeded by \ • \nmeans move to the start of the next line before printing anything else
Simple examples of C fprintf • Print the word OUCH to the screen fprintf( stdout, “OUCH”); • Move to the beginning of a new line then print the sum of 6.5 and 3.5 to the screen fprintf( stdout, “\n %f”, 6.5 + 3.5);