SlideShare a Scribd company logo
1 of 82
OBJECT ORIENTED PROGRAMMING
IN C++
Module II
Procedure-Oriented
Programming
 Procedure-Oriented Programming using high
level languages like COBOL, FORTRAN, C,
etc.
 The primary focus is on functions.
 Procedure-oriented programming basically
consists of writing a list of instructions for the
computer to follow and organizing these
instructions into groups known as functions.
Typical structure of procedure-oriented
program
Main Program
Function-1 Function-2 Function-3
Function-4 Function-5
Function-6 Function-7 Function-8
Procedure-Oriented Programming
 Unrestricted Access (for Global variables)
 In a large program,it is very difficult to identify what data is
used by which function. In case we need to revise an
external data structure, we also need to revise all functions
that access the data.This provides an opportunity for bugs
to creep in.
 This approach does not model real world problems.
 This is because functions are action-oriented and do not
really corresponding to the element of the problem.
Drawbacks :-
Relationship of data and functions in
procedural programming
Function-1 Function-2 Function-3
Local Data Local Data Local Data
Global Data Global Data
Object-Oriented Programming
 OOP treat data as a critical element in the
program development and does not allow it
to flow freely around the system.
 It ties data more closely to the functions that
operate on it, and protects it from accidental
modification from outside functions.
 OOP allows decomposition of a problem into
a number of entities called objects and then
build data functions around these objects.
Organization of data and functions in OOP
Data
Functions
Object A
Data
Functions
Object B
Data
Functions
Object C
Communication
Characteristics of Object-
Oriented Programming
 Emphasis is on data rather than procedure.
 Programs are divided into objects.
 Data structures are designed such that they
characterize the objects.
 Functions that operate on the data of an
object are tied together in the data structure.
 Data is hidden and can not be accessed by
external functions.
An object is like a
black box.
The internal
details are
hidden.
 Identifying objects and
assigning responsibilities to
these objects.
 Objects communicate to
other objects by sending
messages.
 Messages are received by the
methods of an object
9
What is Object Oriented
Programming?
Basic Concepts of Object-
Oriented Programming
1. Objects
2. Classes
3. Data Abstraction
4. Encapsulation
5. Inheritance
6. Polymorphism
7. Dynamic Binding
8. Message Passing
 TangibleThings as a car, printer, ...
 Roles as employee, boss, ...
 Incidents as flight, overflow, ...
 Interactions as contract, sale, ...
 Specifications as colour, shape, …
11
What is an object?
 an object represents an individual, identifiable
item, unit, or entity, either real or abstract, with
a well-defined role in the problem domain.
Or
 An "object" is anything to which a concept
applies.
Etc.
12
So, what are objects?
 Modularity - large software projects can be
split up in smaller pieces.
 Reusability - Programs can be assembled
from pre-written software components.
 Extensibility - New software components
can be written or developed from existing
ones.
13
Why do we care about objects?
 The most important aspect of an object is its
behaviour (the things it can do).
 A behaviour is initiated by sending a message
to the object (usually by calling a method).
14
Basic Terminology:
Behaviour and Messages
Basic Concepts of OOP
 Objects
Objects are the basic run-time entities in an
object-oriented system.They may represent
a person, a place, a bank account, etc.
continue …
Basic Concepts of OOP
 Objects Object : CUSTOMER
DATA
AC No.
Name of AC Holder
Address
FUNCTIONS
Deposit
Withdrawal
AC Balance Display
continue …
Basic Concepts of OOP
Class
Classes are user-defined data types.
A class is a collection of objects of
similar type.
 The entire set of data and code of an object can
be made a user-defined data type with the help
of a class.
 Objects are variables of the type class.
 Once a class has been defined, we can create any
number of objects belonging to that class.
 Each object is associated with the data of type
class with which they are created.
continue …
Basic Concepts of OOP
Class ( Syntax )
class class_name
{
variable declaration;
…………………………
…………………………
function declarations;
…………………………
…………………………
};
Class ( Example )
class student
{
int rollnumber;
char name [20];
float marks;
void getdata();
void putdata();
};
Basic Concepts of OOP
 Class ( Example)
If fruit has been defined as a class, then the statement
fruit mango ;
will create an object mango belonging to the class fruit.
syntax :-
class_name object-name ;
continue …
 Making Classes: Creating, extending or
reusing abstract data types.
 Making Objects interact: Creating objects
from abstract data types and defining their
relationships.
20
The two steps of Object
Oriented Programming
Basic Concepts of OOP
 Data Abstraction and Encapsulation
o The wrapping up of data and functions into a single unit is
known as encapsulation.
o The data is not accessible to the outside world, and only
those functions which are wrapped in the class can access
it.
o Abstraction is the representation of the essential features
of an object.These are ‘encapsulated’ into an abstract
data type.
o These functions provide the interface between the
object’s data and the program.This insulation of the data
from direct access by the program is called data hiding or
information hiding.
continue …
Basic Concepts of OOP
 Data Abstraction and Encapsulation
The attributes wrapped in the classes are called data
members and the functions that operate on these data
are called methods or member functions.
Since the classes use the concept of data abstraction, they are
known as Abstract DataTypes (ADT).
continue …
Basic Concepts of OOP
 Inheritance
