Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

1-Lecture 2 C++ programming Basics

The document provides an overview of C++ programming basics, including program structure, variable declaration, data types, and operators. It explains the roles of preprocessor directives, the main function, and various data types such as integers and floating points. Additionally, it covers operators, input/output functions, and includes examples and rules for variable naming and initialization.

Uploaded by

raahnim7
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

1-Lecture 2 C++ programming Basics

The document provides an overview of C++ programming basics, including program structure, variable declaration, data types, and operators. It explains the roles of preprocessor directives, the main function, and various data types such as integers and floating points. Additionally, it covers operators, input/output functions, and includes examples and rules for variable naming and initialization.

Uploaded by

raahnim7
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

Lecture # 04

C++ PROGRAMMING
BASICS

Instructor: Ms. Javeria Naz


Recommended Book
• Text Book
• Object Oriented Programming in C++, 4th edition by Robert Lafore

• Reference Book
• C++ programming from problem analysis to program design 5 th
edition by D.S Malik
Processing a C++ Program
• Source program
• A program written in a high-level language.
• preprocessor directives
• In a C++ program, statements that begin with the symbol # are called
preprocessor directives.
• Object program
• The machine language version of the high-level language program.

• Linker
• A program that combines the object program with other programs

• Loader
• A program that loads an executable program into main memory.
Linker
Processing a C++ Program
Writing C++ program
• C++ program is stored on disk with extension .cpp
• Code is written in the editor which is then compiled

Linked to
compile library
Source Machine code/ Executable code
program object code

First.cpp First.obj First.exe


C++ Program Structure
A C++ program consists of three parts:
Preprocessor Directives:
•The word directive means instruction or command or order.
•The instructions that are given to the C++ compiler to process the
information before starting the actual compilation process are called
preprocessor directives.
Main() function
•The main() function indicate the beginning of the C++ program.
•The main() function must be included in every C++ program.
•When execution start control goes directly to main() function.

C++ statements
•It is fundamental unit, it is instruction for the computer to do
something.
Example
Preprocessor
directive

#include<iostream>
using namespace std;
Main function
void main()
{
C++
cout<<“my first C++ program”<<endl; statements
}
Declaration of a variable
Variables
• Quantity whose value changes during the execution of program
• Variable name represents memory location in computer.
• Data is stored in the memory location
• Consist of alphabets and digits

Data types in C++


• Each variable is declared by its type.
• C++ has five basic data types.
• There are two categories of data types.
1)Standard data types
2)User defined data types
Integer Data types in C++
• The ‘int’ stands for integer.
• An integer is a whole number without decimal part.
• It can be positive or negative.

Types Storage Capacity Data Storage Range


int 2-Bytes -32768 to 32767
short int 2-Bytes -32768 to 32767
unsigned int 2-Bytes 0 to 65535
long 4-Bytes -2,147,483,648 to 2,147,483,647
unsigned long 4-Bytes 0 to 4,294,967,295
Real Data types in C++
• The numeric value with decimal point is called Real
Data Type/Floating Point Data Type.
• The number of digits after decimal point called
“Precision”.
• Mantissa: It is a value of number.
• Exponent: It is value of exponential power.
Real Data types in C++
• The numeric value with decimal point is called Real
Data Type/Floating Point Data Type.
• The number of digits after decimal point called
“Precision”.
• Mantissa: It is a value of number.
• Exponent: It is value of exponential power.
Data types(cont.)
Types of variable
• int: These type of of variables holds integer value.
int num = 100;//Size 2 bytes
• char: holds character value like ‘c’, ‘F’, ‘B’, ‘p’, ‘q’ etc.
char ch = 'A';//Size 1 byte.
• bool: holds Boolean value true or false.
bool b = true;
• double: double-precision floating point value.
double num = 10098.98899;//Size 8 bytes.
• float: Single-precision floating point value.
float num = 123.78987;//Size 4 bytes.
Declaration of a variable
• Assigning name and data type that variable can hold
is called variable declaration.

• Syntax
Type list of variables;

