0% found this document useful (0 votes)
8 views31 pages

Chapter 2&3

Chapters 2 and 3 cover the basics of C++ programming, including the development cycle of writing, compiling, linking, and running a program. Key concepts such as variable declaration, data types, operators, and statements are explained, along with examples of a simple C++ program. The chapters also introduce reserved keywords, literals, comments, and the importance of proper variable naming and type conversion.

Uploaded by

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

Chapter 2&3

Chapters 2 and 3 cover the basics of C++ programming, including the development cycle of writing, compiling, linking, and running a program. Key concepts such as variable declaration, data types, operators, and statements are explained, along with examples of a simple C++ program. The chapters also introduce reserved keywords, literals, comments, and the importance of proper variable naming and type conversion.

Uploaded by

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

Chapter 2&3

C++ Basics
C++ IDE (integrated development environment)

• The complete development cycle in C++ is:


Write the program
compile the source code
 link the program and
run it.
Writing a Program
• To write a source code, your compiler may
have its own built-in text editor, or you may be
using a commercial text editor or word
processor that can produce text files.

• The files you create with your editor are called


source files, and for C++ they typically are
named with the extension .CPP.
Compiling

• Your source code file can't be executed, or run,


as a program.
• To turn your source code into a program, you use
a compiler.
• After your source code is compiled, an object file
is produced. This file is often named with the
extension .OBJ.
• This is still not an executable program, however.
To turn this into an executable program, you
must run your linker.
Linking
• C++ programs are typically created by linking
together one or more OBJ files with one or more
libraries.

• A library is a collection of linkable files .

• All C++ compilers come with a library of useful


functions (or procedures) and classes that you
can include in your program.
Sample program
• Any meaningful program written in C++ has to
contain a number of components:
• the main function; some variable declarations; and
some executable statements.
• For example, the following is a very basic C++
program:
1: #include <iostream.h>
2:
3: int main()
4: {
5: cout << "Hello World!\n";
6: return 0;
7: }

• # symbol, which is a signal to the preprocessor.
• Each time you start your compiler, the
preprocessor is run.
• The preprocessor reads through your source code,
looking for lines that begin with the pound symbol
(#), and acts on those lines before the compiler
runs.
• The angle brackets(<>) around the filename tell
the preprocessor to look in all the usual places for
this file.
• The file iostream.h (Input-Output-Stream) is used
by cout, which assists with writing to the screen.

• Every C++ program has a main() function.
• main(), like all functions, must state what kind of
value it will return.
• The return value type for main() in HELLO.CPP is
int, which means that this function will return an
integer value.
• All functions begin with an opening brace ({) and
end with a closing brace (}).
• Everything between the opening and closing
braces is considered a part of the function.

• The object cout is used to print a message to the
screen.
• cout is used: type the word cout, followed by
the output redirection operator (<<).
• Whatever follows the output redirection
operator is written to the screen.

• If you want a string of characters written, be


sure to enclose them in double quotes.

• programs declare main() to return an int.

• This value is "returned" to the operating system


when your program completes.

• The main() function ends on line 7 with the


closing brace.
Basic Elements
• Keywords (reserved words)
– Reserved/Key words have a unique meaning within a
C++ program.
– These symbols, the reserved words, must not be
used for any other purposes.
– All reserved words are in lower-case letters.
The following are some of the reserved words of C++.
asm auto Bool break case catch

const_cast class Const char continue default

dynamic_cast do Double delete else enum

Explicit extern False float for friend

Goto if Inline int long mutable

Namespace new Operator private protected public

reinterpret_cast register return short signed sizeof

static_cast static struct switch template this

Throw true try typedef typeid typename

Union unsigned using virtual void volatile

wchar_t
Literals

• Literals are constant values which can be a


number, a character of a string.

• For example the number 129.005, the character


‘A’ and the string “hello world” are all literals.

• There is no identifier that identifies them.


Comments

• A comment is a piece of descriptive text which


explains some aspect of a program.
• Program comments are totally ignored by the
compiler and are only intended for human
readers.
• C++ provides two types of comment delimiters:
– Anything after // (until the end of the line on which it
appears) is considered a comment.
– Anything enclosed by the pair /* and */ is considered
a comment.
Variables
• A variable is a symbolic name for a memory
location in which data can be stored and
subsequently recalled.
• Variables are used for holding data values so that
they can be utilized in various computations in a
program.
• All variables have two important attributes:
A type, which is, established when the variable is defined
(e.g., integer, float, character).
A value, which can be changed by assigning a new value to
the variable.
Variable Declaration

• Declaring a variable means defining (creating) a


variable.
– Stating its type,
– followed by one or more spaces,
– followed by the variable name and a semicolon.

• Good variable names tell you what the variables


are for;
• using good names makes it easier to understand
the flow of your program.
• Variables must be declared before used!

• C++ is case-sensitive.

• In other words, uppercase and lowercase


letters are considered to be different.

• A variable named age is different from Age,


which is different from AGE.
Creating More Than One Variable at a Time

• You can create more than one variable of the