o Inheritance is the process by which objects of one
class acquire the properties of objects of another
class.
o Each derived class shares common characteristics
with the class from which it is derived.
continue …
Property Inheritance
Bird
Attributes:
Feathers
Lay eggs
Flying Bird
Attributes:
------------
------------
Non-flying Bird
Attributes:
------------
------------
Penguin
Attributes:
------------
------------
Kiwi
Attributes:
------------
------------
Robin
Attributes:
------------
------------
Swallow
Attributes:
------------
------------
Basic Concepts of OOP
 Inheritance
o Inheritance provides the idea of reusability.
o We can add additional features to an existing class
without modifying it.
(By deriving new class from existing one.The new class
will have the combined features of both the classes.)
continue …
Basic Concepts of OOP
 Polymorphism –
ability to take more than one form
o An operation may exhibit different behaviors in
different instances.
o The behavior depends upon the types of data used
in the operation.
continue …
Basic Concepts of OOP
 Polymorphism - ability to take more than one form
o The process of making an operator to exhibit
different behaviors in different instances is known
as operator overloading.
o << Insertion Operator
o << Left-shift bit-wise operator
o Using a single function name to perform different
types of tasks is known as function overloading.
o add( 3, 5) gives 8
o Add(“hello”, “-world”) gives “hello-world”
continue …
Basic Concepts of OOP
 Dynamic Binding
 Dynamic binding ( late binding ) means that the
code associated with a given procedure call is not
known until the time of the call at run-time.
 It is associated with polymorphism and inheritance.
continue …
Benefits of OOP
1. Inheritance – eliminate redundant
code and extend the use of existing
classes.
2. We can build programs from the
standard working module, no need
of starting from the scratch.
3. Data hiding helps the programmer to build
secure programs that can not be invaded by
code in other parts of the program.
Benefits of OOP
4. Multiple instances of an objects can co-exists
with out any interference.
5. It is easy to partition the work in a project
based on objects.
6. Object-oriented system can be easily
upgraded from small to large systems.
7. Message passing techniques for
communication between objects makes the
interface descriptions with external systems
much simpler.
8. Software complexity can be easily managed.
Application of OOP
 Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
What is C++
 C++ is an object-oriented programming language. C++ is
the updated version of C.
 Developed by Bjarne Stroustrup at AT&T Bell
Laboratories, USA in the early 1980’s.
 The first commercial implementation of C++ was released
on 14 October 1985
 C++ is sometimes called a hybrid language because it is
possible to write object oriented or procedural code in the
same program in C++.
Structure of a C++ program

Include Files
Class declaration
Member function definitions
Main function program
Simple Structure of a C++ Program
Header files
…………………..
…………………..
main function ( )
{
variable declaration
// Member function //
statements
……………….
………………..
}
// Member function execution
{ } //
#include<iostream.h>
#include<conio.h>
void main ( )
{
variable declaration
statements
……………….
………………..
}
The main( ) Function
 The main( ) returns a value of type int to the
operating system by default.
 The functions that have a return value should
use the return statement for termination.
 Use void main( ), if the function is not
returning any value.
Main Function
int main ()
• This line corresponds to the beginning of the definition of
the main function.
• The main function is the point by where all C++ programs
start their execution, independently of its location within the
source code.
• It does not matter whether there are other functions with
other names defined before or after it – the instructions
contained within this function's definition will always be the
first ones to be executed in any C++ program. For that same
reason, it is essential that all C++ programs have a main
function.
• The word main is followed in the code by a pair of
parenthesis (()).That is because it is a function declaration:
• Right after these parentheses we can find the body of the
main function enclosed in braces ({}). What is contained
within these braces is what the function does when it is
executed.
Output Operator
cout << "Hi, this is my program!";
cout is a predefined object that represents the
standard output operator in C++
screen
cout << C++ variable
Object Insertion Operator
Output Operator
 The operator << is called insertion or put to
operator.
 It inserts or sends the contents of the variable on its
right to the object on its left
cout << "Hi, this is my program!";
cout << number ;
screen
cout << C++ variable
Object Insertion Operator
Input Operator
cin >> number;
cin is a predefined object that corresponds to the
standard input operator in C++, here the standard
input stream is keyboard.
cin >> 50.55
Object Extraction Operator C++ variable
Keyboard
Input Operator
 The operator >> is known as extraction or get from
operator.
 It extracts ( or takes ) the value from the keyboard
and assigns it to the variable on its right.
cin >> 50.55
Object Extraction Operator C++ variable
Keyboard
Cascading of I/O Operators
 Multiple use of << or >> in one statement is
called cascading.
eg:-
cout << “Sum = ” << sum << “n” ;
cin >> num1 >> num2 ;
Comments in C++
 supports single line and multi-line comments.
 All characters available inside any comment are
ignored by C++ compiler.
 Block comments start with /* and end with */
 Line comments start with //,extending to the end of
the line
eg:-
//This is a single line comment
/* C++ comments can also
* span multiple lines
*/
Header Files
 a header file sometimes known as an include
file.
 Header files almost always have a .h
extension.
 The purpose of a header file is to hold
declarations for other files or functions to
use.
 Examples :- iostream.h, conio.h , string.h,