• Examples:
int x;
int a, xyz, k_w;
float b, ab;
char n;
Declaration of a variable
Variables Naming rules
1. Only Alphabets, Digits and Underscores are permitted.
2. Identifier name cannot start with a digit.
3. Keywords cannot be used as a name.
4. Upper case and lower case letters are distinct.
5. Special Characters are not allowed.
6. Blank spaces are not allowed.
7. Variable names can not start from a number.
example:
int 2sum;  invalid because variable name is start with a
number
Declaration of a variable
Valid Variables Names
Identifier Note
Name Capital Letter and Small Letters are Allowed
name Small Letters are allowed
Digits and Underscore is allowed along with
name_1
alphabets
Keywords are allowed but we have to change case of
Int
any letter or complete word
Keywords are allowed but we have to change case of
INT
any letter or complete word
Underscore at the first position is allowed in C++
_SUM
language
sum_of_numbers We can concatenate multiple words with underscore
Best Style to concatenate multiple words (Changing
firstName
case of First Letter of Successive Word)
Declaration of a variable
Invalid Variables Names

Identifier Explanation

int Keyword name cannot be given to Variable/Identifier

$ sign can be used in other programming language for


$sum
creating identifier, however C/C++ do not support ‘$’ sign.

num^2 special characters are not allowed.


Spaces are not allowed in C++ programming language for
num 1
declaring identifier.
2num Digits are allowed but not as first Character
Declaration of a variable
• Token
Tokens are nothing but small parts of program.
Example: Variable, constant, function, name, keyword etc.
E.g. main , int a ; ( ){ }

• Keywords/reserved words
Keywords are one type of token, That is fixed or pre define in
programming language.
E.g. main, include, int
Initialization of a variable
• When a variable is declared, memory is assigned to it.
• Value in that memory location is also assigned to the
variable.
• Assigning value to a variable at the time of declaration is
called initialization of the variable.

• Example
int ab=0;
float c=0.5;
int a=2 , b= 6 , c=10;
Example
#include<iostream>
#include<conio>
using namespace std;
void main()
{
int a=2,b=3; Variable
initialization
float x=3.2;
char name[15] = “Sara khan”;

cout<<name<<endl; Print values on


cout<<a<<endl; output
cout<<b<<endl;
cout<<x<<endl;
getch();
}
Constants
• A quantity that can not change its value during program
execution is called a constant.
• There four type of constants in c++

Int constants cout<< 56;


cout<<10;
Floating point cout<<2.6;
constants
‘a’ , ‘/’, ‘+’
Character constants
“ahmad ali” ,
String constants “pakistan”
Cont . . .
• Constant qualifier
• Item that follow a keyword const
• E.g const float pi = 3.14;
• Define directive
• Is a preprocessor directive, used to define constant
quantity.
• E.g
#define pi 3.14
Macros -> #define
#include <iostream>
using namespace std;
#define pi 3.14159
int main ()
{
cout << "Value of PI :" << pi << endl;
return 0;
}
Operators in C++
Assignment Operators
• Assignments operators in C++ are: =, +=, -=, *=, /=, %=
• num2+=num1 is equal to num2 = num2+num1
• num2-=num1 is equal to num2 = num2-num1
• num2*=num1 is equal to num2 = num2*num1
• num2/=num1 is equal to num2 = num2/num1
• num2%=num1 is equal to num2 = num2%num1
Bitwise
1. Bitwise
used to change individual bits into a number
work with only integral data type like char, int and long
don't work with floating points
• Bitwise AND operators &
• Bitwise OR operator |
• And bitwise XOR operator ^
• And, bitwise NOT operator ~
• Short notations:& = , |= , ^= , ~=
Shift operators
• shift Bits of any variable
• Left Shift Operator <<
• Right Shift Operator >>
• Unsigned Right Shift Operator >>>
Unary Operators
• operators work on only one operand
• many unary operators
• increment ++ and decrement -- operators are most use
Ternary operator and c

• ternary if-else ? : is an operator which has three


operands.
Syntax:
(Condition)? True case code : False case code;

