Introduction to C++ Programming
Introduction to C++ Programming
Elementary Procedural
Advanced Procedural
Special Procedural
Fortran
Algol 68
C++
C#
Cobol
PL/I
Pascal
Ada 95
Java
Ada 83
What is Programming?
Take
A problem A set of data A set of functions
problem
What is C++?
C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for creating largescale applications. C++ is a multi paradigm language. C++ is an object driven language.
Introduction to C++
C++ (pronounced see plus plus) was developed by Bjarne Stroustrup at Bell Labs in 1979. Combination of Simula67 and c C With classes In 1983,the name was changed to C++. C++ is an extended version of C. C++ is a superset of C. All the concepts of C are applicable to C++ also.
C C Procedure Oriented C does not have any classes or objects. C input/output is based on library and the processes are carried out by including functions. C++ Object Oriented It supports Classes and Objects C++ i/o is made through console commands cin and cout.
Variables
Identifiers
Variables names are called identifiers Choosing variable names
Use meaningful names that represent data to be stored First character must be
a letter the underscore character
Keywords
Keywords (also called reserved words)
Are used by the C++ language Must be used as they are defined in the programming language Cannot be used as identifiers Keyword is an essential part of a language definition.
Keywords
Types of C Constants
C constants can be divided into two major categories
Primary Constants Secondary Constants
C Constants Secondary constants
Primary Constants
Integer Constants
An integer constant must have at least one digit It must not have a decimal point It could be either positive or negative If no sign precedes an integer constant, it is assumed to be positive ex., 123, -321, +78 No commas or blanks are allowed within an integer constant
Real Constants
Real constants (RC) must have at least one digit
It must have a decimal point
single quotes
The maximum length of a character constant can be 1 character
Eg a, 1, 5, = (Valid) asd, 12 (Invalid)
String Constants
It is a sequence of characters
Data Types
The most commonly used Data Types in C++ programming language:
short int int long int float double long double char bool
Declaring Variables
Before use, variables must be declared Declaration syntax:
<type_name> <Variable_1>, <Variable_2>, . . . ;
Declaring Variables
Examples: int number_of_students; double weight;
int is an abbreviation for integer. could store 3, 102, 3211, -456, etc. number_of_students is of type integer double represents numbers with a fractional component could store 1.34, 4.0, -345.6, etc. Weight is of type double
Comment
\n";
}
One way to exit the function or to return something
String to print
Editor
Disk
Phase 2
Preprocessor
Disk
Compiler
Phase 4
Linker
Disk
Loader
Primary memory Takes each instruction and executes it storing new data values
Phase 6
CPU
This line sends two items to the monitor The value of number_of_students The quoted string of characters " Students\n The \n causes a new line to be started following the s in Students A new insertion operator is used for each item of output
This code prompts the user to enter data then read one data items using cin The value read is stored in number_of_students
Comment
Comment should be enclosed between /* Single Line Comment is // It is used to increase the readability of the program. Any number of comments can be given at any place in the program. Comment cannot be nested. It can be split over more than one line */
Escape Sequence
\n \t \r \a \\ \ \? \ \0 new line tab carriage return alert backslash double quote Question Mark Quotation Mark Null
Operators in C++
An operator is a symbol (Eg:+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and variables.
Eg: a + b
Operators in C++
1. 2. 3. 4. 5. 6. 7. 8. Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators
Arithmetic Operator
Precedence order
Highest to lowest
()
*, /, %
+, -
Example
Algebra:
a(b+c)+ c(d+e)
C:
a * ( b + c ) + c * ( d + e ) ;
Precedence: 3 1 5 4 2
Example
/* Program using Arithmetic operator */ #include<iostream.h> #include<constream.h> void main() { int a=5,b=6; // Initializing variables int c; clrscr(); // To clear the screen c=a+b; // + operator cout<<The result is:<<c; c=b%a; // % operator cout<<The result is:<<c; }
Relational Operators
Operator < <= > >= Meaning Is less than Is less than or equal to Is greater than Is greater than or equal to
==
!=
Equal to
Not equal to
Logical Operators
Operator && || Meaning Logical AND Logical OR
Logical NOT
Logical expressions are a compound relational expressions -An expression that combines two or more relational expressions Eg: if (a==b && b==c)
Assignment operators
Syntax: v op = exp; Where v = variable, op = shorthand assignment operator exp = expression Ex:
x=x+3 x+=3
a =5 but b=6
i.e.: 1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left 2. a postfix operator first assigns the value to the variable on left and then increments the operand.
Conditional operators
Syntax:
Example
/* Program using Conditional operator */ #include<iostream.h> #include<constream.h> void main() { int a,b,c; // Declaring variables a=5; // Assigning values b=6; clrscr(); // To clear the screen c=(a>b)?a:b; // Conditional operator cout<<The result is:<<c; }
Bitwise operators
Operator & | ^ << >> Meaning Bitwise AND Bitwise OR Bitwise exclusive OR Shift left Shift right
Example
/* Program using bitwise operators */ #include<iostream.h> #include<constream.h>
void main() { clrscr(); int a=5,b=6; // a = 0101 , b = 0110 int c; c=a&b; cout<<"\n a & b : "<<c; // c = 0100 = 4
Continues
a | b : "<<c; // c = 0111 = 7
a ^ b : "<<c; // c = 0011 = 3