stdio.h, etc….
 iostream. h - input output stream. iostream.h is
used for input output functions . ( cout , cin)
 conio. h - concatenated input output . conio.h includes
the declarations of getch, getc, putc, clrscr, etc.
Commonly used Header Files and its purpose
 string. h - used for string handling functions
such as strcpy, strlen strrev, strcpy etc….
Tokens
The smallest individual units in a program are
known as tokens.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Operators and special characters
Keywords
1. Keywords
• Keywords are the reserved words used in C++.
• We cannot use the keywords as variables.
Eg :- public, while, void, friend, if, int …etc.
2. Identifiers (variables)
• A variable is used to store values in the program.
• A valid identifier is a sequence of one or more
letters, digits or underscore characters (_).
Eg :- int A, B, C ;
Identifiers
Rules for valid identifiers
1. A valid identifier is a sequence of one or more
letters, digits or underscore characters (_).
2. Neither spaces nor punctuation marks or
symbols can be part of an identifier.
3. Only letters, digits and single underscore
characters are valid.
4. Variable identifiers should begin with an
underscore character (_ ) or a letter
 Starts with an underscore “_” or a letter. Cannot start
with a digit or a symbol.
Examples are :- Name, gender, _Students, num2.
 Can include letters, underscore, or digits.
Examples are: keyboard, Master, Junction, Player1,
total_grade, _ScoreSide1.
 Cannot include special characters such as !, %, ], or
$.
 Cannot include an empty space.
 Cannot be any of the reserved words.
 Should not be longer than 32 characters. (although
allowed).
Identifiers-naming variable
Initialization of variables
There are two ways to initialize variables in C++:
1. type identifier = initial_value ;
For example:
int a = 0;
2. type identifier (initial_value) ;
For example:
int a (0);
This way to initialize variables, known as
constructor initialization, is done by enclosing
the initial value between parentheses (()):
C++ Variable Scope:
A scope is a region of the program and there are
three places where variables can be declared:
 1. Inside a function or a block which is called local
variables,
 2. In the definition of function parameters which is
called formal parameters.
 3. Outside of all functions(in the main body of the
source code) which is called global variables.
 Global variables can be referred from anywhere in
the code, even inside functions, whenever it is after
its declaration.
 The scope of local variables is limited to the block
enclosed in braces ({}) where they are declared
Examples :-
int num1;
int TotaL ;
float Stud_avg ;
float _mark1 ;
Valid variable
declaration
int @num ;
int 2nd ;
float m1#m2 ;
float stud mark ;
Invalid variable
declaration
3. Constants
• Constants have fixed value.
• Constants, like variables, contain data type.
Defining Constants:
There are two simple ways in C++ to define
constants:
1. Using #define preprocessor.
2. Using const keyword.
The #define Preprocessor:
Following is the form to use #define preprocessor
to define a constant:
#define identifier value
eg: #define LENGTH 10
The const Keyword:
can use const prefix to declare constants with a
specific type as follows:
const type variable = value;
eg: const int LENGTH = 10;
4. Strings
Strings in C++ are represented by arrays of characters.
For example :- “Enter a number ";
5. Operators and special
characters
Arithmetic Operators Relational Operators
Logical Operators Bitwise Operators
Assignment Operators Misc Operators
A simple program
# include <iostream.h> // header file
void main ( ) // main function
{
int a, b; // variable declaration
cout << “enter a number";
cin>>a>>b; statements
cout>>” Sum= “>> a+b;
}
Basic Data Types
C++ DataTypes
User-definedType Built-inType DerivedType
IntegralType Void FloatingType
structure
union
class
enumeration
array
function
pointer
reference
int char float double
1. Built-in Data Types
int, char, float, double are known as basic or
fundamental data types.
Signed, unsigned, long, short modifier for
integer and character basic data types.
Long modifier for double.
Basic Data Types
ANSI C++ added two more data types
 bool
 wchar_t
continue…
Data Type - bool
A variable with bool type can hold a Boolean value
true or false.
Declaration:
bool b1; // declare b1 as bool type
b1 = true; // assign true value to b1
bool b2 = false; // declare and initialize
The default numeric value of true is 1 and
false is 0.
Data Type – wchar_t
The character type wchar_t has been defined to
hold 16-bit wide characters.
wide_character uses two bytes of memory.
wide_character literal in C++ begin with the
letter L
L‘xy’ // wide_character literal
Fundamental data types
Name Description Size Range
char Character 1 byte signed: -128 to 127
unsigned: 0 to 255
short int
(short)
Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer 2 bytes signed: -32768 to 32767
unsigned: 0 to 65535
long int
(long)
Long integer. 4bytes signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
bool Boolean.It can
take one of two
values: true
or false.
n value
1byte true or false
Fundamental data types
Name Description Size Range
float Floating point
number.
4bytes +/- 3.4e +/- 38 (~7 digits)
double Double
precision
floating point
number.
8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double
precision
floating point
number.
10bytes +/- 3.4E-4932 to 1.1E+4932
wchar_t Wide character. 2 or 4
bytes
1 wide character
* The values of the columns Size and Range depend on the system the
program is compiled for. The values shown above are those found on most
32-bit systems
Built-in Data Types
Type void was introduced in ANSI C.
Two normal use of void:
o To specify the return type of a function when it is
not returning any value.
o To indicate an empty argument list to a function.
o eg:- void function-name ( void )
continue…
Some examples :-
int A , B ;
A = 10 ; // is valid
B = 12.34 ; // is not valid because the type of ‘B’ is int
float N, M ;
N = 3.14 ; // is valid
M = ‘A’ ; // is not valid because the type of ‘M’ is float
char S[3] , C ;
S = “Msc” ; // is valid
C= ‘A’ ; // is valid
2. Derived data types
 Derived data types are derived from basic
