0% found this document useful (0 votes)
54 views30 pages

Chapter Two: Basic Elements of C++

The document provides an overview of basic elements in C++ programming, including: - C++ allows high-level control over system resources and memory. It has an object-oriented structure and clear program organization. - A C++ program consists of comments, preprocessor directives, variable declarations, function prototypes and definitions. It requires writing, compiling, linking and running source code files. - Key elements include keywords, identifiers, literals, comments, variables, data types, and basic operators like assignment. Variables are declared with a type and assigned values, while basic operators perform math operations and assignment.

Uploaded by

Biniyam Hagos
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)
54 views30 pages

Chapter Two: Basic Elements of C++

The document provides an overview of basic elements in C++ programming, including: - C++ allows high-level control over system resources and memory. It has an object-oriented structure and clear program organization. - A C++ program consists of comments, preprocessor directives, variable declarations, function prototypes and definitions. It requires writing, compiling, linking and running source code files. - Key elements include keywords, identifiers, literals, comments, variables, data types, and basic operators like assignment. Variables are declared with a type and assigned values, while basic operators perform math operations and assignment.

Uploaded by

Biniyam Hagos
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/ 30

CHAPTER TWO Basic elements of c++

INTRODUCTION
 C++ is a cross-platform language that can be used to create high performance
applications.
 C++ was developed by Bjarne Stroustrup, as an extension to the C language.
 C++ gives programmers a high level of control over system resources and
memory.
 The language was updated 3 major times in 2011, 2014, and 2017 to C++11,
C++14, and C++17.
 C++ is one of the world's most popular programming languages.
 C++ can be found in today's operating systems, Graphical User Interfaces,
and embedded systems.
 C++ is an object-oriented programming language which gives a clear
structure to programs and allows code to be reused, lowering development
costs.
STRUCTURE OF C++ PROGRAM

A C++ program has the following structure


※ Comments
※ Preprocessor directives
※ Global variable declarations
※ Prototypes of functions
※ Definitions of functions
C++ IDE
 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 you need to use
 built-in text editor by compiler / Windows Notepad, the DOS Edit command/
 commercial text/ WordPerfect, Word, and dozens/
↘ 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 can.
 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 that were supplied with your compiler.
 All C++ compilers come with a library of useful functions (or procedures) and classes that
you can include in your program.
↘ A function is a block of code that performs a service
 such as adding two numbers or printing to the screen.
↘ A class is a collection of data and related functions.
SHOWING 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: }

 On line 1:
 the file iostream.h is included in the file
 The first character is the # 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.

 Include
√ is a preprocessor instruction that says, "What follows is a filename. Find that
file and read it in right here."
√ The angle brackets around the filename tell the preprocessor to look in all the
usual places for this file.
√ If your compiler is set up correctly, the angle brackets will cause the
preprocessor to look for the file iostream.h in the directory that holds all the
H files for your compiler.
√ The file iostream.h (Input-Output-Stream) is used by cout and cin, which
assists with writing to the screen and inserting user inputs respectively
 Line 3
∂ begins the actual program with a function named main().
∂ Every C++ program has a main() function.
∂ When your program starts, main() is called automatically
∂ 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 (}) on
lines 4 and 7.
 Everything between the opening and closing braces is considered as part of
functions.
 The object cout is used in C++ to print strings and values/messages to the
screen
 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 ("), as shown on line 5.
※ A text string is a series of printable characters
※ The main() function ends on line 7 with the closing brace
 The final two characters, \n, tell cout to put a new line after the words Hello World!
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++.
IDENTIFIERS
 An identifier is name associated with a function or data object and used to refer to that
function or data object
 An identifier must:
※ Start with a letter or underscore
※ Consist only of letters, the digits 0-9, or the underscore symbol _
※ Not be a reserved word
※ the use of two consecutive underscore symbols, _ _, is forbidden.
 Syntax of an identifier

 The following are valid identifiers

 The following are invalid:


 At this stage, it is worth noting that C++ is case-sensitive. That is lower-case letters are
treated as distinct from upper-case letters.
 the word NUM different from the word num or the word Num.
 Identifiers can be used to identify variable or constants or functions.
 Function identifier is an identifier that is used to name a function.
CON…
Literals
 Literals are constant values which can be a number, a character of a string.
Eg: 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.
#include<iostream.h>
int main()
{
//this is a comment
int n=10; //valid variable declaration
cout<<“my first variable declaration” <<endl;
cout<<n;
return 0;
}
DATA TYPES, VARIABLES,
AND CONSTANTS
 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
1. A type
 which is, established when the variable is defined (e.g., integer, float, character).
 Once defined, the type of a C++ variable cannot be changed.
2. A value
 which can be changed by assigning a new value to the variable.
 The kind of values a variable can assume depends on its type.
 For example, an integer variable can only take integer values
 e.g., 2, 100, -1 not real numbers like 0.123.

 Variable Declaration
 Declaring a variable means defining (creating) a variable.
 You create or define a variable by stating its type, followed by one or more
spaces, followed by the variable name and a semicolon
 Good variable names tell the function of the variable
 using good names makes it easier to understand the flow of your program.
 e.g, int myAge;

 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.
 int myAge, myWeight; // two int variables
 long area, width, length; // three longs

 IMPORTANT- Variables must be declared before used!



 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;
Or
int Width = 5;

Reading assignment
 Basic Data Types
 Signed and Unsigned
 Characters
OPERATORS
Assignment Operators
 The assignment operator is used for storing a value at some memory location
means variable.
 Its left operand should be an lvalue,
 denotes a memory location
 its right operand may be an arbitrary expression.

 Examples
int m, n, p;
m = n = p = 100; // means: n = (m = (p = 100));
m = (n = p = 100) + 2; // means: m = (n = (p = 100)) + 2;
ASSIGNMENT OPERATOR
EXAMPLE
Code Output
#include<iostream.h>
int main()
{
intn=10;
cout<<“n=“<<n; n=10
return 0;
}


 Arithmetic Operators
 C++ provides five basic arithmetic operators.

 arithmetic operations defined by C++ library

 Nb: if you want to use these you must put a #include statement at the start of your program
EXAMPLES OF ARITHMETIC
OPERATIONS DEFINED BY C++ LIBRARY

#include<iostream.h>
#nclude<math.h>//including c++ library
int main()
{
cosin of 10 is =-0.839072
float n=10;
float m=cos(n);
Cout<<“cosin of 10 is =“<<m;
return 0;
}

 Relational Operators
 Used for comparing numeric quantities.

 Logical Operators
 Used for combining logical expression

 Bitwise Operators

 Example

 Increment/decrement Operators
INCREMENT OPERATOR
EXAMPLE
#include<iostream.h> #include<iostream.h>
#include<iostream.h>
int main() int
int main()
main()
{ {{
int x=10, y=15; int
int x=10,
x=10, y=15;
y=15;
cout<<“pre_increment of x is=“<<++x; cout<<“pre_increment
cout<<“pre_increment of
of xx is=“<<++x;
is=“<<++x;
cout<<endl; cout<<endl;
cout<<endl;
cout<<“post_increment of y is=“<<y++; cout<<“post_increment
cout<<“post_increment of
of yy is=“<<y++;
is=“<<y++;
return 0; return
return 0;
0;
} }}
LAB EXERCISE
Write c++ programs for the following problems
1. displaying hello world
2. addition of numbers
3. area of a circle using user input data
4. value swapping between variable using third variable
5. that will calculate simple interest

You might also like