Chapter 5
Review of C++
Introduction:
OOP characteristics:
- Abstraction
- Data encapsulation
- modularity
- Inheritance
- Polymorphism
- Dynamic binding
- Message passing
Chapter 5
Review of C++
OOP characteristics:
- Abstraction: abstraction represents the essential features
of an entity without including background details about it.
- Data encapsulation: the wrapping up of data and functions
into a single unit called as the class is known as data
encapsulation.
the process of insulating the data from direct access by
the program is called data hiding.
Chapter 5
Review of C++
OOP characteristics:
- Modularity: the lengthy program is divided into small
logical components, each component performs a specific
task. Each component is called module.
- Inheritance: the process in which object of one class
acquires the properties of objects of another class is
called inheritance.
Chapter 5
Review of C++
OOP characteristics:
- Polymorphism: it is an ability of the message to process in
more than one form. Poly means many morph means forms.
Example, function overloading and operator overloading.
- Dynamic binding: binding means linking of a function call to
the code to be executed when the function is called.
dynamic binding means the code associated with a function
call is not known until the time of the call at run-time.
Chapter 5
Review of C++
OOP characteristics:
- Message passing: the objects of a class communicate
with each other. For example, obj.getdata();
Chapter 5
Review of C++
Fundamentals of C++
C++ was developed by Bjarne stroustrup developed at AT&T
bell laboratories new jersey.
C++ character set:
Alphabets: a to z or A to Z
Digits: 0 to 9
Special characters: (,),*,&,!,?,#,@ etc.
Chapter 5
Review of C++
Tokens: a token is a smallest individual unit in a program or
lexical unit.
C++ has following tokens:
Identifiers
Keywords
Variables
Constants
Punctuators
Operators
Chapter 5
Review of C++
Identifiers: an identifier is a name given to the programming
elements such as variables, functions, arrays etc.
Example: _abc, my_name, employee123, etc.
Keywords: keywords are predefined words that has fixed
meaning to the compiler that cannot be changed by the
programmer.
Example: break, if, else, continue, case, for, while etc.
Chapter 5
Review of C++
Constants or literals: the quantity or value that does not
change during execution of the program is called constant.
Integer constant: numbers that doesn’t have fractional part
is called integer constants.
Integer constants may be decimal, octal, hexadecimal and
unsigned form.
Chapter 5
Review of C++
Decimal constants: numbers ranges from 0 to 9 are called
decimal constants. Example, +125, -450, 0 etc.
Octal constants: numbers ranges from 0 to 7 are called octal
constants. Example, +0725, -0235, 0412 etc.
Hexadecimal constants: numbers ranges from 0 to 9 and A
through F. are called hexadecimal constants. Example,
0xfffab, -0X456, etc.
Chapter 5
Review of C++
Unsigned constants: the numbers that are positive are called
unsigned constants. These numbers always suffix with u or
U. to specify the long type, the numbers suffix with ul or UL.
Example: 132u, 0x452785ul, 0472UL etc.
Chapter 5
Review of C++
Floating point constants: these are also called as real
numbers. The numbers that has decimal point are called
floating-point numbers.
- Fractional form
- Mantissa exponent notation
Example: 25.25, 78.10, -45.23 etc.
1.58e10, -2.3E-10 etc.
Chapter 5
Review of C++
Character constants: a single character enclosed within a
single pair of quotation marks is called character constant.
Each character represent ASCII code.
Example: ‘H’, ‘6’, ‘?’, etc.
Escape sequences: these are another set of character
constants. These are non-printable characters commonly
used with output statements to organize the output in a
proper order.
Example: \’, \0, \”, \a, \b, \t, \f etc.
Chapter 5
Review of C++
String constants: a string is a sequence of characters
enclosed within a pair of double quotation marks. Strings are
always terminated by the special character called NULL
character (‘\0’).
Example: “Programming”
Punctuators: these are the symbols that have syntactic and
semantic meaning to the compiler. But do not by themselves
specify an operation that yields a value.
Example: !, %, ^, &, *, (, ), ~
Chapter 5
Review of C++
Operators: an operator is a symbol that tells the compiler to
perform some specific mathematical or logical manipulation.
Operators are classified as,
- Unary operator
- Binary operator
- Ternary operator
Chapter 5
Review of C++
- Unary operator: operators operates only on one operand is called unary
operator.
Example, !, &, ~, *, ++, --, +, -
Binary operator: operators operates on two operands is called binary
operator.
Binary operators are classified as,
Arithmetic operators
Relational operators
Logical operators
Bitwise operators
Shorthand operators
Assignment operators
Special operators
Chapter 5
Review of C++
- Arithmetic operators: +, -, *, /, %
- Relational operators: ==, !=, <, <=, >, >=
- Logical operators: &&, ||, !
- Bitwise operators: &, |, ^, ~, <<, >>
- Shorthand operators: +=, -=, /=, %=, &=, |=, ^=
- Assignment operator:
Syntax:
variable=value or expression;
Example, n=5;
- Special operators: sizeof(), comma(,), dot(.), pointer(*)
Chapter 5
Review of C++
- Ternary operators: operators operate on three or more
operands. It is also called as conditional operator.
?:
Precedence of operators or hierarchy of operators
A=2+3*4
BODMAS rule
Type conversion: the process of converting one type of data into
another is called type conversion or type casting.
There are two types,
- Implicit type casting
- Explicit type casting
Chapter 5
Review of C++
- Structure of C++ program
Include files
class declarations
Member function declaration
main() function
Library function: these are built-in functions present in
specific header file
Chapter 5
Review of C++
- Character functions: ctype.h
isalpha(), isdigit(), isalnum(), isupper(), isspace(), islower(),
tolower(), toupper(), toascii(), ispunct().
String functions: string.h
strlen(), strcat(), strcmp(), strcmpi(), strcpy(), strrev(),
strlwr(), strupr()
Console I/O functions: getchar(), putchar(), gets(), puts().
Chapter 5
Review of C++
- Variables: the quantity that changes during the execution
of a program is called variable.
- Declaration: syntax: data_type variable_name;
Example, int n;
- Initializing a variable: syntax: data_type
variable_name=value;
Example, int n=10;
Chapter 5
Review of C++
Data types: it is a type of data that the variable can hold.
There are three categories of data types,
- Fundamental data types
int, float, char, double, void
- Derived data types
Arrays, functions, pointer and references
- User defined data types
enumerated, structure, union and class
Chapter 5
Review of C++
Input and output operators:
Input operator “>>”
Example,
int age;
cin>>age;
Output operator “<<“
Example,
cout<<“Let us learn C++”;
Chapter 5
Review of C++
Cascading of I/O operators:
C++ allows multiple input and output operators in the same instruction
is called input output cascading.
Example,
cout<<“Enter the value of a”;
cin>>a;
cout<<“enter the value of b”;
cin>>b;
Instead of writing multiple lines of cin and cout statements, we can
write it in a single line of instruction as,
cout<<“Enter the value of a and b”;
cin>>a>>b;
Chapter 5
Review of C++
Control statements: the statements that alter the flow of a sequence of
instructions.
Two types of control statements are,
- Selection statements
- simple if statement
- if-else statement
- if-else-if ladder statement
- nested if statement
- Iterative statements
- while loop
- do-while loop
- for loop
- Jump statements
- break, exit(), continue and goto.
Chapter 5
Review of C++
Arrays: array is a collection of homogeneous type of data
under the same name. each element can be accessed with
the help of its index number assigned as 0, 1, 2, …, n-1.
Types of arrays
- one-dimensional array
- Two-dimensional array
- Multi-dimensional array
Chapter 5
Review of C++
One-dimensional array: elements of the array can be
accessed with only one subscript is called one-dimensional
array.
Declaration:
Syntax:
data_type array_name[size];
Example, int a[10];
Initialization:
Example, int a[5]={10,20,30,40,50};
Chapter 5
Review of C++
Two-dimensional array: elements of the array can be accessed
with two subscripts is called two-dimensional array.
Declaration:
Syntax:
data_type array_name[row][column];
Example, int a[10][10];
Initialization:
Example, int a[2][2]={10,20,30,40};
Multi-dimensional array: these are array of n-dimensions. It is
also called as array of arrays.
Chapter 5
Review of C++
Functions: the complex programs are divided in to smaller
logical units, each unit consists of certain set of instructions
that performs a specific task called function. It is also called
routine or sub-program.
Types of functions:
- Built-in functions
- stdio.h, string.h, iomanip.h, iostream.h, math.h, conio.h etc.
- User-defined functions
Chapter 5
Review of C++
Inputting single character: get() function is used to input
single character.
Example,
char ch; OR char ch;
cin=get(ch); ch=cin.get();
Outputting single character: put() function is used to output
single character.
Example, cout.put(ch);
Chapter 5
Review of C++
String functions: a string is a sequence of characters
enclosed within a pair of double quotation marks. To use
string functions we have to include string.h header file.
Syntax:
char string_name[size];
Example, char str[20];
Initializing a string:
char str[20]=“India”;
Chapter 5
Review of C++
Inputting a string:
Syntax: cin.getline(string,size);
Example, cin.getline(str,20);
Outputting a string:
Syntax: cin.write(string.size);
Example, cin.write(str,20);
Chapter 5
Review of C++
strlen() function: this function is used to return length of the
string and terminated by a null (‘\0’) character.
Syntax: variable=strlen(string);
Example, int l=strlen(“Program”); returns 7
strcat() function: this function is used to combine two strings
together is called string concatenation.
strcpy() function: a string cannot be copied to another string by
using assignment operator. The function strcpy() function is used
to copy from one string to another.
Chapter 5
Review of C++
strcmp() function: this function is used to compare character
by character. This function is case sensitive. i.e., both
uppercase and lowercase letters are distinct.
strcmpi() function: this function is also used to compare
character by character. But this function is not case sensitive.
i.e. both uppercase and lowercase letters are same.
strrev() function: this function is used to reverse the string.
Chapter 5
Review of C++
User-defined functions: the functions defined by the user to solve
his/her problems.
Function definition or structure of user-defined function:
return_type function_name(argument_list_with_declaration)
{
Local variable declarations;
Executable statement-1;
Executable statement-2;
…
Executable statement-n;
return(expression);
Chapter 5
Review of C++
Calling a function: variable=function_name(argument_list);
OR
Variable=function_name();
Example, big=biggest(a,b,c);
main() function:
int main() void main()
{ {
Executable_statements; OR executable_statements;
return 0; }
}
Returning a value: return(expression) or return 0;
Chapter 5
Review of C++
Function prototypes:
Return_type function_name(type1 arg1, type2 arg2, …);
OR
Return_type function_name(type1, type2, …);
Example:
float cube(int a);
OR
float cube(int);
Chapter 5
Review of C++
Types of arguments:
- Actual arguments
In the function call big=biggest(a,b,c);
- Formal arguments
In the function header int biggest(int a, int b, int c);
Local variables:
Global variables:
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with no arguments and no return values.
- Function with arguments and no return values.
- Function with no arguments and with return values.
- Function with arguments and with return values.
- Recursive functions.
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with no arguments and no return values.
Example: void natural()
{
for(int i=0;i<n;i++)
cout<<i;
}
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with arguments and no return values.
Example: void average(int x, int y, int z)
{
float sum,average;
sum=a+b+c;
average=sum/3.0;
cout<<“Average=”<<average;
}
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with no arguments and with return values.
Example: int greatest()
{
if(a>b)
return(a);
else
return(b);
}
Chapter 5
Review of C++
Type of functions or categories of functions:
- Function with arguments and with return values.
Example: float interest(float p, float t,float r)
{
si=p*t*r/100.0;
return(si);
}
- Recursive functions: The function that calls by itself is called
recursive function.
Recursion function must have one or more terminating conditions
to terminate recursion. Otherwise, recursion will become infinite.
Chapter 5
Review of C++
Passing default arguments to functions:
Example: consider the prototype,
float interest(float p, int t, float r=0.10);
0.10 is a default value assigned to the argument rate
The function call statement must be,
si=interest(1000.0,2);
Passing constant arguments:
Example: int strlen(const char *p);
int total(const int x, const int y);
Chapter 5
Review of C++
Pass by value or call by value:
Pass by reference or call by reference:
Passing arrays to functions:
Passing structures to functions:
Chapter 5
Review of C++
Structures:
Defining a structure:
struct structure_name
{
Data_type1 member_name1;
Data_type2 member_name2;
…
Data_type-n member_name-n;
};
Chapter 5
Review of C++
Example:
struct employee
{
int empid;
char name[10];
char designation[10];
float salary;
};