lesson 1- programmingwithc++

21

Upload: john-son

Post on 03-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 1/21

Page 2: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 2/21

What Do you mean by a Program? A program is a set of instructions that are

 grouped together to accomplish a task or tasks. 

These instructions can be given to a computer.

The instructions, called machine code or assemblycode consist of things like reading and writingmemory, arithmetic operations, and comparisons.

these instructions sound simple, it is actually possible

to solve a huge group problems with them. the individual instructions that the machine actually 

quite simple or low-level in computer parlance.

Page 3: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 3/21

What do you mean by Programming?

Computer Programming is the art of making a

computer do what you want it (the computer) to do.

 At the very simplest level it consists of issuing asequence of commands to a computer to achieve anobjective

 Writing software, computer programs, is describinghow to do something. In its simplest form, it is a lotlike writing down the steps it takes to do something - a

 process. Programming will help you learn the importance of 

clarity of expression.

Page 4: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 4/21

Programming Language  A vocabulary and set of grammatical rules for instructing a

computer to perform specific tasks. “A programming language is a machine-readable artificial 

language designed to express computations that can be performed by a machine, particularly a computer.Programming languages can be used to create programs

that specify the behavior of a machine, to expressalgorithms precisely, or as a mode of humancommunication.”  

Many programming languages have some form of writtenspecification of their syntax and semantics

Machine Code

Program

Page 5: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 5/21

C++ is a Programming LanguageC++ is an innovative programming language

History of C++!!! C++ is a statically typed, free-form, multi-paradigm, compiled, general-purposeprogramming language.It is regarded as a "middle-level" language, as itcomprises a combination of both high-level andlow-level language features. 

It was developed by Bjarne Stroustrup starting in

1979 at Bell Labs as an enhancement to the Clanguage and originally named C with Classes.It was renamed C++ in 1983. 

Page 7: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 7/21

Static typing

 A programming language is said to use static typing when type checking is performed during compile-timeas opposed to run-time.

Page 9: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 9/21

Compiled language A compiled language is a programming language

 whose implementations are typically compilers(translators which generate machine code from sourcecode), and not interpreters (step-by-step executors of source code, where no translation takes place).

Page 10: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 10/21

Source Code -

#include <iostream> 

using namespace std;

int main (){

cout << "Hello World!";

return 0;

}

Output

Hello World!

 We have to compile the program and execute to getthe answer.

Page 11: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 11/21

#include <iostream> Lines beginning with a hash sign (#) are directives for

the preprocessor.

#include <iostream> tells the preprocessor to includethe iostream standard file.

iosream includes the declarations of the basicstandard input-output library in C++, and it is

included because its functionality is going to be usedlater in the program.

Page 12: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 12/21

using namespace std; All the elements of the standard C++ library are

declared within what is called a namespace, thenamespace with the name std .

So in order to access its functionality we declare withthis expression that we will be using these entities.

This line is very frequent in C++ programs that use the

standard library, and in fact it will be included in mostof the source codes.

Page 13: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 13/21

int main () This line corresponds to the beginning of the

definition of the main function.

The main function is the point by where all C++programs start their execution, independently of itslocation within the source code.

Page 14: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 14/21

cout << "Hello World!"; This line is a C++ statement.

cout is the name of the standard output stream in C++,

and the meaning of the entire statement is to insert asequence of characters (in this case the Hello Worldsequence of characters) into the standard outputstream (cout, which usually corresponds to thescreen).

cout is declared in the iostream standard file withinthe std namespace.

Page 15: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 15/21

return 0; The return statement causes the main function to

finish.

return may be followed by a return code A return code of 0 for the main function is generally 

interpreted as the program worked as expected without any errors during its execution.

Page 16: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 16/21

Second Program// my second program in C++ 

#include <iostream> 

using namespace std;int main ()

{

cout << "Hello World! ";

cout << "I'm a C++ program";

return 0;

}

Page 17: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 17/21

Comments Comments are parts of the source code disregarded by 

the compiler. They simply do nothing. Their purpose isonly to allow the programmer to insert notes ordescriptions embedded within the source code.

C++ supports two ways to insert comments:// line comment

/* block comment */ 

Page 18: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 18/21

/* my second program in C++ with more comments */ 

#include <iostream> 

using namespace std;int main ()

{

cout << "Hello World! "; // prints Hello World! 

cout << "I'm a C++ program"; // prints I'm a c++ program

return 0;

}

Page 19: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 19/21

Identifiers and reserved words

 A valid identifier is a sequence of one or more letters, digits orunderscore characters (_). Neither spaces nor punctuation marksor symbols can be part of an identifier. Only letters, digits and

single underscore characters are valid. In addition, variable identifiers always have to begin with a letter.

They can also begin with an underline character (_ ), but in somecases these may be reserved for compiler specific keywords orexternal identifiers, as well as identifiers containing twosuccessive underscore characters anywhere.

In no case can they begin with a digit.

Page 20: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 20/21

 Another rule that you have to consider when inventing your ownidentifiers is that they cannot match any keyword of the C++ languagenor your compiler's specific ones, which are reserved keywords. Thestandard reserved keywords are:

asm, auto, bool, break, case, catch, char, class, const, const_cast,continue, default, delete, do, double, dynamic_cast, else, enum,explicit, export, extern, false, float, for, friend, goto, if, inline, int, long,mutable, namespace, new, operator, private, protected, public, register,reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct,

switch, template, this, throw, true, try, typedef, typeid, typename,union, unsigned, using, virtual, void, volatile, wchar_t, while

Page 21: Lesson 1- ProgrammingwithC++

7/28/2019 Lesson 1- ProgrammingwithC++

http://slidepdf.com/reader/full/lesson-1-programmingwithc 21/21

 Additionally, alternative representations for some operatorscannot be used as identifiers since they are reserved words undersome circumstances:and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor,xor_eq

 Very important: The C++ language is a "case sensitive"  language. That means that an identifier written in capital lettersis not equivalent to another one with the same name but written

in small letters. Thus, for example, the RESULT variable is notthe same as the result variable or the Result variable. These arethree different variable identifiers.