Example:
int a = 10;
a > 5 ? cout << "true" : cout << "false“
Comma operators
• used to separate variable names and to separate
expressions
• In case of expressions, the value of last expression is
produced and used.
• int a,b,c; // variables declaration using comma operator
a=b++, c++; // a = c++ will be done.
Relational operators
• six relational operators in C++: ==, !=, >, <, >=, <=
• == returns true if both the left side and right side are equal
• != returns true if left side is not equal to the right side of
operator.
• > returns true if left side is greater than right.
• < returns true if left side is less than right side.
• >= returns true if left side is greater than or equal to right
side.
• <= returns true if left side is less than or equal to right
side.
Logical operators
• Logical Operators are used with binary variables
• mainly used in conditional statements and loops for
evaluating a condition.
• Logical operators in C++ are: &&, ||, !
• b1&&b2 will return true if both b1 and b2 are true else it
would return false.
• b1||b2 will return false if both b1 and b2 are false else it
would return true.
• !b1 would return the opposite of b1, that means it would
be true if b1 is false and it would return false if b1 is true.
Arithmetic operators
• Operators are the symbols that represent arithmetic
operations.
• Following arithmetic operators are used in c++

operat Meaning
or
+ Addition
- Subtraction
* Multiplication
/ Division
% For remainder
Arithmetic expression
• Combination of variables, constants and arithmetic
operators.
• Assignment operator = is used to assign the calculated
value of the expression to the receiving variable

• Example
int a = 2, b = 3, ans;
ans = a * b + 10;
Basic input/output
• cout stands for console output, it is used to display the
output on computer screen.
• e.g. cout<<“hello”<<endl;

Put to operator Manipulator used with


(<<) put to operator

• cin is console input, it is used to get input from keyboard


during program execution.
• e.g cin >> a;

Get from
operator (>>)
The “endl” manipulator
• The “endl” stands for end of line. It has same effect as the
escape “\n”.
• Is predefined in <iostream.h> header file.
• Example
void main()
{
cout<<“I am a “<<endl<<“Pakistani”;
}
Escape sequence
• Special non-printing character used to control printing on
the output device.
• They are written in single and double quotes.
• It is a combination of back slash \ and the code character.
Common escape Function
sequences
\ a Beep(alarm)
\ b Backspace
\ n New line
\ t Tab
\\ Backslash
\’ Single
quotation
\” Double
quotation
Compound assignment statement
• The assignment statement used to assign one value to
many variables
• E.g x = y = 10;

• Compound assignment expression


Is used to + , -, * , / a value without having to write the
variable on either side of the assignment operator.
Syntax:
Variable name operator = expression;
Example:
x = x+ 10 x + = 10
x = x* 3 x*=3
Increment and decrement operator
• Operator that is used to add 1 to the value of a variable is
called increment operator.
• It is represented by (++) sign
• x = x+1 is same as x++

• Prefix increment operator


• Adds 1 to the value of variable before it is used in the
expression. ++x

• Postfix increment operator


• Adds 1 to the value of variable after it is used in the
expression. x++
Increment and decrement operator
• Operator that is used to subtract 1 from the value of a
variable is called decrement operator.
• It is represented by (--) sign
• x--

• Prefix decrement operator


• subtract 1 from the value of variable before it is used in
the expression. --x

• Postfix decrement operator


• subtract 1 from the value of variable after it is used in the
expression. x--
Example
void main()
{
int c = 10;
cout<< “value of c = “<< c <<endl;
cout<< “value of c = “<< ++c <<endl;
cout<< “value of c = “<< --c <<endl;
cout<< “value of c = “<< c <<endl;
cout<< “value of c = “<< c++ <<endl;
cout<< “value of c = “<< c-- <<endl;
cout<< “value of c = “<< c <<endl;
}
Comments in C++
• Are non-executable statements, used to add remarks in a
program.
• // is used to mark single line comments
• E.g.
// this is an example of comment
// c++ program
sum = a+ b; // add two variable
sum ++ ; // add 1 to the value of sum
• /* and */ is used for multiple line comments
• E.g
/* s = a +b;
x ++; * /
cout<<“Sum is “<<a + b<<endl;
Q/A
Quiz-1
1. Visually show how does a c++program works. (5)
2. What is variable declaration explain with example. Write
its rules. (5)
3. Differentiate between prefix and postfix increment
operator. (5)
4. What is ternary operator. Write its syntax. (2)
5. Write a program that Print the following output: (3)

***Learning C++***
***QUIZ-1***

You might also like