same type in one statement by writing the type
and then the variable names, separated by
commas.
• For example:
– int myAge, myWeight; // two int variables
– long area, width, length; // three longs
Assigning Values to Your Variables

• You assign a value to a variable by using the


assignment operator (=).
• Thus, you would assign 5 to Width by writing
– int Width;
– Width = 5;
• You can combine these steps and initialize
Width when you define it by writing
– int Width = 5;
– int width = 5, length = 7;
Basic Data Types

• When you define a variable in C++, you must tell


the compiler what kind of variable it is: an
integer, a character, and so forth.
• This information tells the compiler how much
room to set aside and what kind of value you want
to store in your variable.
C++ data types and their ranges
Type Size Values

unsigned short int 2 bytes 0 to 65,535

short int(signed short int) 2 bytes -32,768 to 32,767

unsigned long int 4 bytes 0 to 4,294,967,295

long int(signed long int) 4 bytes -2,147,483,648 to 2,147,483,647

int 2 bytes -32,768 to 32,767

unsigned int 2 bytes 0 to 65,535

signed int 2 bytes -32,768 to 32,767

Char 1 byte 256 character values

Float 4 bytes 3.4e-38 to 3.4e38

Double 8 bytes 1.7e-308 to 1.7e308

long double 10 bytes 1.2e-4932 to 1.2e4932


Operators
• C++ provides operators for composing
arithmetic, relational, logical, bitwise, and
conditional expressions.
• It also provides operators which produce
useful side-effects, such as assignment,
increment, and decrement.
Assignment Operators

• The assignment operator is used for storing a


value at some memory location (typically
denoted by a variable).
• Its left operand should be value (standing for left
value), and its right operand may be an arbitrary
expression.
• The latter is evaluated and the outcome is stored
in the location denoted by the value.

Operator Example Equivalent To

= n = 25

+= n += 25 n = n + 25

-= n -= 25 n = n - 25

*= n *= 25 n = n * 25

/= n /= 25 n = n / 25

%= n %= 25 n = n % 25

&= n &= 0xF2F2 n = n & 0xF2F2

|= n |= 0xF2F2 n = n | 0xF2F2

^= n ^= 0xF2F2 n = n ^ 0xF2F2

<<= n <<= 4 n = n << 4

>>= n >>= 4 n = n >> 4


Arithmetic Operators

• C++ provides five basic arithmetic operators.


Operator Name Example

+ Addition 12 + 4.9 // gives 16.9

- Subtraction 3.98 - 4 // gives -0.02

* Multiplication 2 * 3.4 // gives 6.8

/ Division 9 / 2.0 // gives 4.5

% Remainder 13 % 3 //gives 1
// gives 1
Arithmetic operators.
Relational Operators

• C++ provides six relational operators for comparing numeric


quantities.
• These are summarized in table below.
• Relational operators evaluate to 1 (representing the true outcome) or
0 (representing the false outcome).
Operator Name Example
== Equality 5 == 5 // gives 1
!= Inequality 5 != 5 // gives 0
< Less Than 5 < 5.5 // gives 1
<= Less Than or Equal 5 <= 5 // gives 1
> Greater Than 5 > 5.5 // gives 0
>= Greater Than or Equal 6.3 >= 5 // gives 1
Relational operators
Logical Operators
• C++ provides three logical operators for
combining logical expression.

• These are summarized in the table below. Like


the relational operators, logical operators
evaluate to 1 or 0.
Operator Name Example

! Logical Negation !(5 == 5) // gives 0

&& Logical And 5 < 6 && 6 < =6 // gives 1

|| Logical Or 5 < 6 || 6 < 5 // gives 1

Logical operators
Increment/decrement Operators
• The auto increment (++) and auto decrement (--)
operators provide a convenient way of,
respectively, adding and subtracting 1 from a
numeric variable.
• The examples assume the following variable
definition:
• int k = 5;
Operator Name Example

++ Auto Increment (prefix) ++k + 10 // gives 16

++ Auto Increment (postfix) k++ + 10 // gives 15

-- Auto Decrement (prefix) --k + 10 // gives 14

-- Auto Decrement (postfix) k-- + 10 // gives 15

Increment and decrement operators


Simple Type Conversion

• A value in any of the built-in types we have see so


far can be converted (type-cast) to any of the other
types.
For example:
• (int) 3.14 // converts 3.14 to an int to give 3
• (long) 3.14 // converts 3.14 to a long to give 3L
• (double) 2 // converts 2 to a double to give 2.0
• (char) 122 // converts 122 to a char whose code is
122
• (unsigned short) 3.14 // gives 3 as an unsigned short
Statements

• Statements represent the lowest-level building blocks of a


program.
• Roughly speaking, each statement represents a
computational step which has a certain side-effect.

• Statements are useful because of the side-effects they


cause, the combination of which enables the program to
serve a specific purpose
• A running program spends all of its time executing
statements.
Assignment statement.

• Syntax:
• <Variable Identifier> = < expression>;
• Description:
• The <expression> is evaluated and the resulting
value is stored in the memory space reserved
for <variable identifier>.
• Eg: - int x,y ;
• x=5;
• y=x+3;
• x=y*y;

You might also like