Introduction To OOPS and C++
Introduction To OOPS and C++
Introduction To OOPS and C++
Function-4 Function-5
Function-1 Function-2 Function-3
Data Data
Communication
Methods Methods
Object C
Methods
Data
Bottom Up approach:
– Lot of small modules will be grouped to form a single large
module
– Specific to General
• If the requirements are clear at the first instance, we can go for Top
Down approach.
• Classes
• Objects
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
Introduction to OOP(2 of 7)
Class & Object:
class object
For example, mango, apple and orange are members of the class
fruit.
Classes are user-defined data types and behave like the built-in
types of a programming language.
Data Abstraction
• Abstraction refers to the act of representing essential
features without including the background details or
explanations.
• Abstraction are of two types:
• Procedure Abstraction
• Data Abstraction
• The access specifiers in JAVA or any OOP language,
provides data abstraction.
Introduction to OOP(4 of 7)
Encapsulation
• That is, the data and methods are given in the class definition.
reusability.
parent class or super class. Class that inherits a parent class is called
char string[20] = {‘g’, ’e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ’e’, ‘e’, ‘k’,
‘s’, ‘\0’};
char string[20] = “geeksforgeeks”;
char string [] = “geeksforgeeks”;
Operator Description
++ Increment
-- Decrement
sizeof returns the size of a variable
, causes a sequence of operations to
be performed.
. (dot) and -> (arrow) are used to reference individual
members of classes, structures,
and unions.
& returns the address of a variable.
Operators(4 of 9)
Binary Operators:
Arithmetic Operators:
Operator Result
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus
Operators(5 of 9)
Relational Operators:
Operator Result
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Operators(6 of 9)
Logical Operators:
Operator Result
|| Logical OR
&& Logical AND
! Logical unary NOT
Operators(7 of 9)
Assignment Operators:
Operator Result
= assigns value from right opearnad to left operand
+= short hand addition assignment opeartor
-= short hand subtraction assignment opeartor
*= short hand multiplication assignment opeartor
/= short hand division assignment opeartor
%= short hand modulo assignment opeartor
Operators(8 of 9)
Conditional Operator or ternary Opeartor:
Condition ? X : Y
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Basic Data types(2 of 3)
Several of the basic types can be modified using one or more of
these type modifiers −
signed
unsigned
short
long
Basic Data types(3 of 3)
The following table shows the variable type, how much memory it takes to store the value in
memory, and what is maximum and minimum value which can be stored in such type of
variables.
Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 4bytes -2,147,483,648 to 2,147,483,647
signed long int 4bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
Type Conversion
Converting from one type to other type is called type conversion
Implicit conversion: Implicit conversions are automatically performed
when a value is copied to a compatible type
Example:
short a=2000;
int b;
b=a;
Type casting(Explicit conversion):If imlicit is not possible we need to
write a casting statement
Example:
double x = 10.3;
int y;
y = (int) x; // cast notation
Refer Example program typecast.cpp
Returning values from functions(1 of 4)
return statement:
The return statement stops execution and returns to the calling function.
When a return statement is executed, the function is terminated immediately
at that point, regardless of whether it's in the middle of a loop, etc.
Original value
yes
No
modified
Call by
Call by value
reference
Reference arguments(2 of 2)
Call by value
In call by value, value being passed to the function is locally stored by the
function parameter in stack memory location.
If you change the value of function parameter, it is changed for the current
function only.
It will not change the value of variable inside the caller method such as
main().
Call by reference
In call by reference, original value is modified because we pass reference
(address).
Here, address of the value is passed in the function, so actual and formal
arguments share the same address space.
Hence, value changed inside the function, is reflected inside as well as
outside the function.
Refer Example program callbyvalue.cpp
Refer Example program callbyref.cpp
Overloaded Function
C++ allows you to specify more than one definition for a function name
or an operator in the same scope, which is called function
overloading and operator overloading respectively.
Function Overloading:
We can have multiple definitions for the same function name in the same scope.
The definition of the function must differ from each other by the types and/or the
number of arguments in the argument list.
We cannot overload function declarations that differ only by return type.
Example: void print(int i)
void print(double f)
void print(char* c)
Syntax:
int vals[] = {10, 12, 33, 24, 50};
int& setValues( int i )
{
int vals[i]; // return a reference to the ith element
}
Refer Example program returnbyref.cpp