3. Introduction
Prof. K. Adisesha (Ph. D)
3
History of C++:
The C++ programming language was created by Bjarne Stroustrup and his team at
Bell Laboratories (AT&T, USA) to help implement simulation projects in an object-
oriented and efficient way.
➢ C++ is a superset of C because; any valid C program is valid C++ program too but not
the vice versa is not true.
➢ C++ can make use of existing C software libraries with major addition of “Class
Construct”.
➢ This language was called “C with classes” and later in 1983, it was named “C++” by
Rick Mascitii. As the name C++ implies, C++ was derived from the C programming
language: ++ is the increment operator in C.
4. Introduction
Prof. K. Adisesha (Ph. D)
4
OOPs characteristics
➢ Modularity: Module is a logically self-contained unit that can be tested and executed independently.
➢ Abstraction: It represents the essential features of an entity without including explanations or any
background details about it.
➢ Data Encapsulation: Wrapping of data and functions into a single unit is called data encapsulation.
➢ Inheritance: The process by which objects of one class acquires the properties of the objects of
another class.
➢ Polymorphism: The ability for a message to be processed in more than one form.
➢ Dynamic Binding: Linking of a procedure call to the code to be executed when it is called.
➢ Message Passing: Passing message objects and invoking the function by the object by sending a
message is known as message passing.
5. Introduction
Prof. K. Adisesha (Ph. D)
5
OOPs Benefits
➢ OOPs model the real world entity very well.
➢ Inheritance eliminates the redundancy (repetition) of code and hence supports code
reusability.
➢ Data hiding helps to build secured programs.
➢ Multiple instances (objects) can be created.
➢ Work can be divided easily.
➢ OOPs can be easily upgraded from small to large systems.
➢ Complexity can be easily managed.
➢ Message passing concept helps the objects to communicate and share data.
6. Introduction
Prof. K. Adisesha (Ph. D)
6
OOPs Applications
➢ Object oriented databases.
➢ Hypermedia, expert text and hypertext.
➢ Artificial intelligence and expert systems.
➢ Decision support systems and office automation systems.
➢ Parallel programming and neural networks.
➢ CAD, CAM, CIM systems.
➢ Simulation and modeling.
7. Introduction
Prof. K. Adisesha (Ph. D)
Why Use C++
➢ 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.
➢ C++ is portable and can be used to develop applications that can be adapted to multiple
platforms.
➢ C++ is fun and easy to learn!
➢ As C++ is close to C# and Java, it makes it easy for programmers to switch to C++ or vice
versa
7
8. Introduction
Prof. K. Adisesha (Ph. D)
8
Translating a C++ program
➢ Computers execute binary instructions.
➢ These binary instructions are known as machine instructions or machine code.
➢ The program creation process consists of the following steps:
Step 1 – Write the program in a computer language humans can read and understand (like C++),
Step 2 – Save the programs in text files as (.cpp) extension. Programs can be a few lines long and
reside in one file or can consist of many millions of lines of code and span thousands of files,
Step 3 – Run the source code files through a program called a compiler to generate object code for the
target computer,
Step 4 – Run the object files through a program called a linker to produce an executable image.
9. Introduction
Prof. K. Adisesha (Ph. D)
9
Translating a C++ program
➢ C++ Install IDE: download and Install Turbo C++ or Install Codeblocks
➢ The program execution process consists of the following steps:
10. Introduction
Prof. K. Adisesha (Ph. D)
10
General Structure of C++ Program.
Different programming languages have their own format of coding.
The basic components of a C++program are:
◼ Comments or Documentation Section
◼ Pre-processor Directives (Linker Section):
◼ Definition
◼ Global Declaration
◼ main ( ) function
◼ Declarations
◼ Statements
12. Characteristics of C++
Prof. K. Adisesha (Ph. D)
12
Characteristics of C++:
Object-Oriented Programming:
➢ It allows the programmer to design applications like a communication between object
rather than on a structured sequence of code. It allows a greater reusability of code in a
more logical and productive way.
Portability:
➢ We can compile the same C++ code in almost any type of computer & operating
system without making any changes.
Modular Programming:
➢ An application’s body in C++ can be made up of several source code files that are
compiled separately and then linked together saving time.
13. Characteristics of C++
Prof. K. Adisesha (Ph. D)
13
Characteristics of C++:
C Compatibility:
➢ Any code written in C can easily be included in a C++ program without making any
changes.
Speed:
➢ The resulting code from a C++ compilation is very efficient due to its duality as high-
level and low-level language.
Machine independent:
➢ It is a Machine Independent Language.
14. Characteristics of C++
Prof. K. Adisesha (Ph. D)
14
Characteristics of C++:
Flexibility:
➢ It is highly flexible language and versatility.
Wide range of library functions:
➢ It has huge library functions; it reduces the code development time and also reduces
cost of software development.
System Software Development:
➢ It can be used for developing System Software Viz., Operating system, Compilers,
Editors and Database.
15. C++ Character Set
Prof. K. Adisesha (Ph. D)
15
C++ Character Set:
Character Set means the valid set of characters that a language can recognizes.
➢ The character set of C++ includes the following:
16. C++ Tokens
Prof. K. Adisesha (Ph. D)
16
C++ Tokens:
The smallest individual unit in a program is known as token.
➢ These elements help us to construct statements, definitions, declarations, and so on,
which in turn helps us to construct complete program.
➢ Tokens used in C++ are:
❖ Identifier
❖ Reserved Keywords
❖ Constants or Literals
❖ Punctuators
❖ Operators
17. C++ Tokens
Prof. K. Adisesha (Ph. D)
17
C++ Identifier:
Identifiers is a name given to programming elements such as variables, functions,
arrays, objects, classes, etc.
➢ It contains letters, digits and underscore.
➢ C++ is a case sensitive; it treats uppercase and lowercase characters differently.
➢ The following are some valid identifiers:
▪ Adisesha
▪ time_table
▪ s2e2r3
▪ _dos _HJI3_JK
18. C++ Tokens
Prof. K. Adisesha (Ph. D)
18
C++ Identifier:
Rules to be followed while creating identifiers.
➢ Identifiers are a sequence of characters which should begin with the alphabet either
from A-Z (uppercase) or a-z (lowercase) or _ (underscore).
➢ C++ treats uppercase and lowercase characters differently. For example, DATA is not
same as data.
➢ No Special character is allowed except underscore “_”.
➢ Identifier should be single words i.e. blank spaces cannot be included in identifier.
➢ Reserved Keywords should not be used as identifiers.
➢ Identifiers should be of reasonable length.
19. C++ Tokens
Prof. K. Adisesha (Ph. D)
19
C++ Keywords:
Keyword is a predefined word that gives special meaning to the complier. The
programmer is not allowed to change its meaning.
➢ These are reserve for special purpose and must not be used as identifier name.
➢ Example:
cin, cout, for, if, else, this,
do, float, while, switch etc.,
20. C++ Tokens
Prof. K. Adisesha (Ph. D)
20
C++ Keywords:
There are keywords in C++ as mentioned below:
21. C++ Tokens
Prof. K. Adisesha (Ph. D)
21
Literals: A Literals/ constant are identifiers whose value does not change
during program execution.
➢ Constants are sometimes referred to as literal.
➢ A constant or literal my be any one of the following:
❖ Integer Constant
❖ Floating Constant
❖ Character Constant
❖ String Constant
22. Integer Constant:
➢ An integer constant is a whole number, which can be either positive or negative.
➢ They do not have fractional part or exponents.
➢ We can specify integer constants in:
❖ Decimal Integer Constant int a = 120; //Decimal Constant
❖ Octal Integer Constant int b = 0374; //Octal Constant
❖ Hexadecimal Integer Constant int c = -0XABF; //Hexadecimal Constant
❖ Unsigned Constant unsigned d = 328u; //Unsigned value
C++ Tokens
Prof. K. Adisesha (Ph. D)
22
23. C++ Tokens
Prof. K. Adisesha (Ph. D)
23
Floating Point Constant:
➢ Floating point constants are also called as “real constants”.
➢ These values contain decimal points (.) and can contain exponents.
➢ They are used to represent values that will have a fractional part and can be
represented in two forms (i.e. fractional form and exponent form)
➢ We can specify Floating Point constants in:
❖ float a=23.46 // equal to 23.46 x 100 = 23.46 x 1 = 23.46
❖ float b=26.126
24. Character constants: are specified as single character enclosed in pair of single
quotation marks.
❖ For example char ch = ‘P’;
➢ There are certain characters used in C++ which represents character constants called as
escape sequence which starts with a back slash ( ) followed by a character.
C++ Tokens
Prof. K. Adisesha (Ph. D)
24
25. C++ Tokens
Prof. K. Adisesha (Ph. D)
25
String Constants:
➢ A string constant consists of zero or more character enclosed by double quotation
marks (“ ”).
➢ Multiple character constants are called string constants and they are treated as an array
of char.
➢ By default compiler adds a special character called the “Null Character” (0) at the end
of the string to mark the end of the string.
➢ Example:
◼ char str[25] = “Hello Adisesha” ;
◼ This is actually represented as char str[25] = “Hello Adisesha0” in the memory
26. C++ Operators: Operators are used to perform operations on variables and values.
❖ Example: int x = 100 + 50;
➢ C++ Operators Types:
❖ C++ divides the operators into the following groups.
❖ Arithmetic operators
❖ Assignment operators
❖ Comparison operators
❖ Logical operators
❖ Bitwise operators
.
C++ Operators
Prof. K. Adisesha (Ph. D)
26
27. Arithmetic Operators
Prof. K. Adisesha (Ph. D)
27
➢ Arithmetic operators are used to perform common mathematical operations.
29. Relational Operator
Prof. K. Adisesha (Ph. D)
29
➢ Comparison operators are used to compare two values.
➢ The return value of a comparison is either true (1) or false (0).
30. Logical Operators
Prof. K. Adisesha (Ph. D)
30
➢ Logical operators are used to determine the logic between variables or values.
32. Classification of Operators
Prof. K. Adisesha (Ph. D)
32
➢ Operators may also be classified on the number of operands they act on either:
❖ Unary Operators
◼ Example: a++, a+1
❖ Binary Operators
◼ Example: x = x – 10;
❖ Ternary Operators
◼ Example: x = (a>b) ? a:b;
33. C++ Operators
Prof. K. Adisesha (Ph. D)
33
Special Operator :
➢ An expression is a combination of opcode and operand.
➢ Some special operators used in C++ programming are:
35. Structure of C++ Program
Prof. K. Adisesha (Ph. D)
35
General Structure of C++ Program.
Different programming languages have their own format of coding.
The basic components of a C++program are:
◼ Comments or Documentation Section
◼ Pre-processor Directives (Linker Section):
◼ Definition
◼ Global Declaration
◼ main ( ) function
◼ Declarations
◼ Statements
36. Structure of C++ Program
Prof. K. Adisesha (Ph. D)
36
General Structure of C++ Program
➢ Different programming languages have their own format of coding.
➢ The basic components of a C++program are:
37. C++ Comments
Prof. K. Adisesha (Ph. D)
37
C++ Comments
Comments can be used as non-executable statement to explain C++ code, and to make
it more readable.
➢ It can also be used to prevent execution when testing alternative code.
➢ Comments can be:
❖ singled-lined: Single-line comments start with two forward slashes (//).
Example:
❖ multi-lined.: Multi-line comments start with /* and ends with */.
Example:
38. C++ Linker Section
Prof. K. Adisesha (Ph. D)
38
Linker Section in C++
The linker is a program that makes executable files.
➢ The linker resolves linkage issues, such as the use of symbol hash (#) defined in one
translation unit.
➢ The C++ preprocessor directives programming skills that should be acquired:
❖ Able to understand and use #include.
❖ Able to understand and use #define.
❖ Able to understand and use macros and inline functions.
39. Library Functions in C++
Built-in functions are also known as library functions.
➢ We need not to declare and define these functions as they are already written in the
C++ libraries such as iostream, cmath etc.
C++ Library Functions
Prof. K. Adisesha (Ph. D)
39
40. Library Functions in C++
Built-in functions for String manipulation are:
C++ Library Functions
Prof. K. Adisesha (Ph. D)
40
42. C++ Main function
Prof. K. Adisesha (Ph. D)
42
Main function section in C++
The starting point of all C++ programs is the main function.
➢ This function is called by the operating system when your program is executed by the
computer.
int main( )
{
cout << "Hello World" << endl; //Function body section
return 0;
}
❖ { signifies the start of a block of code, and } signifies the end.
43. Data Types
Prof. K. Adisesha (Ph. D)
43
Data Types in C++:
Data Types can be defined as the set of values, which can be stored in a variable along
with the operations that can be performed on those values.
C++ defines several types of data and each type has unique characteristics.
C++ data types can be classified as:
❖ The fundamental data type(built-in data)
❖ Derived Data type
❖ User-defined data type
44. Data Types:
➢ C++ defines several types of data and each type has unique characteristics.
➢ C++ data types can be classified as:
Data Types
Prof. K. Adisesha (Ph. D)
44
45. Data Types
Prof. K. Adisesha (Ph. D)
45
Basic Data Types:char type
The char type: It is character data type to store any character from the basic character set.
➢ Characters are enclosed in single quotation marks (‘). ‘A’, ‘a’, ‘b’, ‘9’, ‘+’ etc. are
character constants.
❖ When a variable of type char is declared, the compiler converts the character to its equivalent
ASCII code.
❖ A character is allocated only 1 byte (8 bits) of memory space.
❖ The general form of a character declaration is: char variable_list;
➢ Example: char alpha=’a’;
46. Data Types
Prof. K. Adisesha (Ph. D)
46
Basic Data Types:int type
The int type: The int type is used to store integers. Integers are whole numbers without
any fractional parts.
➢ An integer is allocated 2 bytes (16 bits) of memory space.
➢ The operations include addition, subtraction, multiplication, division, remainder etc.
➢ The integer can be positive or negative values and the ranges of number we can store
are from - 32786 to 32767.
➢ This includes number such as:
❖ 45 and -9 are integers.
❖ 5.2 is not an integer because it contains a decimal point.
47. Data Types
Prof. K. Adisesha (Ph. D)
47
Basic Data Types:float type
The float type: This represents the number with fractional part i.e. real numbers.
➢ The float type is used to store real numbers.
➢ Float is allocated 4 bytes (32 bits) of memory space.
➢ Number such as 1.8, 4.5, 12e-5 and -9.66 are all floating-point numbers.
➢ The range of numbers we can store from -34e-38 to 3.4e38.
➢ The general form of a float declaration is: float variable_name;
➢ Example:
❖ float a=5.5;
48. Data Types
Prof. K. Adisesha (Ph. D)
48
Basic Data Types:double type
The double type: The double and float are very similar.
➢ The float type allows you to store single precision floating point numbers, while the
double keyword allows you to store double precision floating point numbers.
➢ Its size is typically 8 bytes of memory space. The range of numbers we can store are
from -1.7e308 to 1.7e308.
➢ The general form of a double declaration is: double variable_list;
➢ Example: double a = 5.5e-7; //a is equivalent to 5.5x10-7
❖ float a=5.5;
49. Data Types
Prof. K. Adisesha (Ph. D)
49
Basic Data Types:bool type
The bool type: The bool type has logical value true or false.
➢ The identifier true has the value 1, and the identifier false has the value 0.
➢ The general form of a bool declaration is: bool variable_name;
➢ Example:
❖ bool legal_age=true;
❖ The statement legal_age= (age>=21);
✓ assigns the value true if age is greater than or equal to 21 or else it returns the value
false
50. Data Types
Prof. K. Adisesha (Ph. D)
50
Basic Data Types:void type
The void type : The void data type has no values and no operations.
➢ In other words, both the set of values and set of operations are empty.
➢ In this declaration the main function does not return any value.
➢ Example:
❖ void main( )
{
Statements;
}
51. Basic Data Types:
➢ The data type specifies the size and type of information the variable will
store.
➢ Example:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = ‘A'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
Data Types
Prof. K. Adisesha (Ph. D)
51
52. Data Types
Prof. K. Adisesha (Ph. D)
52
Basic Data Types:
➢ The data type specifies the size and type of information the variable will
store.
53. Data Types
Prof. K. Adisesha (Ph. D)
53
Derived data types:
These data types are constructed using simple or fundamental data types.
➢ Derived data types emphasize on structuring of a group of homogeneous (same type)
data items.
➢ These data types includes:
❖ Arrays
❖ Functions
❖ Pointers
❖ References
54. Data Types
Prof. K. Adisesha (Ph. D)
54
User defined data types:
These data types are constructed by user using simple or fundamental data types.
➢ User defined data types emphasize on structuring of a group of homogeneous (same
type) or heterogeneous (different type) data items.
➢ Some user defined data types include
❖ Enumerated
❖ Structure
❖ Union
❖ Class
55. Data Types
Prof. K. Adisesha (Ph. D)
55
Enumerated data type:
Enumerated data type: An enumeration is a user defined type consisting of a set of named
constants called enumerators.
➢ enum is a keyword that assigns values 0, 1, 2…… automatically.
➢ This helps in providing an alternative means for creating symbolic constants.
➢ The syntax for enum is as follows:
enum [tag] { enum – list} ; //for definition for enumerated type
➢ Example : enum choice { bad, satisfactory, good, very_good};
choice mychoice;
56. Data Types
Prof. K. Adisesha (Ph. D)
56
C++ Modifier Types:
A modifier is used to alter the meaning of the base type so that it more precisely fits the
needs of various situations.
➢ C++ allows the char, int, and double data types to have modifiers preceding them.
➢ The data type modifiers are listed here −
❖ Signed Ex: unsigned x;
❖ Unsigned unsigned int y;
❖ long
❖ Short
57. Converting an expression of a given type into another type is known as typecasting or
type conversion.
➢ Type conversions are of two types, they are:
◼ Implicit Conversion: They are automatically performed when a value is copied to a
compatible type.
Example: short a = 2000;
int b;
b = a;
◼ Explicit Conversion: Many conversions, especially those that imply a different
interpretation of the value, require an explicit conversion.
Example: short a = 2000;
int b;
b = (int) a; //c-like cast notation
Type Conversion
Prof. K. Adisesha (Ph. D)
57
58. Input & Output Operators
Prof. K. Adisesha (Ph. D)
58
Input & Output Operators
➢ The input output operations are done using library functions cin and cout objects of
the class iostream.
➢ Using the standard input and output library, we will able to interact with the user by
printing message on the screen and getting the user’s input from the keyboard.
❖ A stream is an object where a program can either insert/extract characters to/from
it.
❖ The standard C++ library includes the header file iostream, where the standard
input and output stream objects are declared.
59. Input & Output Operators
Prof. K. Adisesha (Ph. D)
59
Input Operators:
➢ Input Operator “>>”: The standard input device is usually the keyboard.
➢ Input in C++ is done by using the “stream extraction” (>>) on the cin stream.
➢ “cin” stands for “console input”.
➢ Example: int age;
cin>>age;
60. Input & Output Operators
Prof. K. Adisesha (Ph. D)
60
Output Operator:
Output Operator “<<”: The standard output device is the screen (Monitor).
Outputting in C++ is done by using the object followed by the “stream insertion”
(<<).
“cout” stands for console output
Example: cout<<”sum”; //prints sum
cout<<sum; //prints the content of the variable sum;
61. Input & Output Operators
Prof. K. Adisesha (Ph. D)
61
Cascading of I/O Operators:
If a program requires more than one input variable then it is possible to input these
variables in a single cin statement using multiple stream extraction “>>” operators.
Example: cout<<”Enter the two number”;
cin>>a>>b;
If a program requires more than one output result then this can be done using a single
cout statement with multiple stream insertion “<<“ operators.
This is called cascading of input & output operators.
Example: cout<<”Entered the two number”<<a<<b<<endl;
cout<<”The sum of two number is”<<sum<<endl;
62. Manipulators
Prof. K. Adisesha (Ph. D)
62
Formatted Output (Manipulators): :
➢ Manipulators are the operators used with the insertion operator “<<“ to format the data
display.
➢ To use manipulator it is must to include header file <iomanip.h>
❖ endl
❖ setw()
➢ Example: endl manipulator: causes a line feed to be inserted. It has same effect as using new
line character “n”.
cout<<”Entered the two number”<<a<<b<<endl;
The setw( ) manipulator sets the width of the field assign for the output.
cout<<setw(6)<<”R” ; Output: _ _ _ _ _ R
63. Formatted Output (Manipulators):
➢ Manipulators are the operators used with the insertion operator “<<“ to format the data display.
➢ Program: To find the sum of two numbers:
#include<iostream.h>
#include<iomanip.h>
void main( )
{ int a, b, add;
clrscr( );
cout<<”Enter the two numbers”<<endl;
cin>>a>>b;
add = a + b;
cout<<”The sum of two number is”<<setw(6)<<sum<<endl;
getch(); }
Manipulators
Prof. K. Adisesha (Ph. D)
63