data type.
 A derived data type must contains a basic
data type.
Eg :- int *p, int a [10], float B [25] ;
 Array, Function,Reference and Pointer are
derived data types
Functions:-
 A function is a group of statements that together
perform a task.
 Every C++ program has at least one function which is
main(), and can define additional functions.
 A function declaration tells the compiler about a
function's name, return type, and parameters.
 A function definition provides the actual body of the
function
Benefits:-
1. top-down - structured programming
2. to reduce length of the program
3. reusability
4. function over-loading
Defining a Function:
The general form of a C++ function definition is as follows:
return_type function_name( parameter list )
{
body of the function
}
ReturnType:A function may return a value.The return_type is the data
type of the value the function returns. Some functions perform the
desired operations without returning a value. In this case, the
return_type is the keyword void.
Function Name:This is the actual name of the function.The function
name and the parameter list together constitute the function signature.
Parameters: A parameter is like a placeholder.When a function is invoked,
you pass a value to the parameter.This value is referred to as actual
parameter or argument.The parameter list refers to the type, order,
and number of the parameters of a function. Parameters are optional;
that is, a function may contain no parameters.
Function Body:The function body contains a collection of statements that
define
Example:-// function returning the max between
two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
Calling a Function
 To use a function, call or invoke that function.
 When a program calls a function, program
control is transferred to the called function.
 A called function performs defined task and
when its return statement is executed or when
its function-ending closing brace is reached, it
returns program control back to the main
program.
 To call a function simply need to pass the
