Академический Документы
Профессиональный Документы
Культура Документы
Each byte is divided into eight smaller storage locations known as bits.
In the binary numbering system (or binary) all numeric values are written as sequences of 0s
and 1s.
If the word length of the computer is 8 bit, the left most bit is reserved for the sign of the
number. A negative number is represented by a 1 in this bit and the remaining 7 bits are used
to store the binary equivalent of the number.
Any piece of data that is stored in a computers memory must be stored as a binary number.
Compiler
It converts a high level language (programming language) to a low level language (machine
language).
It converts source code to object code (machine code).
INTRODUCTION TO C++
#include <iostream>
using namespace std;
int main()
{
cout << Hello World;
return 0;
Pre-processor Directives
These are compiler pre-processor statements.
Preprocessor directives are lines included in a program with the character #
It gives instruction to the compiler to preprocess the information before actual compilation
starts.
The general syntax is: #include <header file> or #include "header file"
For example:
#include <iostream.h>
#include <conio.h>
main()
main function is where a program starts execution.
Main () program is the C++ program's main structure in which we process some statements.
Output Opreator
The statement
cout<< Hello World;
causes the string in quotation mark to be displayed on the screen.
cout is a predefined object that represents the standard output stream in C++.
The operator << is called insertion or put to operator.
Input Opreator
The statement
cin >> num1;
is an input statement and causes the program to wait for the user to type in a number.
cout is a predefined object that represents the standard input stream in C++.
The operator >> is called extraction or get from operator.
#include <iostream>
using namespace std;
int main()
{
float num1, num2, sum, avg;
sum = num1+num2;
avg = sum/2;
return 0;
}
Write a C++ Program to find area and perimeter of a circle.
#include <iostream>
using namespace std;
int main()
{
float radius, area, perimeter;
return 0;
#include <iostream>
using namespace std;
int main()
{
float length, breadth, area, perimeter;
return 0;
}
Structure of C++ Program
C++ program contain 4 sections. These section may be placed in separate code files and then
compiled independently or jointly. It is a common practice to organize a program into 3 files.
The class declarations are placed in a header file and the definitions of member functions go
into another files
Finally the main program that uses the class is placed in a third file which includes the
previous two files as well as any other files required.
This concept is based on the concept of client server model. The class definition including
member functions constitute the server that provides services to main program known as
client.
Tokens
The smallest individual unit in a program are known as tokens. C++ has the following tokens:
Keywords
Identifiers
Constants
Strings
Operators
Keywords
These are reserved words and cannot be used as names for the program variables or other user
defined program elements.
Identifiers
Example of variables
sum, Average, roll_no, year_99
Data Type
A data type determines the type and the operations that can be performed on the data.
C++ provides various data types and each data type is represented differently within the
computer's memory.
The various data types provided by C++ are shown in Figure
Built-In Datatypes (Primitive datatype)
The basic (fundamental) data types provided by c++ are integral, floating point and void data
type.
Among these data types, the integral and floating-point data types can be preceded by several
type modifiers.
These modifiers (also known as type qualifiers) are the keywords that alter either size or range
or both of the data types.
The various modifiers are short, long, signed and unsigned.
Int:
Numbers without the fractional part represent integer data.
In C++, the int data type is used to store integers such as 4, 42, 5233, -32, -745.
Thus, it cannot store numbers such as 4.28, -62.533.
Example int a;
Boolean :
The boo1ean data type can hold only Boolean values, that is; either true or false, where true
represents 1 and false represents 0.
It requires only one bit of storage.
The bool data type is most commonly used for expressing the results of logical operations
performed on the data.
Void : actually refers to an object that does not have a value of any type
Constants
Constant refers to fixed values that do not change during execution of a program. They
include characters, integers, floating point numbers and strings.
Literal constants do not have memory location.
Escape Sequence
Escape sequence or backslash ( \ ) character constants are a special category of constants.
These are used to represent certain non printing characters or symbols like , , ? , \ in a string
constant. Backslash ( \ ) causes escape from the normal way the characters are interpreted by the
compiler.
Example
\n - newline
\t - horizontal tab
\v - vertical tab
\b - backspace
\r - carriage return
\ - double quote
\ - single quote
\? - question mark
\\ - backslash
OPERATORS
An operator is a symbol which operates on a value or a variable. It tells compiler to perform
specific mathematical or logical manipulations on operands.
Data items on which operator acts upon are called operands.
For example: + is an operator to perform addition.
C++ programming has wide range of operators to perform various operations.
Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction and multiplication
on numerical values (constants and variables).
When the operator preceeds the operand (prefix), the increment or decrement operation is performed
before using the operands value in the expression
If the operator follows the operand (postfix), the value of operand will be used before incrementing or
decrementing it.
Example 1
int x=5;
y = ++x;
Here value of y = 6 and x=6. Since x is incremented first and that value is assigned to y.
Example 2
int x=5;
y = x++;
Here value of y = 5 and x=6. Since only after assigning the value of x to y, x will be
incremented
Precedence
It is the order of evaluation of different operators in a statement. Operations with higher
precedence are carried out before other operations having lower precedence. Usage of parentheses can
forcefully change the natural order of evaluation.
Associativity
The order in which consecutive operations within the same precedence group are carried out is
known as associativity.
Associativity of arithmetic operator is from left to right.
Relational Operators
A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false.
Logical operators are commonly used in decision making in C programming.
Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common assignment
operator is =
Bitwise Operators
During computation, mathematical operations like: addition, subtraction, addition and division
are converted to bit-level which makes processing faster and saves power.
Bitwise operators are used in C++ to perform bit-level operations.
Comma Operator
Comma operators are used to link related expressions together. For example:
Example
int a=8, b =3;
float x;
x = a / b;
Since a and b are integer, result of division of two integer number is also an integer, so x=2.0.
x = (float) a / b;
here it will convert variable to float type and perform floating point division instead of integer
division. So x=2.66
Conditional Operator ( ? : )
It is also known as ternary operator, ie it works on 3 operands.
Syntax
conditionalexpression ? expression1 : expression2;
Write a C++ Program to find largest of two numbers using conditional operator.
#include <iostream>
using namespace std;
int main()
{
int a, b, large;
large = a>b? a : b ;
return 0;
}
Write a C++ Program to find largest of three numbers using conditional operator.
#include <iostream>
using namespace std;
int main()
{
int a, b, c, large;
return 0;
}
Simple and Compound Statements
Most statements in a typical C++ program are Simple statements, such as assignments or
function calls.
Simple statement is a statement followed by a semicolon.
Eg of simple statement
a = b+c;
cout << Hi;
Compound statements or blocks are brace-enclosed sequences of statements
A compound statement is a grouping of statements in which each individual statement ends
with a semi-colon.
Declaration Statement
A simple declaration is a statement that introduces, creates, and optionally initializes one or
several identifiers, typically variables.
Syntax:
Datatype identifier;
Example:
int a;
char ch;
float avg;
#include <iostream>
using namespace std;
int main()
{
int a, b, temp;
temp=a;
a=b;
b=temp;
return 0;
}
Write a C++ Program to swap two numbers without temporary variable
#include <iostream>
using namespace std;
int main()
{
int a, b;
a=a + b;
b=a - b;
a=a - b;
return 0;
An enumerated type declares an optional type name and a set of zero or more identifiers that can be
used as values of the type. Each enumerator is a constant whose type is the enumeration.Creating an
enumeration requires the use of the keyword enum. The general form of an enumeration type is
enum enum-name ( enumeration-list } variable-list;
the enum-name is the enumeration's type name. The enumeration-listis comma separated.For
example, the following code defines an enumeration of colors called colors and the variable c
of type color. Finally, c is assigned the value "blue".
c = blue;
By default, the value of the first name is 0, the second name has the value 1, and the third has the
value 2, and so on. But you can give a name, a specific value by adding an initializer.
For example, in the following enumeration, green will have the value 5.
Here, red will have value 0, blue will have a value of 6 because each name will be one greater than
the one that precedes it.
#include <iostream>
using namespace std;
int main()
{
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
week today;
today = Wednesday;
cout<< "Day " << today;
return 0;
}
Here the output is Day 3. Because Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3,
Thursday = 4, Friday = 5, Saturday = 6 . Variable today is assigned value Wednesday.