0% found this document useful (0 votes)
22 views

Computer Programming Chapter Two Basics of C++ Programming Part

Uploaded by

Dereje sisay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Computer Programming Chapter Two Basics of C++ Programming Part

Uploaded by

Dereje sisay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Computer

Programming {
Chapter Two: Basics
of C++ Programming
{
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
Outline
1. Introduction to C++
2. C++ IDE
3.The C++ Standard Library
4. Parts of a program: Keywords, Identifiers, Inputs, Outputs, Comments
5. Data Types
6.Variables
7. Constants
8.Operators
1. Assignment Operators
2.Compound Assignment Operators
3.Arithmetic Operators
4.Relational Operators
5.Increment and Decrement Operators
6.Infix and postfix types
7.Precedence of Operators
2
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
Introduction to C++
▪ C++programming language developed by Bjarne Stroustrup in 1979 at Bell Labs.

▪ C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions
of UNIX.

▪ C++ is regarded as a middle-level language, as it comprises a combination of both


high-level and low-level language features.

▪ It is a superset of C, and that virtually any legal C program is a legal C++ program.

▪ C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form


programming language that supports procedural, object-oriented, and generic
programming.
3
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
Introduction to C++
▪ The most important facilities that C++ adds on C language are C++
fully supports object-oriented programming, including the four pillars of
object-oriented development:
▪ Encapsulation

▪ Inheritance

▪ Polymorphism

▪ Addition of namespaces and STL (Standard Template Library) and Many new
keywords.
4
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
COMPILERS and IDE FOR PROGRAMMING WITH C++
To start using C++, you need two things:
1. A text editor, like Notepad, to write C++ code and
2. A compiler, like GCC, to translate the C++ code into a language that the computer will
understand

▪ Some of the free compilers for windows


are given below.
▪ (i) Borland C++
▪ (ii) Dev-C++
▪ (iii) Quincy
▪ (iv) Microsoft Visual Studio
▪ (V) Code Block
▪ And more

5
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
COMPILERS and IDE FOR PROGRAMMING WITH C++
▪ An IDE (Integrated Development Environment) is used to edit AND compile the

code.

▪ Popular IDE's include Code::Blocks, Eclipse, and Visual Studio, Dev-c++. These

are all free, and they can be used to both edit and debug C++ code.

▪ You can find the latest version of Codeblocks at http://www.codeblocks.org/.

Download the mingw-setup.exe file, which will install the text editor with a

compiler.
6
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
The C++ Standard Library
▪ It is a rich collection of programs on classes, functions and template classes and template
functions which are helpful in program writing and in its execution.

▪ Every program requires applications of several functions of C++ Standard Library.

Standard C++ consists of three important parts:

1. The core language giving all the building blocks including variables, data types and
literals, etc.
2. The C++ Standard Library giving a rich set of functions manipulating files, strings, etc.

3. The Standard Template Library (STL) giving a rich set of methods manipulating data
structures, etc.
7
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
The C++ Standard Library
▪ These functions are included in the program by including the header files
which contain these functions (which help in the execution of the
programs)

▪ Every program has to include the header files which are required by the
program.

▪ For example, the header file <iostream> stands for input and output
stream used to take input with the help of “cin>>” function and display the
output using “cout<<” function.
8
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
The C++ Standard Library
What are C++ Header Files?
▪ These are those files that store predefined functions.

▪ It contains definitions of functions that you can include or import using a


preprocessor directive #include.

▪ This preprocessor directive tells the compiler that the header file needs
to be processed prior to the compilation.
▪ Basically, header files are of 2 types: Standard library header files and
User-defined header files
9
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
The C++ Standard Library
1. Standard library header files: These are the pre-existing header files
already available in the C/C++ compiler.
▪ C++ Header File
▪ #include<iostream>
▪ #include<stdio. h> (Standard input-output header) ...
▪ #include<string. h> (String header) ...
▪ #include<conio. h> (Console input-output header) ...
▪ #include<stdlib. h> (Standard library header) ...
▪ #include<math. h> (Math header ) ...
▪ #include<ctype. h>(Character type header) ...
▪ #include<time. h>(Time header) ...

2. User-defined header files: Header files starting #define can be


designed by the user.
10
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
1. /* The following is a C++ program which prints text message
saying “Helloworld!” */
2. #include <iostream.h> //this is input output header file
3. int main() //this function begins execution of a program
4. { // shows beginning of the main function
5. cout << "Hello World!\n"; //displays the message
6. return 0; // indicate program ended successfully
7. } // shows end of the main function
11
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure
▪Every C++ program has a number of components
▪ Comments

▪ Compiler directive

▪ main function

▪ Braces

▪ Statement
12
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure:
Comments
▪ Are remarks that are ignored by the complier .
▪ Use comments to:
▪ Explain the purpose of a program
▪ Keep notes regarding change to the source code
▪ Store the names of programmers for future reference
▪ Explain the parts of your program
▪ Two types:
1. Single-line comment
▪ Begin with //
▪ Example
▪ // This is a text-printing program.
2. Multi-line comment
▪ Start with /*
▪ End with */
▪ May appear anywhere in the program
13
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure:
Compiler directives
▪ Are instructions to the compiler rather than a C++ instructions that instruct the
C++ compiler to load a file from disk into the current program.
▪ It is a signal for pre-processor which runs before the compiler.
▪ Begin with #: is a pre-processor directive.
▪ Eg. #include <iostream.h>
▪ Tells preprocessor to include the input/output stream header file
<iostream.h>
▪ The name of a header file when included in a program is enclosed between a
pair of angular brackets < >
▪ Only one header file is written in one line.
14
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure:
The main function
▪ Is a function that runs first in C++ programs.
▪ Program execution starts by executing whatever instructions found in the main
function. “main” is function where execution starts.
▪ Body is delimited by braces ({}): opening and closing
▪ Is a function called by the operating system and returns a value to the
operating system. The int keyword allows the main function to return an
integer value. The return statement is what actually returns the value to the
operating system.
▪ Every C++ program must have only one main function .
▪ The parentheses “()” following the main tell the compiler that main is a
function.
15
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure:
Braces {}
▪ Are used to mark the beginning and the end of a block of code.
▪ Every opening brace must have a corresponding closing brace.
▪ The body of program enclosed in the curly braces{}.

▪ These braces generally contain expressions, statements and operators with a purpose to carry out some action on the data
presented to the program.
Statements
▪ Are instructions and commands that make the computer work.
▪ Each statement must end in semicolon (;) (The semicolon tells the compiler that the
statement is completed.)
return statement
▪ One of several means to exit a function
▪ When used at the end of main
▪ The value 0 indicates the program terminated successfully
▪ Example
▪ return 0;
16
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure:
▪ A typical program in C++ may comprise a list of statements involving variables,
constants, operators ,keyword, rules/special syntax and functions.

▪ Keywords have special meanings for the compiler and are used to control and execute
the program.

▪ C++ is case sensitive because it interprets uppercase and lowercase letters differently

▪ For example: Cow and cow are two different combination of letters.

▪ White space, Blank lines, space characters and tabs

▪ Are used to make programs easier to read and they are Ignored by the compiler

17
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++ program structure: First Program in C++: Printing a Line of Text
▪ Stream insertion operator <<
▪ Value to right (right operand) inserted into left operand
▪ Example
▪ cout << "Hello World!\n";
▪ Inserts the string " "Hello World!” " into the standard output
▪ Displays to the screen
▪ Escape characters
▪ A character preceded by "\"
▪ Indicates “special” character output
▪ Example
▪ "\n"
▪ Cursor moves to beginning of next line on the screen

▪ Any C++ program file should be saved with file name extension .cpp
18
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
C++Tokens
▪ The smallest individual units (the smallest building block of C++ programs that the
compile understans) in a program are known as tokens.
▪ Every word in a C++ source code can be considered a token.
▪ We have several types of tokens each of which serves a specific purpose in the syntax
and semantics of C++. Below are the main types of tokens in C++:
▪ Identifiers
▪ Keywords
▪ Constants
▪ Strings
▪ Special Symbols
▪ Operators 19
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
Keywords (reserved words)
▪ Words with special meaning to the compiler
▪ Have a predefined meaning that can’t be changed
▪ All reserved words are in lower-case letters
▪ Must not be and cannot be used for any other purposes

20
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
Identifiers
▪ Programmer given names
▪ Identify classes, variables, functions, etc.
▪ Consist of letters, digits, and the underscore character (_)
▪ Must begin with a letter or underscore but not with digits
▪ should Not be a reserved word
▪ Whitespaces and special characters such as @,#,!etc are not allowed.
▪ The identifiers in C++ can have identifiers with any length of characters.
▪ They are case sensitive, which means NUM1 and num1 are not the same
identifiers.
▪ A keyword cannot be used as an identifier, because there is a reserved
meaning for this word defined in the C++ library.
21
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.
Legal and Illegal Identifiers
▪ The following are legal identifiers in C++:
▪ payrate
▪ number1
▪ First_name
▪ _manUtd

22
Computer Programming Chapter Two: Basics of C++ Programming By: Kibru G.

You might also like