required parameters along with function name
and if function returns a value ,store returned
value
#include <iostream.h>
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100;
int b = 200;
int ret;
// calling a function to get
max value.
ret = max(a, b);
cout << "Max value is : " << ret
<< endl;
return 0;
}
// function returning the max
between two numbers
int max(int num1, int num2)
{ // local variable declaration
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
 Array is a collection of same type elements.
Examples :- int a[5] ;
a [0] a [1] a [2] a [3] a [4]
 character array is called a string.
Examples :- char S [10 ] ;
 Pointer is used to hold the address (memory
location) of a value or variable.
A =10
1200
1211
1222
1233
1244
int A = 10 ;
int *p ;
P= & A; // assign the address of A
cout<< P ; // output = 1211
cout<< *P ; // output = 10
Example
3. User-Defined Data Types
Structures
 Structure is used to store different type of
data in a single variable .
 Structure is a user defined data type.
 In a structure declaration, we must use the
keyword Struct.
 A structure ends with a semi colon.
Structure
syntax :-
struct struct_name
{
data type variable1;
data type variable2;
…………………..
…………………..
};
Structure
Example
struct student
{
char name [ 20 ] ;
int mark1, mark2 ;
int total ;
};
Class
 Class in C++ is same as structure.
 Class is a concept of OOP.
 In a class declaration, we must use the keyword
Class.
 The class variables are known as objects.
 A class ends with a semi colon.
 the main difference of class and structure is, we
can include functions in a class.
Class syntax
:-
class class_name
{
data type variable1;
data type variable2;
function declarations;
…………………..
};
Class
Example
class student
{
char name [ 20 ] ;
int mark1, mark2 ;
void read ( );
void display ( );
};
User-Defined Data Types
Enumerated DataType:
Enumerated data type provides a way for
attaching names to numbers.
enum keyword automatically enumerates a list of
words by assigning them values 0, 1, 2, and so on.
enum shape {circle, square, triangle};
enum colour {red, blue, green, yellow};
enum position {off, on};
continue…
User-Defined Data Types
Enumerated DataType:
enum colour {red, blue, green, yellow};
In C++ the tag names can be used to declare
new variables.
colour background;
In C++ each enumerated data type retains its
own separate type.
C++ does not permit an int value to be
automatically converted to an enum value.
continue…
User-Defined Data Types
Enumerated DataType:
colour background = blue; // allowed
colour background = 3; // error in C++
colour background = (colour) 3; // OK
int c = red; // valid
continue…
User-Defined Data Types
Enumerated DataType:
By default, the enumerators are assigned
integer values starting with 0.
We can override the default value by explicitly
assigning integer values to the enumerators.
enum colour { red, blue=4, green =8};
enum colour {red=5, blue, green};
continue…
User-Defined Data Types
Enumerated DataType:
C++ also permits the creation of anonymous
enum (i.e., enum with out tag name).
enum {off, on}; here off  0 and on  1
int switch_1 = off;
int switch_2 = on;
continue…

More Related Content

Similar to CPP_,module2_1.pptx

1 intro
1 intro1 intro
1 introabha48
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesFellowBuddy.com
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
A Hand Book of Visual Basic 6.0.pdf.pdf
A Hand Book of Visual Basic 6.0.pdf.pdfA Hand Book of Visual Basic 6.0.pdf.pdf
A Hand Book of Visual Basic 6.0.pdf.pdfAnn Wera
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programmingPraveen Chowdary
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTnikshaikh786
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesBalamuruganV28
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++ shammi mehra
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptxBilalHussainShah5
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionRai University
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programmingPraveen M Jigajinni
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languagesppd1961
 
Mca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroductionMca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroductionRai University
 

Similar to CPP_,module2_1.pptx (20)

1 intro
1 intro1 intro
1 intro
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
A Hand Book of Visual Basic 6.0.pdf.pdf
A Hand Book of Visual Basic 6.0.pdf.pdfA Hand Book of Visual Basic 6.0.pdf.pdf
A Hand Book of Visual Basic 6.0.pdf.pdf
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programming
 
MCA NOTES.pdf
MCA NOTES.pdfMCA NOTES.pdf
MCA NOTES.pdf
 
OOP-1.pptx
OOP-1.pptxOOP-1.pptx
OOP-1.pptx
 
C++
C++C++
C++
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPT
 
Object Oriented Programming All Unit Notes
Object Oriented Programming All Unit NotesObject Oriented Programming All Unit Notes
Object Oriented Programming All Unit Notes
 
Programming In C++
Programming In C++ Programming In C++
Programming In C++
 
OOP ppt.pdf
OOP ppt.pdfOOP ppt.pdf
OOP ppt.pdf
 
Interview preparation for programming.pptx
Interview preparation for programming.pptxInterview preparation for programming.pptx
Interview preparation for programming.pptx
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroduction
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Unit 1 OOSE
Unit 1 OOSE Unit 1 OOSE
Unit 1 OOSE
 
Chapter1 introduction
Chapter1 introductionChapter1 introduction
Chapter1 introduction
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 
Mca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroductionMca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroduction
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxakanksha16arora
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17Celine George
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 

Recently uploaded (20)

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 

CPP_,module2_1.pptx

  • 2. Procedure-Oriented Programming  Procedure-Oriented Programming using high level languages like COBOL, FORTRAN, C, etc.  The primary focus is on functions.  Procedure-oriented programming basically consists of writing a list of instructions for the computer to follow and organizing these instructions into groups known as functions.
  • 3. Typical structure of procedure-oriented program Main Program Function-1 Function-2 Function-3 Function-4 Function-5 Function-6 Function-7 Function-8
  • 4. Procedure-Oriented Programming  Unrestricted Access (for Global variables)  In a large program,it is very difficult to identify what data is used by which function. In case we need to revise an external data structure, we also need to revise all functions that access the data.This provides an opportunity for bugs to creep in.  This approach does not model real world problems.  This is because functions are action-oriented and do not really corresponding to the element of the problem. Drawbacks :-
  • 5. Relationship of data and functions in procedural programming Function-1 Function-2 Function-3 Local Data Local Data Local Data Global Data Global Data
  • 6. Object-Oriented Programming  OOP treat data as a critical element in the program development and does not allow it to flow freely around the system.  It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions.  OOP allows decomposition of a problem into a number of entities called objects and then build data functions around these objects.
  • 7. Organization of data and functions in OOP Data Functions Object A Data Functions Object B Data Functions Object C Communication
  • 8. Characteristics of Object- Oriented Programming  Emphasis is on data rather than procedure.  Programs are divided into objects.  Data structures are designed such that they characterize the objects.  Functions that operate on the data of an object are tied together in the data structure.  Data is hidden and can not be accessed by external functions.
  • 9. An object is like a black box. The internal details are hidden.  Identifying objects and assigning responsibilities to these objects.  Objects communicate to other objects by sending messages.  Messages are received by the methods of an object 9 What is Object Oriented Programming?
  • 10. Basic Concepts of Object- Oriented Programming 1. Objects 2. Classes 3. Data Abstraction 4. Encapsulation 5. Inheritance 6. Polymorphism 7. Dynamic Binding 8. Message Passing
  • 11.  TangibleThings as a car, printer, ...  Roles as employee, boss, ...  Incidents as flight, overflow, ...  Interactions as contract, sale, ...  Specifications as colour, shape, … 11 What is an object?
  • 12.  an object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. Or  An "object" is anything to which a concept applies. Etc. 12 So, what are objects?
  • 13.  Modularity - large software projects can be split up in smaller pieces.  Reusability - Programs can be assembled from pre-written software components.  Extensibility - New software components can be written or developed from existing ones. 13 Why do we care about objects?
  • 14.  The most important aspect of an object is its behaviour (the things it can do).  A behaviour is initiated by sending a message to the object (usually by calling a method). 14 Basic Terminology: Behaviour and Messages
  • 15. Basic Concepts of OOP  Objects Objects are the basic run-time entities in an object-oriented system.They may represent a person, a place, a bank account, etc. continue …
  • 16. Basic Concepts of OOP  Objects Object : CUSTOMER DATA AC No. Name of AC Holder Address FUNCTIONS Deposit Withdrawal AC Balance Display continue …
  • 17. Basic Concepts of OOP Class Classes are user-defined data types. A class is a collection of objects of similar type.  The entire set of data and code of an object can be made a user-defined data type with the help of a class.  Objects are variables of the type class.  Once a class has been defined, we can create any number of objects belonging to that class.  Each object is associated with the data of type class with which they are created. continue …
  • 18. Basic Concepts of OOP Class ( Syntax ) class class_name { variable declaration; ………………………… ………………………… function declarations; ………………………… ………………………… }; Class ( Example ) class student { int rollnumber; char name [20]; float marks; void getdata(); void putdata(); };
  • 19. Basic Concepts of OOP  Class ( Example) If fruit has been defined as a class, then the statement fruit mango ; will create an object mango belonging to the class fruit. syntax :- class_name object-name ; continue …
  • 20.  Making Classes: Creating, extending or reusing abstract data types.  Making Objects interact: Creating objects from abstract data types and defining their relationships. 20 The two steps of Object Oriented Programming
  • 21. Basic Concepts of OOP  Data Abstraction and Encapsulation o The wrapping up of data and functions into a single unit is known as encapsulation. o The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. o Abstraction is the representation of the essential features of an object.These are ‘encapsulated’ into an abstract data type. o These functions provide the interface between the object’s data and the program.This insulation of the data from direct access by the program is called data hiding or information hiding. continue …
  • 22. Basic Concepts of OOP  Data Abstraction and Encapsulation The attributes wrapped in the classes are called data members and the functions that operate on these data are called methods or member functions. Since the classes use the concept of data abstraction, they are known as Abstract DataTypes (ADT). continue …
  • 23. Basic Concepts of OOP  Inheritance o Inheritance is the process by which objects of one class acquire the properties of objects of another class. o Each derived class shares common characteristics with the class from which it is derived. continue …
  • 24. Property Inheritance Bird Attributes: Feathers Lay eggs Flying Bird Attributes: ------------ ------------ Non-flying Bird Attributes: ------------ ------------ Penguin Attributes: ------------ ------------ Kiwi Attributes: ------------ ------------ Robin Attributes: ------------ ------------ Swallow Attributes: ------------ ------------
  • 25. Basic Concepts of OOP  Inheritance o Inheritance provides the idea of reusability. o We can add additional features to an existing class without modifying it. (By deriving new class from existing one.The new class will have the combined features of both the classes.) continue …
  • 26. Basic Concepts of OOP  Polymorphism – ability to take more than one form o An operation may exhibit different behaviors in different instances. o The behavior depends upon the types of data used in the operation. continue …
  • 27. Basic Concepts of OOP  Polymorphism - ability to take more than one form o The process of making an operator to exhibit different behaviors in different instances is known as operator overloading. o << Insertion Operator o << Left-shift bit-wise operator o Using a single function name to perform different types of tasks is known as function overloading. o add( 3, 5) gives 8 o Add(“hello”, “-world”) gives “hello-world” continue …
  • 28. Basic Concepts of OOP  Dynamic Binding  Dynamic binding ( late binding ) means that the code associated with a given procedure call is not known until the time of the call at run-time.  It is associated with polymorphism and inheritance. continue …
  • 29. Benefits of OOP 1. Inheritance – eliminate redundant code and extend the use of existing classes. 2. We can build programs from the standard working module, no need of starting from the scratch. 3. Data hiding helps the programmer to build secure programs that can not be invaded by code in other parts of the program.
  • 30. Benefits of OOP 4. Multiple instances of an objects can co-exists with out any interference. 5. It is easy to partition the work in a project based on objects. 6. Object-oriented system can be easily upgraded from small to large systems. 7. Message passing techniques for communication between objects makes the interface descriptions with external systems much simpler. 8. Software complexity can be easily managed.
  • 31. Application of OOP  Real-time system • Simulation and modeling • Object-oriented data bases • Hypertext, Hypermedia, and expertext • AI and expert systems • Neural networks and parallel programming • Decision support and office automation systems • CIM/CAM/CAD systems
  • 32. What is C++  C++ is an object-oriented programming language. C++ is the updated version of C.  Developed by Bjarne Stroustrup at AT&T Bell Laboratories, USA in the early 1980’s.  The first commercial implementation of C++ was released on 14 October 1985  C++ is sometimes called a hybrid language because it is possible to write object oriented or procedural code in the same program in C++.
  • 33. Structure of a C++ program  Include Files Class declaration Member function definitions Main function program
  • 34. Simple Structure of a C++ Program Header files ………………….. ………………….. main function ( ) { variable declaration // Member function // statements ………………. ……………….. } // Member function execution { } // #include<iostream.h> #include<conio.h> void main ( ) { variable declaration statements ………………. ……………….. }
  • 35. The main( ) Function  The main( ) returns a value of type int to the operating system by default.  The functions that have a return value should use the return statement for termination.  Use void main( ), if the function is not returning any value.
  • 36. Main Function int main () • This line corresponds to the beginning of the definition of the main function. • The main function is the point by where all C++ programs start their execution, independently of its location within the source code. • It does not matter whether there are other functions with other names defined before or after it – the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function. • The word main is followed in the code by a pair of parenthesis (()).That is because it is a function declaration: • Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
  • 37. Output Operator cout << "Hi, this is my program!"; cout is a predefined object that represents the standard output operator in C++ screen cout << C++ variable Object Insertion Operator
  • 38. Output Operator  The operator << is called insertion or put to operator.  It inserts or sends the contents of the variable on its right to the object on its left cout << "Hi, this is my program!"; cout << number ; screen cout << C++ variable Object Insertion Operator
  • 39. Input Operator cin >> number; cin is a predefined object that corresponds to the standard input operator in C++, here the standard input stream is keyboard. cin >> 50.55 Object Extraction Operator C++ variable Keyboard
  • 40. Input Operator  The operator >> is known as extraction or get from operator.  It extracts ( or takes ) the value from the keyboard and assigns it to the variable on its right. cin >> 50.55 Object Extraction Operator C++ variable Keyboard
  • 41. Cascading of I/O Operators  Multiple use of << or >> in one statement is called cascading. eg:- cout << “Sum = ” << sum << “n” ; cin >> num1 >> num2 ;
  • 42. Comments in C++  supports single line and multi-line comments.  All characters available inside any comment are ignored by C++ compiler.  Block comments start with /* and end with */  Line comments start with //,extending to the end of the line eg:- //This is a single line comment /* C++ comments can also * span multiple lines */
  • 43. Header Files  a header file sometimes known as an include file.  Header files almost always have a .h extension.  The purpose of a header file is to hold declarations for other files or functions to use.  Examples :- iostream.h, conio.h , string.h, stdio.h, etc….
  • 44.  iostream. h - input output stream. iostream.h is used for input output functions . ( cout , cin)  conio. h - concatenated input output . conio.h includes the declarations of getch, getc, putc, clrscr, etc. Commonly used Header Files and its purpose  string. h - used for string handling functions such as strcpy, strlen strrev, strcpy etc….
  • 45. Tokens The smallest individual units in a program are known as tokens. 1. Keywords 2. Identifiers 3. Constants 4. Strings 5. Operators and special characters
  • 47. 1. Keywords • Keywords are the reserved words used in C++. • We cannot use the keywords as variables. Eg :- public, while, void, friend, if, int …etc. 2. Identifiers (variables) • A variable is used to store values in the program. • A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Eg :- int A, B, C ;
  • 48. Identifiers Rules for valid identifiers 1. A valid identifier is a sequence of one or more letters, digits or underscore characters (_). 2. Neither spaces nor punctuation marks or symbols can be part of an identifier. 3. Only letters, digits and single underscore characters are valid. 4. Variable identifiers should begin with an underscore character (_ ) or a letter
  • 49.  Starts with an underscore “_” or a letter. Cannot start with a digit or a symbol. Examples are :- Name, gender, _Students, num2.  Can include letters, underscore, or digits. Examples are: keyboard, Master, Junction, Player1, total_grade, _ScoreSide1.  Cannot include special characters such as !, %, ], or $.  Cannot include an empty space.  Cannot be any of the reserved words.  Should not be longer than 32 characters. (although allowed). Identifiers-naming variable
  • 50. Initialization of variables There are two ways to initialize variables in C++: 1. type identifier = initial_value ; For example: int a = 0; 2. type identifier (initial_value) ; For example: int a (0); This way to initialize variables, known as constructor initialization, is done by enclosing the initial value between parentheses (()):
  • 51. C++ Variable Scope: A scope is a region of the program and there are three places where variables can be declared:  1. Inside a function or a block which is called local variables,  2. In the definition of function parameters which is called formal parameters.  3. Outside of all functions(in the main body of the source code) which is called global variables.  Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.  The scope of local variables is limited to the block enclosed in braces ({}) where they are declared
  • 52. Examples :- int num1; int TotaL ; float Stud_avg ; float _mark1 ; Valid variable declaration int @num ; int 2nd ; float m1#m2 ; float stud mark ; Invalid variable declaration
  • 53. 3. Constants • Constants have fixed value. • Constants, like variables, contain data type. Defining Constants: There are two simple ways in C++ to define constants: 1. Using #define preprocessor. 2. Using const keyword.
  • 54. The #define Preprocessor: Following is the form to use #define preprocessor to define a constant: #define identifier value eg: #define LENGTH 10 The const Keyword: can use const prefix to declare constants with a specific type as follows: const type variable = value; eg: const int LENGTH = 10;
  • 55. 4. Strings Strings in C++ are represented by arrays of characters. For example :- “Enter a number "; 5. Operators and special characters Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators
  • 56. A simple program # include <iostream.h> // header file void main ( ) // main function { int a, b; // variable declaration cout << “enter a number"; cin>>a>>b; statements cout>>” Sum= “>> a+b; }
  • 57. Basic Data Types C++ DataTypes User-definedType Built-inType DerivedType IntegralType Void FloatingType structure union class enumeration array function pointer reference int char float double
  • 58. 1. Built-in Data Types int, char, float, double are known as basic or fundamental data types. Signed, unsigned, long, short modifier for integer and character basic data types. Long modifier for double.
  • 59. Basic Data Types ANSI C++ added two more data types  bool  wchar_t continue…
  • 60. Data Type - bool A variable with bool type can hold a Boolean value true or false. Declaration: bool b1; // declare b1 as bool type b1 = true; // assign true value to b1 bool b2 = false; // declare and initialize The default numeric value of true is 1 and false is 0.
  • 61. Data Type – wchar_t The character type wchar_t has been defined to hold 16-bit wide characters. wide_character uses two bytes of memory. wide_character literal in C++ begin with the letter L L‘xy’ // wide_character literal
  • 62. Fundamental data types Name Description Size Range char Character 1 byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer 2 bytes signed: -32768 to 32767 unsigned: 0 to 65535 long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 bool Boolean.It can take one of two values: true or false. n value 1byte true or false
  • 63. Fundamental data types Name Description Size Range float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. 10bytes +/- 3.4E-4932 to 1.1E+4932 wchar_t Wide character. 2 or 4 bytes 1 wide character * The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems
  • 64. Built-in Data Types Type void was introduced in ANSI C. Two normal use of void: o To specify the return type of a function when it is not returning any value. o To indicate an empty argument list to a function. o eg:- void function-name ( void ) continue…
  • 65. Some examples :- int A , B ; A = 10 ; // is valid B = 12.34 ; // is not valid because the type of ‘B’ is int float N, M ; N = 3.14 ; // is valid M = ‘A’ ; // is not valid because the type of ‘M’ is float char S[3] , C ; S = “Msc” ; // is valid C= ‘A’ ; // is valid
  • 66. 2. Derived data types  Derived data types are derived from basic data type.  A derived data type must contains a basic data type. Eg :- int *p, int a [10], float B [25] ;  Array, Function,Reference and Pointer are derived data types
  • 67. Functions:-  A function is a group of statements that together perform a task.  Every C++ program has at least one function which is main(), and can define additional functions.  A function declaration tells the compiler about a function's name, return type, and parameters.  A function definition provides the actual body of the function Benefits:- 1. top-down - structured programming 2. to reduce length of the program 3. reusability 4. function over-loading
  • 68. Defining a Function: The general form of a C++ function definition is as follows: return_type function_name( parameter list ) { body of the function } ReturnType:A function may return a value.The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name:This is the actual name of the function.The function name and the parameter list together constitute the function signature. Parameters: A parameter is like a placeholder.When a function is invoked, you pass a value to the parameter.This value is referred to as actual parameter or argument.The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body:The function body contains a collection of statements that define
  • 69. Example:-// function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 70. Calling a Function  To use a function, call or invoke that function.  When a program calls a function, program control is transferred to the called function.  A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.  To call a function simply need to pass the required parameters along with function name and if function returns a value ,store returned value
  • 71. #include <iostream.h> // function declaration int max(int num1, int num2); int main () { // local variable declaration: int a = 100; int b = 200; int ret; // calling a function to get max value. ret = max(a, b); cout << "Max value is : " << ret << endl; return 0; } // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 72.  Array is a collection of same type elements. Examples :- int a[5] ; a [0] a [1] a [2] a [3] a [4]  character array is called a string. Examples :- char S [10 ] ;
  • 73.  Pointer is used to hold the address (memory location) of a value or variable. A =10 1200 1211 1222 1233 1244 int A = 10 ; int *p ; P= & A; // assign the address of A cout<< P ; // output = 1211 cout<< *P ; // output = 10 Example
  • 74. 3. User-Defined Data Types Structures  Structure is used to store different type of data in a single variable .  Structure is a user defined data type.  In a structure declaration, we must use the keyword Struct.  A structure ends with a semi colon.
  • 75. Structure syntax :- struct struct_name { data type variable1; data type variable2; ………………….. ………………….. }; Structure Example struct student { char name [ 20 ] ; int mark1, mark2 ; int total ; };
  • 76. Class  Class in C++ is same as structure.  Class is a concept of OOP.  In a class declaration, we must use the keyword Class.  The class variables are known as objects.  A class ends with a semi colon.  the main difference of class and structure is, we can include functions in a class.
  • 77. Class syntax :- class class_name { data type variable1; data type variable2; function declarations; ………………….. }; Class Example class student { char name [ 20 ] ; int mark1, mark2 ; void read ( ); void display ( ); };
  • 78. User-Defined Data Types Enumerated DataType: Enumerated data type provides a way for attaching names to numbers. enum keyword automatically enumerates a list of words by assigning them values 0, 1, 2, and so on. enum shape {circle, square, triangle}; enum colour {red, blue, green, yellow}; enum position {off, on}; continue…
  • 79. User-Defined Data Types Enumerated DataType: enum colour {red, blue, green, yellow}; In C++ the tag names can be used to declare new variables. colour background; In C++ each enumerated data type retains its own separate type. C++ does not permit an int value to be automatically converted to an enum value. continue…
  • 80. User-Defined Data Types Enumerated DataType: colour background = blue; // allowed colour background = 3; // error in C++ colour background = (colour) 3; // OK int c = red; // valid continue…
  • 81. User-Defined Data Types Enumerated DataType: By default, the enumerators are assigned integer values starting with 0. We can override the default value by explicitly assigning integer values to the enumerators. enum colour { red, blue=4, green =8}; enum colour {red=5, blue, green}; continue…
  • 82. User-Defined Data Types Enumerated DataType: C++ also permits the creation of anonymous enum (i.e., enum with out tag name). enum {off, on}; here off  0 and on  1 int switch_1 = off; int switch_2 = on; continue…