SlideShare a Scribd company logo
OOPS Using C++
Introduction
• OOPS stands for Object Oriented Programming System or Skills.
• Object means a real-world entity such as a pen, chair, table,
computer, watch, etc.
• Object-Oriented Programming is a methodology or paradigm to
design a program using classes and objects.
• It simplifies software development and maintenance by providing
some concepts: Object, Class, Inheritance, Polymorphism,
Abstraction, Encapsulation.
• The major purpose of C++ programming is to introduce the concept
of object orientation to the C programming language.
• Object
• Any entity that has state and behavior is known as an object. For
example: chair, pen, table, keyboard, bike etc. It can be physical and
logical.
• Class
• Collection of objects is called class. It is a logical entity.
• Inheritance
• When one object acquires all the properties and behaviours of
parent object i.e. known as inheritance. It provides code reusability.
It is used to achieve runtime polymorphism.
• Polymorphism
• When one task is performed by different ways i.e. known as
polymorphism. For example: to convince the customer differently, to
draw something e.g. shape or rectangle etc.
• In C++, we use Function overloading and Function overriding to
achieve polymorphism.
• Abstraction
• Hiding internal details and showing functionality is
known as abstraction. For example: phone call, we don't
know the internal processing.
• In C++, we use abstract class and interface to achieve
abstraction.
• Encapsulation
• Binding (or wrapping) code and data together into a
single unit is known as encapsulation. For example:
capsule, it is wrapped with different medicines.
Benefits of OOPS :
• OOP offers several benefits to both the program designer and the user Object-
orientation contributes to the solution of many problems associated with the
development and quality of software products.
• Through inheritance, we can eliminate redundant code and extend the use of
existing classes.
• We can build programs from the standard working modules that communicate
with one another, rather than having to start writing the code from scratch.
• This leads to saving of development time and higher productivity.
• Strict controls and protocols need to be developed if reuse is not to be
compromised.
• A software that is easy to use is hard to build. It is hoped that the object-
oriented programming languages like C++ and Java would help manage this
problem.
Object-Oriented Languages
• Object-oriented language was primarily designed to reduce complexity in typical
procedural languages through data binding and encapsulation techniques.
• In object-oriented language, the objects created provide limited or no access to other
functions or methods within the program.
• This enables only authorized or inherited methods/functions to access a particular object.
• Object-oriented language typically supports the following features, at minimum:
• The ability to create classes and their associated objects
• Encapsulation
• Inheritance
• Java, C++ and Smalltalk are popular examples of object-oriented languages.
Application of OOPs in C++
• It is about creating objects that contain both data and functions. Object-Oriented
programming has several advantages over procedural languages.
• As OOP is faster and easier to execute it becomes more powerful than procedural
languages like C++.
• OOPs is the most important and flexible paradigm of modern programming. It is
specifically useful in modeling real-world problems.
• Below are some applications of OOPs:
• Real-Time System design: Real-time system inherits complexities and makes it
difficult to build them. OOP techniques make it easier to handle those complexities.
• Hypertext and Hypermedia: Hypertext is similar to regular text as it can be stored,
searched, and edited easily. Hypermedia on the other hand is a superset of
hypertext. OOP also helps in laying the framework for hypertext and hypermedia.
• AI Expert System: These are computer application that is developed to
solve complex problems which are far beyond the human brain. OOP helps
to develop such an AI expert System
• Office automation System: These include formal as well as informal
electronic systems that primarily concerned with information sharing and
communication to and from people inside and outside the organization.
OOP also help in making office automation principle.
• Object-oriented database: The databases try to maintain a direct
correspondence between the real world and database object in order to let
the object retain it identity and integrity.
• Client-server system: Object-oriented client-server system provides the IT
infrastructure creating object-oriented server internet(OCSI) applications.
• CIM/CAD/CAM systems: OOP can also be used in manufacturing and
designing applications as it allows people to reduce the efforts involved. For
instance, it can be used while designing blueprints and flowcharts. So it
makes it possible to produce these flowcharts and blueprint accurately.
C++ Tokens
• C++ Tokens are the smallest individual units of a program.
• C++ is the superset of C and so most constructs of C are
legal in C++ with their meaning and usage unchanged. So
tokens, expressions, and data types are similar to that of C.
• Following are the C++ tokens : (most of c++ tokens are
basically similar to the C tokens)
• Keywords
• Identifiers
• Constants
Keywords
• Keywords are reserved words which have fixed meaning, and its meaning cannot be
changed.
• The meaning and working of these keywords are already known to the compiler.
• C++ has more numbers of keyword than C, and those extra ones have special working
capabilities.
• There are 32 of these, and here they are
• auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
While in C++ there are 31 additional keywords other than C Keywords
they are:
asm bool catch class
const_cast delete dynamic_cast explicit
export false friend inline
mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw
true try typeid typename
using virtual wchar_t
Identifiers
• Identifiers are used as the general terminology for the naming of variables,
functions and arrays.
• These are user-defined names consisting of an arbitrarily long sequence of
letters and digits with either a letter or the underscore(_) as a first character.
• Identifier names must differ in spelling and case from any keywords. You
cannot use keywords as identifiers; they are reserved for special use.
• Once declared, you can use the identifier in later program statements to
refer to the associated value.
• A special kind of identifier, called a statement label, can be used in goto
statements.
• There are certain rules that should be followed while naming c identifiers:
• They must begin with a letter or underscore(_).
• They must consist of only letters, digits, or underscore. No other
special character is allowed.
• It should not be a keyword.
• It must not contain white space.
• It should be up to 31 characters long as only the first 31 characters are
significant.
• main: method name.
• a: variable name.
Constants
• Constants are also like normal variables. But, the only difference is, their values can
not be modified by the program once they are defined. Constants refer to fixed values.
• They are also called literals.
• Constants may belong to any of the data type
Syntax:
• const data_type variable_name; (or) const data_type *variable_name;
• Types of Constants:
• Integer constants – Example: 0, 1, 1218, 12482
• Real or Floating-point constants – Example: 0.0, 1203.03, 30486.184
• Octal & Hexadecimal constants – Example: octal: (013 )8 = (11)10, Hexadecimal:
(013)16 = (19)10
• Character constants -Example: ‘a’, ‘A’, ‘z’
• String constants -Example: “Hello world”
Character Constant
• Character constants have either a single character or group of characters or a character with
backslash used for special purpose. These are further subdivided into three types:
• (i) Single Character Constant
• (ii) String Character Constant
• (iii) Backslash Character Constant
• (i) Single Character Constant
• Single Character constants are enclosed between single quotes(‘). For example, ‘a’ and ‘%’ are
both character constants. So these are also called single quote character constant.
• For example: ‘a’, ‘M’, ‘5’, ‘+’, ‘1’ etc. are some valid single character constant.
String Character Constant
• C++ supports another type of constant: the string. A string is a set of characters
enclosed in double quotes (“). For example: “Punar Deep” is a string.
• You must not confuse strings with characters. A single character constant is
enclosed in single quotes, as in ‘a’. However, “a” is a string containing only one
letter.
• For example: “Dinesh”, “Hello”, “2013”, “2013-2020”, “5+3”, “?+!” etc. are some
valid string character constant. These are used for printing purpose or display
purpose in the C++ program’s output statements. These can also be used for
assigning the string data to the character (string) type variables..
Backslash Character Constants
• Enclosing character constants in single quotes works for most printing
characters. A few, however, such as the carriage return, can’t be. For this
reason, C++ includes the special backslash character constants, shown
below, so that you may easily enter these special characters as constants.
• These are also referred to as escape sequences. You should use the
backslash codes instead of their ASCII equivalents to help ensure
portability.
• These are used for special purpose in the C++ language. These are used in
output statements like cout etc.
Features of C++
• Object-Oriented Programming
C++ is an Object-Oriented Programming Language, unlike C which is a procedural
programming language. This is the most important feature of C++. It can create/destroy
objects while programming. Also, It can create blueprints with which objects can be
created.
• Machine Independent
A C++ executable is not platform-independent (compiled programs on Linux won’t run on
Windows), however they are machine independent.
Suppose you have written a piece of code which can run on Linux/Windows/Mac OSx which
makes C++ Machine Independent but the executable file of the C++ cannot run on different
operating systems.
• Simple
It is a simple language in the sense that programs can be broken down into logical units and
parts, has rich library support, and a variety of data-types.
• High-Level Language
C++ is a High-Level Language, unlike C which is a Mid-Level Programming Language.
• Popular
C++ can be the base language for many other programming languages that
supports the feature of object-oriented programming.
• Case-sensitive
It is clear that the C++ is a case-sensitive programming language. For example,
cin is used to take input from the input stream.
• Compiler Based
C++ is a compiler-based language, unlike Python. That is C++ programs used to
be compiled and their executable file is used to run it.
• Dynamic Memory Allocation
When the program executes in the C++ then the variables are allocated the
dynamical heap space. Inside of the functions the variables are allocated in the
stack space.
• Memory Management
• C++ allows us to allocate the memory of a variable or an array in run time. This is known
as Dynamic Memory Allocation.
• In other programming languages such as Java and Python, the compiler automatically
manages the memories allocated to variables. But this is not the case in C++.
• In C++, the memory must be de-allocate dynamically allocated memory manually after
it is of no use.
• The allocation and deallocation of the memory can be done using the new and delete
operators respectively.
• Multithreading
Multithreading is a specialized form of multitasking and multitasking is a feature that
allows your system to execute two or more programs concurrently. In general, there are
two sorts of multitasking: process-based and thread-based.
• Process-based multitasking handles the concurrent execution of programs. Thread-
based multitasking deals with the multiprogramming of pieces of an equivalent
program.
• A multithreaded program contains two or more parts that will run concurrently.
Structure of a C+ + Program
Simple C++ program without class
1) // Comments: A comment is used to display additional information about the
program. A comment does not contain any programming logic. When a comment is
encountered by a compiler, the compiler simply skips that line of code. Any line
beginning with ‘//’ without quotes OR in between /*…*/ in C++ is comment.
2) #include: In C++, all lines that start with (#) sign are called directives and are
processed by a preprocessor which is a program invoked by the compiler. The #include
directive tells the compiler to include a file and #include<iostream>. It tells the compiler
to include the standard iostream file which contains declarations of all the standard
input/output library functions.
3) using namespace std: This is used to import the entirety of the std namespace into
the current namespace of the program. When we import a namespace we are
essentially pulling all type definitions into the current scope. The std namespace is huge.
4) int main(): This line is used to declare a function named “main”
which returns data of integer type or void. Which is knows as a return
type. A function is a group of statements that are designed to perform
a specific task. Execution of every C++ program begins with the main()
function, no matter where the function is located in the program. So,
every C++ program must have a main() function.
5) { and } : The opening braces ‘{‘ indicates the beginning of the main
function and the closing braces ‘}’ indicates the ending of the main
function. Everything between these two comprises the body of the
main function.
6) cout<<“Hello Class”; : This line tells the compiler to display the
message “Hello World” on the screen. A semi-colon ‘;’ is used to end a
statement. Semi-colon character at the end of the statement is used to
indicate that the statement is ending there. Everything followed by the
character “<<” is displayed to the output device. This is a statement.
7) return 0; : This is also a statement. This statement is used to return a value from a
function and indicates the finishing of a function. This statement is basically used in
functions to return the results of the operations performed by a function.
8) cin>>a; : The cin object in C++ is an object of class iostream. It is used to accept the input
from the standard input device i.e. keyboard. It is associated with the standard C input
stream stdin. The extraction operator(>>) is used along with the object cin for reading
inputs. The extraction operator extracts the data from the object cin which is entered using
the keyboard.
Important Points to Note while Writing a C++ Program:
Always include the necessary header files for the smooth execution of functions. For
example, <iostream> must be included to use std::cin and std::cout.
The execution of code begins from the main() function.
It is a good practice to use Indentation and comments in programs for easy understanding.
cout is used to print statements and cin is used to take inputs.
Basic data types in C++
• Data types specify the type of data that a valid C# variable can hold.
C# is a strongly typed programming language because in C#, each
type of data (such as integer, character, float, and so forth) is
predefined as part of the programming language.
• Data types in C# is mainly divided into three categories
• Value Data Types
• Reference Data Types
• Pointer Data Type
Value Data Type :-
• The value data types are integer-based and floating-point based. C# language supports
both signed and unsigned literals.
• There are 2 types of value data type in C++ language.
• 1) Predefined Data Types - such as Integer, Boolean, Float, etc.
• 2) User defined Data Types - such as Structure, Enumerations, etc.
• The memory size of data types may change according to 32 or 64 bit operating system.
• Let's see the value data types. It size is given according to 32 bit OS.
Data Types Memory Size Range
char 1 byte -128 to 127
signed char 1 byte -128 to 127
unsigned char 1 byte 0 to 127
short 2 byte -32,768 to 32,767
signed short 2 byte -32,768 to 32,767
unsigned short 2 byte 0 to 65,535
int 4 byte -2,147,483,648 to -2,147,483,647
signed int 4 byte -2,147,483,648 to -2,147,483,647
unsigned int 4 byte 0 to 4,294,967,295
long 8 byte ?9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
• In c++, a data type is a Value Type if it holds the value of variable
directly on its own memory space and Value Types will use Stack
memory to store the values of the variables.
• For example, if we define and assign a value to the variable like int x =
123; then the system will use the same memory space of variable ‘x’
to store the value ‘123’.
• Following is the pictorial representation of value types in c++
programming language.
Object Oriented Programming  Using C++.pptx
Reference Data Type :-
• The reference data types do not contain the actual data stored in a
variable, but they contain a reference to the variables.
• If the data is changed by one of the variables, the other variable
automatically reflects this change in value.
• There are 2 types of reference data type in C++ language.
• 1) Predefined Types - such as Objects, String.
• 2) User defined Types - such as Classes, Interface.
• In c++, Reference Types will contain a pointer which points to other
memory location that holds the data. The Reference Types won’t store
the variable value directly in its memory instead, it will store the memory
address of the variable value to indicate where the value is being stored.
• For example, if we define and assign a value to the variable like string
name = "Suresh Dasari"; then the system will store the variable value
“Suresh Dasari” in one location and the variable "name" in another
location along with the memory address of the variable value.
• Following is the pictorial representation of reference type in c++
programming language.
Object Oriented Programming  Using C++.pptx
Pointer Data Type :-
• The pointer in C++ language is a variable, it is also known as locator or
indicator that points to an address of a value.
• Symbols used in pointer :-
Symbol Name Description
& (ampersand
sign)
Address
operator
Determine the
address of a
variable.
* (asterisk
sign)
Indirection
operator
Access the
value of an
address.
• Declaring a pointer :-
• The pointer in C# language can be declared using * (asterisk symbol).
• int * a; //pointer to int
• char * c; //pointer to char
C++ Variables
• Variables in C++ is a name given to a memory location. It is the basic unit of storage in a
program.
• The value stored in a variable can be changed during program execution.
• A variable is only a name given to a memory location, all the operations done on the
variable effects that memory location.
• In C++, all the variables must be declared before use.
Rules For Declaring Variable
The name of the variable contains letters, digits, and underscores.
The name of the variable is case sensitive (ex Arr and arr both are different variables).
The name of the variable does not contain any whitespace and special characters (ex #,$,%,*,
etc).
All the variable names must begin with a letter of the alphabet or an underscore(_).
We cannot used C++ keyword(ex float,double,class)as a variable name.
• How to Declare Variables?
• A typical variable declaration is of the form:
// Declaring a single variable
type variable_name;
// Declaring multiple variables:
• type variable1_name, variable2_name, variable3_name;
• A variable name can consist of alphabets (both upper and lower case),
numbers, and the underscore ‘_’ character. However, the name must
not start with a number.
• datatype: Type of data that can be stored in this variable.
• variable_name: Name given to the variable.
• value: It is the initial value stored in the variable.
Object Oriented Programming  Using C++.pptx
Type Conversion in C++
• A type cast is basically a conversion from one type to another. There are two
types of type conversion:
• Implicit Type Conversion Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without any external trigger from the user.
• Generally takes place when in an expression more than one data type is
present. In such condition type conversion (type promotion) takes place to
avoid lose of data.
• All the data types of the variables are upgraded to the data type of the
variable with largest data type.
• Explicit Type Conversion: This process is also called type casting and it is user-
defined. Here the user can typecast the result to make it of a particular data type.
• In C++, it can be done by two ways:
• The word “explicit” means ‘open’ or ‘clear’. In explicit C++ type casting, the data
type in which the value is to be converted is clearly specified in the program. It is
done by cast operator. The cast operator is unary operator. It converts the value of
an expression into a value of the type specified.
• The general form of the C++ type casting operator is as follows;
• (type)expression Where,
• (type): it indicates one of the C++ data type (or a user-defined data type) to which
the value of the expression is to be converted.
• Expression: it indicates a constant value, variable or an expression whose data type
is to be converted.
Eg. int x;
x=(int)2.322;
cout<<x;
After executing the above statements, the floating-point value will be
converted into integer. The value assigned to variable ‘x’. the decimal portion
will be truncated( or removed. The value 2 will be displayed on the screen.
Implicit C++ Type Casting:
The word “implicit” means ‘understood’ or ‘embedded’. In implicit C++ type
casting, the data type in which the value is to be converted is not specified in
the program. It is automatically done by the C++ compiler.
When constant values and variables of different types are mixed in an
expression, they are converted into the same type. The compiler will convert
all operands of lower order data type into higher order data type.
• Implicit Type Conversion Also known as ‘automatic type conversion’.
• Done by the compiler on its own, without any external trigger from
the user.
• Generally takes place when in an expression more than one data type
is present. In such condition type conversion (type promotion) takes
place to avoid lose of data.
• All the data types of the variables are upgraded to the data type of
the variable with largest data type.
• bool -> char -> short int -> int ->
• unsigned int -> long -> unsigned ->
• long long -> float -> double -> long double
C++ Operators
• Operators are used to perform operations on variables and values.
• An operator is a symbol that operates on a value to perform specific mathematical or
logical computations.
• They form the foundation of any programming language. In C++, we have built-in
operators to provide the required functionality.
• Operators in C++ can be classified into 6 types:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Ternary or Conditional Operators
Operators Precedence in C++
• Operator precedence determines the grouping of terms in
an expression.
• The associativity of an operator is a property that
determines how operators of the same precedence are
grouped in the absence of parentheses.
• This affects how an expression is evaluated.
• Certain operators have higher precedence than others; for
example, the multiplication operator has higher precedence
than the addition operator:
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma
, Left to right
Decision Making, Loops, Arrays and Strings:
• The if Statement :-
• Use the if statement to specify a block of C++ code to be executed if a condition is true.
• Syntax
• if (condition) {
• // block of code to be executed if the condition is true
• }
• The if_else Statement :-
• Use the else statement to specify a block of code to be executed if the condition is false.
• Syntax
• if (condition) {
• // block of code to be executed if the condition is true
• } else {
• // block of code to be executed if the condition is false
• }
• The else if Statement :-
• Use the else if statement to specify a new condition if the first
condition is false.
• Syntax
• if (condition1) {
• // block of code to be executed if condition1 is true
• } else if (condition2) {
• // block of code to be executed if the condition1 is false and
condition2 is true
• } else {
• // block of code to be executed if the condition1 is false and
condition2 is false
• }
• C++ Switch Statements :-
• Use the switch statement to select one of many code blocks to be executed.
• Syntax
• switch(expression) {
• case x:
• // code block
• break;
• case y:
• // code block
• break;
• default:
• // code block
• }
• This is how it works:
• The switch expression is evaluated once
• The value of the expression is compared with the values of each case
• If there is a match, the associated block of code is executed
• The break and default keywords are optional.
• C++ Loops
• Loops can execute a block of code as long as a specified condition is
reached.
• Loops are handy because they save time, reduce errors, and they
make code more readable.
• C++ While Loop
• The while loop loops through a block of code as long as a specified
condition is true:
• Syntax
• while (condition) {
• // code block to be executed
• }
• The Do/While Loop
• The do/while loop is a variant of the while loop. This loop will execute the code block once,
before checking if the condition is true, then it will repeat the loop as long as the condition
is true.
• Syntax
• do {
• // code block to be executed
• }
• while (condition);
• C++ For Loop
• When you know exactly how many times you want to loop through a block of code, use the
for loop instead of a while loop:
• Syntax
• for (Initialization; condition; Iteration) {
• // code block to be executed
• }
C++ Arrays
• Like other programming languages, array in C++ is a group of similar
types of elements that have contiguous memory location.
• In C++ std::array is a container that encapsulates fixed size arrays. In
C++, array index starts from 0. We can store only fixed set of elements
in C++ array.
• Advantages of C++ Array
• Code Optimization (less code)
• Random Access
• Easy to traverse data
• Easy to manipulate data
• Easy to sort data etc.
• Disadvantages of C++ Array
• Fixed size
• C++ Array Types
• There are 2 types of arrays in C++ programming:
1.Single Dimensional Array
2.Multidimensional Array
C++ Single Dimensional Array
#include <iostream>
using namespace std;
int main()
{
int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array
//traversing array
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"n";
}
}
C++ Multidimensional Arrays
• The multidimensional array is also known as rectangular
arrays in C++. It can be two dimensional or three
dimensional. The data is stored in tabular form (row ∗
column) which is also known as matrix.
include <iostream>
using namespace std;
int main()
{
int test[3][3]; //declaration of 2D array
test[0][0]=5; //initialization
test[0][1]=10;
test[1][1]=15;
test[1][2]=20;
test[2][0]=30;
test[2][2]=10;
//traversal
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
cout<< test[i][j]<<" ";
}
cout<<"n"; //new line at each row
}
return 0;
}
String and String manipulations
• C++ supports a wide range of functions that manipulate
null-terminated strings. These are:
• strcpy(str1, str2): Copies string str2 into string str1.
• strcat(str1, str2): Concatenates string str2 onto the end of
string str1.
• strlen(str1): Returns the length of string str1.
• strcmp(str1, str2): Returns 0 if str1 and str2 are the same;
less than 0 if str1<str2; greater than 0 if str1>str2.
• strchr(str1, ch): Returns a pointer to the first occurrence of
character ch in string str1.
• strstr(str1, str2): Returns a pointer to the first occurrence of
string str2 in string str1.
Operators used for String Objects
Operator Meaning
= Assignment
+ Concatenation
== Equality
!= Inequality
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
[] Subscription
<< Output
>> Input
dot (.) operator in C/C++
• The dot (.) operator is used for direct member selection via object
name.
• In other words, it is used to access the child object.
Syntax:
• object.member;
• The dot and arrow operator are both used in C++ to access
the members of a class.
• They are just used in different scenarios.
• In C++, types declared as a class, struct, or union are
considered "of class type".
• Eg.
#include <stdio.h>
struct Point {
int x, y;
};
int main()
{
struct Point p1 = { 0, 1 };
// Accessing members of point p1
// using the dot operator
p1.x = 20;
Cout<<"x = %d, y = %d", p1.x, p1.y;
return 0;
}
Data members and Member functions in C++
• "Data Member" and "Member Functions" are the new names/terms for
the members of a class, which are introduced in C++ programming
language.
• The variables which are declared in any class by using any fundamental
data types (like int, char, float etc) or derived data type (like class,
structure, pointer etc.) are known as Data Members. And the functions
which are declared either in private section of public section are known
as Member functions.
• There are two types of data members/member functions in C++:
• There are two types of data members/member functions in C++:
1.Private members
2.Public members
1) Private members
• The members which are declared in private section of the class (using
private access modifier) are known as private members. Private
members can also be accessible within the same class in which they
are declared.
2) Public members
• The members which are declared in public section of the class (using
public access modifier) are known as public members. Public
members can access within the class and outside of the class by using
the object name of the class in which they are declared.
Object Oriented Programming  Using C++.pptx

More Related Content

Similar to Object Oriented Programming Using C++.pptx (20)

C++ Programming with examples for B.Tech
C++ Programming with examples for B.TechC++ Programming with examples for B.Tech
C++ Programming with examples for B.Tech
ashutoshgupta1102
 
C++ with student management system project
C++ with student management system projectC++ with student management system project
C++ with student management system project
Kratik Khandelwal
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
MamataAnilgod
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
C++
C++C++
C++
k v
 
M.c.a (sem iii) paper - i - object oriented programming
M.c.a (sem   iii) paper - i - object oriented programmingM.c.a (sem   iii) paper - i - object oriented programming
M.c.a (sem iii) paper - i - object oriented programming
रवींद्र वैद्य
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroduction
Rai University
 
@vtucode.in-module-1-c++-2022-scheme.pdf
@vtucode.in-module-1-c++-2022-scheme.pdf@vtucode.in-module-1-c++-2022-scheme.pdf
@vtucode.in-module-1-c++-2022-scheme.pdf
TheertheshTheertha1
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
Divyanshu Dubey
 
C++
C++C++
C++
Rome468
 
Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01
Getachew Ganfur
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
MZGINBarwary
 
object oriented programming language in c++
object oriented programming language in c++object oriented programming language in c++
object oriented programming language in c++
Ravikant517175
 
Object Oriented Programming With C Sharma A K
Object Oriented Programming With C Sharma A KObject Oriented Programming With C Sharma A K
Object Oriented Programming With C Sharma A K
visicpubali
 
CS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semesterCS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semester
VeeraswamyDasari2
 
Getting started with C++
Getting started with C++Getting started with C++
Getting started with C++
Michael Redlich
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
jatin batra
 
Mca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroductionMca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroduction
Rai University
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Michael Redlich
 
Oops concepts in c++ documentation
Oops concepts in c++ documentationOops concepts in c++ documentation
Oops concepts in c++ documentation
farouq umar
 
C++ Programming with examples for B.Tech
C++ Programming with examples for B.TechC++ Programming with examples for B.Tech
C++ Programming with examples for B.Tech
ashutoshgupta1102
 
C++ with student management system project
C++ with student management system projectC++ with student management system project
C++ with student management system project
Kratik Khandelwal
 
INTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptxINTRODUCTION TO C++.pptx
INTRODUCTION TO C++.pptx
MamataAnilgod
 
C++
C++C++
C++
k v
 
Bca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroductionBca 2nd sem u-1 iintroduction
Bca 2nd sem u-1 iintroduction
Rai University
 
@vtucode.in-module-1-c++-2022-scheme.pdf
@vtucode.in-module-1-c++-2022-scheme.pdf@vtucode.in-module-1-c++-2022-scheme.pdf
@vtucode.in-module-1-c++-2022-scheme.pdf
TheertheshTheertha1
 
Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01Programmingwithc 131017034813-phpapp01
Programmingwithc 131017034813-phpapp01
Getachew Ganfur
 
object oriented programming language in c++
object oriented programming language in c++object oriented programming language in c++
object oriented programming language in c++
Ravikant517175
 
Object Oriented Programming With C Sharma A K
Object Oriented Programming With C Sharma A KObject Oriented Programming With C Sharma A K
Object Oriented Programming With C Sharma A K
visicpubali
 
CS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semesterCS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semester
VeeraswamyDasari2
 
Getting started with C++
Getting started with C++Getting started with C++
Getting started with C++
Michael Redlich
 
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++  Langauage Training in Ambala ! BATRA COMPUTER CENTREC++  Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
jatin batra
 
Mca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroductionMca 2 sem u-1 iintroduction
Mca 2 sem u-1 iintroduction
Rai University
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Michael Redlich
 
Oops concepts in c++ documentation
Oops concepts in c++ documentationOops concepts in c++ documentation
Oops concepts in c++ documentation
farouq umar
 

Recently uploaded (20)

Talk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya WeersTalk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya Weers
Kaya Weers
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
Master tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 Professio
Master tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 ProfessioMaster tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 Professio
Master tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 Professio
Kari Kakkonen
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
A Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment GatewayA Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment Gateway
danielle hunter
 
New Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDBNew Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDB
ScyllaDB
 
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AI Emotional Actors:  “When Machines Learn to Feel and Perform"AI Emotional Actors:  “When Machines Learn to Feel and Perform"
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AkashKumar809858
 
From Legacy to Cloud-Native: A Guide to AWS Modernization.pptx
From Legacy to Cloud-Native: A Guide to AWS Modernization.pptxFrom Legacy to Cloud-Native: A Guide to AWS Modernization.pptx
From Legacy to Cloud-Native: A Guide to AWS Modernization.pptx
Mohammad Jomaa
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
SOFTTECHHUB
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
James Anderson
 
Contributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptxContributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptx
Patrick Lumumba
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AIAI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
Buhake Sindi
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 
Talk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya WeersTalk: On an adventure into the depths of Maven - Kaya Weers
Talk: On an adventure into the depths of Maven - Kaya Weers
Kaya Weers
 
AI Trends - Mary Meeker
AI Trends - Mary MeekerAI Trends - Mary Meeker
AI Trends - Mary Meeker
Razin Mustafiz
 
Master tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 Professio
Master tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 ProfessioMaster tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 Professio
Master tester AI toolbox - Kari Kakkonen at Testaus ja AI 2025 Professio
Kari Kakkonen
 
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath InsightsUiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPath Community Berlin: Studio Tips & Tricks and UiPath Insights
UiPathCommunity
 
A Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment GatewayA Comprehensive Guide on Integrating Monoova Payment Gateway
A Comprehensive Guide on Integrating Monoova Payment Gateway
danielle hunter
 
New Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDBNew Ways to Reduce Database Costs with ScyllaDB
New Ways to Reduce Database Costs with ScyllaDB
ScyllaDB
 
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AI Emotional Actors:  “When Machines Learn to Feel and Perform"AI Emotional Actors:  “When Machines Learn to Feel and Perform"
AI Emotional Actors: “When Machines Learn to Feel and Perform"
AkashKumar809858
 
From Legacy to Cloud-Native: A Guide to AWS Modernization.pptx
From Legacy to Cloud-Native: A Guide to AWS Modernization.pptxFrom Legacy to Cloud-Native: A Guide to AWS Modernization.pptx
From Legacy to Cloud-Native: A Guide to AWS Modernization.pptx
Mohammad Jomaa
 
Cyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptxCyber Security Legal Framework in Nepal.pptx
Cyber Security Legal Framework in Nepal.pptx
Ghimire B.R.
 
Introducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRCIntroducing the OSA 3200 SP and OSA 3250 ePRC
Introducing the OSA 3200 SP and OSA 3250 ePRC
Adtran
 
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Multistream in SIP and NoSIP @ OpenSIPS Summit 2025
Lorenzo Miniero
 
Droidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing HealthcareDroidal: AI Agents Revolutionizing Healthcare
Droidal: AI Agents Revolutionizing Healthcare
Droidal LLC
 
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
With Claude 4, Anthropic redefines AI capabilities, effectively unleashing a ...
SOFTTECHHUB
 
Agentic AI - The New Era of Intelligence
Agentic AI - The New Era of IntelligenceAgentic AI - The New Era of Intelligence
Agentic AI - The New Era of Intelligence
Muzammil Shah
 
Gihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai TechnologyGihbli AI and Geo sitution |use/misuse of Ai Technology
Gihbli AI and Geo sitution |use/misuse of Ai Technology
zainkhurram1111
 
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
GDG Cloud Southlake #43: Tommy Todd: The Quantum Apocalypse: A Looming Threat...
James Anderson
 
Contributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptxContributing to WordPress With & Without Code.pptx
Contributing to WordPress With & Without Code.pptx
Patrick Lumumba
 
SDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhereSDG 9000 Series: Unleashing multigigabit everywhere
SDG 9000 Series: Unleashing multigigabit everywhere
Adtran
 
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AIAI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
AI in Java - MCP in Action, Langchain4J-CDI, SmallRye-LLM, Spring AI
Buhake Sindi
 
Dev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API WorkflowsDev Dives: System-to-system integration with UiPath API Workflows
Dev Dives: System-to-system integration with UiPath API Workflows
UiPathCommunity
 

Object Oriented Programming Using C++.pptx

  • 2. Introduction • OOPS stands for Object Oriented Programming System or Skills. • Object means a real-world entity such as a pen, chair, table, computer, watch, etc. • Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. • It simplifies software development and maintenance by providing some concepts: Object, Class, Inheritance, Polymorphism, Abstraction, Encapsulation. • The major purpose of C++ programming is to introduce the concept of object orientation to the C programming language.
  • 3. • Object • Any entity that has state and behavior is known as an object. For example: chair, pen, table, keyboard, bike etc. It can be physical and logical. • Class • Collection of objects is called class. It is a logical entity. • Inheritance • When one object acquires all the properties and behaviours of parent object i.e. known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism. • Polymorphism • When one task is performed by different ways i.e. known as polymorphism. For example: to convince the customer differently, to draw something e.g. shape or rectangle etc. • In C++, we use Function overloading and Function overriding to achieve polymorphism.
  • 4. • Abstraction • Hiding internal details and showing functionality is known as abstraction. For example: phone call, we don't know the internal processing. • In C++, we use abstract class and interface to achieve abstraction. • Encapsulation • Binding (or wrapping) code and data together into a single unit is known as encapsulation. For example: capsule, it is wrapped with different medicines.
  • 5. Benefits of OOPS : • OOP offers several benefits to both the program designer and the user Object- orientation contributes to the solution of many problems associated with the development and quality of software products. • Through inheritance, we can eliminate redundant code and extend the use of existing classes. • We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. • This leads to saving of development time and higher productivity. • Strict controls and protocols need to be developed if reuse is not to be compromised. • A software that is easy to use is hard to build. It is hoped that the object- oriented programming languages like C++ and Java would help manage this problem.
  • 6. Object-Oriented Languages • Object-oriented language was primarily designed to reduce complexity in typical procedural languages through data binding and encapsulation techniques. • In object-oriented language, the objects created provide limited or no access to other functions or methods within the program. • This enables only authorized or inherited methods/functions to access a particular object. • Object-oriented language typically supports the following features, at minimum: • The ability to create classes and their associated objects • Encapsulation • Inheritance • Java, C++ and Smalltalk are popular examples of object-oriented languages.
  • 7. Application of OOPs in C++ • It is about creating objects that contain both data and functions. Object-Oriented programming has several advantages over procedural languages. • As OOP is faster and easier to execute it becomes more powerful than procedural languages like C++. • OOPs is the most important and flexible paradigm of modern programming. It is specifically useful in modeling real-world problems. • Below are some applications of OOPs: • Real-Time System design: Real-time system inherits complexities and makes it difficult to build them. OOP techniques make it easier to handle those complexities. • Hypertext and Hypermedia: Hypertext is similar to regular text as it can be stored, searched, and edited easily. Hypermedia on the other hand is a superset of hypertext. OOP also helps in laying the framework for hypertext and hypermedia.
  • 8. • AI Expert System: These are computer application that is developed to solve complex problems which are far beyond the human brain. OOP helps to develop such an AI expert System • Office automation System: These include formal as well as informal electronic systems that primarily concerned with information sharing and communication to and from people inside and outside the organization. OOP also help in making office automation principle. • Object-oriented database: The databases try to maintain a direct correspondence between the real world and database object in order to let the object retain it identity and integrity. • Client-server system: Object-oriented client-server system provides the IT infrastructure creating object-oriented server internet(OCSI) applications. • CIM/CAD/CAM systems: OOP can also be used in manufacturing and designing applications as it allows people to reduce the efforts involved. For instance, it can be used while designing blueprints and flowcharts. So it makes it possible to produce these flowcharts and blueprint accurately.
  • 9. C++ Tokens • C++ Tokens are the smallest individual units of a program. • C++ is the superset of C and so most constructs of C are legal in C++ with their meaning and usage unchanged. So tokens, expressions, and data types are similar to that of C. • Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens) • Keywords • Identifiers • Constants
  • 10. Keywords • Keywords are reserved words which have fixed meaning, and its meaning cannot be changed. • The meaning and working of these keywords are already known to the compiler. • C++ has more numbers of keyword than C, and those extra ones have special working capabilities. • There are 32 of these, and here they are • auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
  • 11. While in C++ there are 31 additional keywords other than C Keywords they are: asm bool catch class const_cast delete dynamic_cast explicit export false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t
  • 12. Identifiers • Identifiers are used as the general terminology for the naming of variables, functions and arrays. • These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character. • Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers; they are reserved for special use. • Once declared, you can use the identifier in later program statements to refer to the associated value. • A special kind of identifier, called a statement label, can be used in goto statements. • There are certain rules that should be followed while naming c identifiers:
  • 13. • They must begin with a letter or underscore(_). • They must consist of only letters, digits, or underscore. No other special character is allowed. • It should not be a keyword. • It must not contain white space. • It should be up to 31 characters long as only the first 31 characters are significant. • main: method name. • a: variable name.
  • 14. Constants • Constants are also like normal variables. But, the only difference is, their values can not be modified by the program once they are defined. Constants refer to fixed values. • They are also called literals. • Constants may belong to any of the data type Syntax: • const data_type variable_name; (or) const data_type *variable_name; • Types of Constants: • Integer constants – Example: 0, 1, 1218, 12482 • Real or Floating-point constants – Example: 0.0, 1203.03, 30486.184 • Octal & Hexadecimal constants – Example: octal: (013 )8 = (11)10, Hexadecimal: (013)16 = (19)10 • Character constants -Example: ‘a’, ‘A’, ‘z’ • String constants -Example: “Hello world”
  • 15. Character Constant • Character constants have either a single character or group of characters or a character with backslash used for special purpose. These are further subdivided into three types: • (i) Single Character Constant • (ii) String Character Constant • (iii) Backslash Character Constant • (i) Single Character Constant • Single Character constants are enclosed between single quotes(‘). For example, ‘a’ and ‘%’ are both character constants. So these are also called single quote character constant. • For example: ‘a’, ‘M’, ‘5’, ‘+’, ‘1’ etc. are some valid single character constant.
  • 16. String Character Constant • C++ supports another type of constant: the string. A string is a set of characters enclosed in double quotes (“). For example: “Punar Deep” is a string. • You must not confuse strings with characters. A single character constant is enclosed in single quotes, as in ‘a’. However, “a” is a string containing only one letter. • For example: “Dinesh”, “Hello”, “2013”, “2013-2020”, “5+3”, “?+!” etc. are some valid string character constant. These are used for printing purpose or display purpose in the C++ program’s output statements. These can also be used for assigning the string data to the character (string) type variables..
  • 17. Backslash Character Constants • Enclosing character constants in single quotes works for most printing characters. A few, however, such as the carriage return, can’t be. For this reason, C++ includes the special backslash character constants, shown below, so that you may easily enter these special characters as constants. • These are also referred to as escape sequences. You should use the backslash codes instead of their ASCII equivalents to help ensure portability. • These are used for special purpose in the C++ language. These are used in output statements like cout etc.
  • 19. • Object-Oriented Programming C++ is an Object-Oriented Programming Language, unlike C which is a procedural programming language. This is the most important feature of C++. It can create/destroy objects while programming. Also, It can create blueprints with which objects can be created. • Machine Independent A C++ executable is not platform-independent (compiled programs on Linux won’t run on Windows), however they are machine independent. Suppose you have written a piece of code which can run on Linux/Windows/Mac OSx which makes C++ Machine Independent but the executable file of the C++ cannot run on different operating systems. • Simple It is a simple language in the sense that programs can be broken down into logical units and parts, has rich library support, and a variety of data-types. • High-Level Language C++ is a High-Level Language, unlike C which is a Mid-Level Programming Language.
  • 20. • Popular C++ can be the base language for many other programming languages that supports the feature of object-oriented programming. • Case-sensitive It is clear that the C++ is a case-sensitive programming language. For example, cin is used to take input from the input stream. • Compiler Based C++ is a compiler-based language, unlike Python. That is C++ programs used to be compiled and their executable file is used to run it. • Dynamic Memory Allocation When the program executes in the C++ then the variables are allocated the dynamical heap space. Inside of the functions the variables are allocated in the stack space.
  • 21. • Memory Management • C++ allows us to allocate the memory of a variable or an array in run time. This is known as Dynamic Memory Allocation. • In other programming languages such as Java and Python, the compiler automatically manages the memories allocated to variables. But this is not the case in C++. • In C++, the memory must be de-allocate dynamically allocated memory manually after it is of no use. • The allocation and deallocation of the memory can be done using the new and delete operators respectively. • Multithreading Multithreading is a specialized form of multitasking and multitasking is a feature that allows your system to execute two or more programs concurrently. In general, there are two sorts of multitasking: process-based and thread-based. • Process-based multitasking handles the concurrent execution of programs. Thread- based multitasking deals with the multiprogramming of pieces of an equivalent program. • A multithreaded program contains two or more parts that will run concurrently.
  • 22. Structure of a C+ + Program
  • 23. Simple C++ program without class 1) // Comments: A comment is used to display additional information about the program. A comment does not contain any programming logic. When a comment is encountered by a compiler, the compiler simply skips that line of code. Any line beginning with ‘//’ without quotes OR in between /*…*/ in C++ is comment. 2) #include: In C++, all lines that start with (#) sign are called directives and are processed by a preprocessor which is a program invoked by the compiler. The #include directive tells the compiler to include a file and #include<iostream>. It tells the compiler to include the standard iostream file which contains declarations of all the standard input/output library functions. 3) using namespace std: This is used to import the entirety of the std namespace into the current namespace of the program. When we import a namespace we are essentially pulling all type definitions into the current scope. The std namespace is huge.
  • 24. 4) int main(): This line is used to declare a function named “main” which returns data of integer type or void. Which is knows as a return type. A function is a group of statements that are designed to perform a specific task. Execution of every C++ program begins with the main() function, no matter where the function is located in the program. So, every C++ program must have a main() function. 5) { and } : The opening braces ‘{‘ indicates the beginning of the main function and the closing braces ‘}’ indicates the ending of the main function. Everything between these two comprises the body of the main function. 6) cout<<“Hello Class”; : This line tells the compiler to display the message “Hello World” on the screen. A semi-colon ‘;’ is used to end a statement. Semi-colon character at the end of the statement is used to indicate that the statement is ending there. Everything followed by the character “<<” is displayed to the output device. This is a statement.
  • 25. 7) return 0; : This is also a statement. This statement is used to return a value from a function and indicates the finishing of a function. This statement is basically used in functions to return the results of the operations performed by a function. 8) cin>>a; : The cin object in C++ is an object of class iostream. It is used to accept the input from the standard input device i.e. keyboard. It is associated with the standard C input stream stdin. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. Important Points to Note while Writing a C++ Program: Always include the necessary header files for the smooth execution of functions. For example, <iostream> must be included to use std::cin and std::cout. The execution of code begins from the main() function. It is a good practice to use Indentation and comments in programs for easy understanding. cout is used to print statements and cin is used to take inputs.
  • 26. Basic data types in C++ • Data types specify the type of data that a valid C# variable can hold. C# is a strongly typed programming language because in C#, each type of data (such as integer, character, float, and so forth) is predefined as part of the programming language. • Data types in C# is mainly divided into three categories • Value Data Types • Reference Data Types • Pointer Data Type
  • 27. Value Data Type :- • The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literals. • There are 2 types of value data type in C++ language. • 1) Predefined Data Types - such as Integer, Boolean, Float, etc. • 2) User defined Data Types - such as Structure, Enumerations, etc. • The memory size of data types may change according to 32 or 64 bit operating system. • Let's see the value data types. It size is given according to 32 bit OS.
  • 28. Data Types Memory Size Range char 1 byte -128 to 127 signed char 1 byte -128 to 127 unsigned char 1 byte 0 to 127 short 2 byte -32,768 to 32,767 signed short 2 byte -32,768 to 32,767 unsigned short 2 byte 0 to 65,535 int 4 byte -2,147,483,648 to -2,147,483,647 signed int 4 byte -2,147,483,648 to -2,147,483,647 unsigned int 4 byte 0 to 4,294,967,295 long 8 byte ?9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • 29. • In c++, a data type is a Value Type if it holds the value of variable directly on its own memory space and Value Types will use Stack memory to store the values of the variables. • For example, if we define and assign a value to the variable like int x = 123; then the system will use the same memory space of variable ‘x’ to store the value ‘123’. • Following is the pictorial representation of value types in c++ programming language.
  • 31. Reference Data Type :- • The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables. • If the data is changed by one of the variables, the other variable automatically reflects this change in value. • There are 2 types of reference data type in C++ language. • 1) Predefined Types - such as Objects, String. • 2) User defined Types - such as Classes, Interface.
  • 32. • In c++, Reference Types will contain a pointer which points to other memory location that holds the data. The Reference Types won’t store the variable value directly in its memory instead, it will store the memory address of the variable value to indicate where the value is being stored. • For example, if we define and assign a value to the variable like string name = "Suresh Dasari"; then the system will store the variable value “Suresh Dasari” in one location and the variable "name" in another location along with the memory address of the variable value. • Following is the pictorial representation of reference type in c++ programming language.
  • 34. Pointer Data Type :- • The pointer in C++ language is a variable, it is also known as locator or indicator that points to an address of a value. • Symbols used in pointer :- Symbol Name Description & (ampersand sign) Address operator Determine the address of a variable. * (asterisk sign) Indirection operator Access the value of an address.
  • 35. • Declaring a pointer :- • The pointer in C# language can be declared using * (asterisk symbol). • int * a; //pointer to int • char * c; //pointer to char
  • 36. C++ Variables • Variables in C++ is a name given to a memory location. It is the basic unit of storage in a program. • The value stored in a variable can be changed during program execution. • A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. • In C++, all the variables must be declared before use. Rules For Declaring Variable The name of the variable contains letters, digits, and underscores. The name of the variable is case sensitive (ex Arr and arr both are different variables). The name of the variable does not contain any whitespace and special characters (ex #,$,%,*, etc). All the variable names must begin with a letter of the alphabet or an underscore(_). We cannot used C++ keyword(ex float,double,class)as a variable name.
  • 37. • How to Declare Variables? • A typical variable declaration is of the form: // Declaring a single variable type variable_name; // Declaring multiple variables: • type variable1_name, variable2_name, variable3_name; • A variable name can consist of alphabets (both upper and lower case), numbers, and the underscore ‘_’ character. However, the name must not start with a number. • datatype: Type of data that can be stored in this variable. • variable_name: Name given to the variable. • value: It is the initial value stored in the variable.
  • 39. Type Conversion in C++ • A type cast is basically a conversion from one type to another. There are two types of type conversion: • Implicit Type Conversion Also known as ‘automatic type conversion’. • Done by the compiler on its own, without any external trigger from the user. • Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data. • All the data types of the variables are upgraded to the data type of the variable with largest data type.
  • 40. • Explicit Type Conversion: This process is also called type casting and it is user- defined. Here the user can typecast the result to make it of a particular data type. • In C++, it can be done by two ways: • The word “explicit” means ‘open’ or ‘clear’. In explicit C++ type casting, the data type in which the value is to be converted is clearly specified in the program. It is done by cast operator. The cast operator is unary operator. It converts the value of an expression into a value of the type specified. • The general form of the C++ type casting operator is as follows; • (type)expression Where, • (type): it indicates one of the C++ data type (or a user-defined data type) to which the value of the expression is to be converted. • Expression: it indicates a constant value, variable or an expression whose data type is to be converted.
  • 41. Eg. int x; x=(int)2.322; cout<<x; After executing the above statements, the floating-point value will be converted into integer. The value assigned to variable ‘x’. the decimal portion will be truncated( or removed. The value 2 will be displayed on the screen. Implicit C++ Type Casting: The word “implicit” means ‘understood’ or ‘embedded’. In implicit C++ type casting, the data type in which the value is to be converted is not specified in the program. It is automatically done by the C++ compiler. When constant values and variables of different types are mixed in an expression, they are converted into the same type. The compiler will convert all operands of lower order data type into higher order data type.
  • 42. • Implicit Type Conversion Also known as ‘automatic type conversion’. • Done by the compiler on its own, without any external trigger from the user. • Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data. • All the data types of the variables are upgraded to the data type of the variable with largest data type. • bool -> char -> short int -> int -> • unsigned int -> long -> unsigned -> • long long -> float -> double -> long double
  • 43. C++ Operators • Operators are used to perform operations on variables and values. • An operator is a symbol that operates on a value to perform specific mathematical or logical computations. • They form the foundation of any programming language. In C++, we have built-in operators to provide the required functionality. • Operators in C++ can be classified into 6 types: • Arithmetic Operators • Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators • Ternary or Conditional Operators
  • 44. Operators Precedence in C++ • Operator precedence determines the grouping of terms in an expression. • The associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. • This affects how an expression is evaluated. • Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator:
  • 45. Category Operator Associativity Postfix () [] -> . ++ - - Left to right Unary + - ! ~ ++ - - (type)* & sizeof Right to left Multiplicative * / % Left to right Additive + - Left to right Shift << >> Left to right Relational < <= > >= Left to right Equality == != Left to right Bitwise AND & Left to right Bitwise XOR ^ Left to right Bitwise OR | Left to right Logical AND && Left to right Logical OR || Left to right Conditional ?: Right to left Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left Comma , Left to right
  • 46. Decision Making, Loops, Arrays and Strings: • The if Statement :- • Use the if statement to specify a block of C++ code to be executed if a condition is true. • Syntax • if (condition) { • // block of code to be executed if the condition is true • } • The if_else Statement :- • Use the else statement to specify a block of code to be executed if the condition is false. • Syntax • if (condition) { • // block of code to be executed if the condition is true • } else { • // block of code to be executed if the condition is false • }
  • 47. • The else if Statement :- • Use the else if statement to specify a new condition if the first condition is false. • Syntax • if (condition1) { • // block of code to be executed if condition1 is true • } else if (condition2) { • // block of code to be executed if the condition1 is false and condition2 is true • } else { • // block of code to be executed if the condition1 is false and condition2 is false • }
  • 48. • C++ Switch Statements :- • Use the switch statement to select one of many code blocks to be executed. • Syntax • switch(expression) { • case x: • // code block • break; • case y: • // code block • break; • default: • // code block • } • This is how it works: • The switch expression is evaluated once • The value of the expression is compared with the values of each case • If there is a match, the associated block of code is executed • The break and default keywords are optional.
  • 49. • C++ Loops • Loops can execute a block of code as long as a specified condition is reached. • Loops are handy because they save time, reduce errors, and they make code more readable. • C++ While Loop • The while loop loops through a block of code as long as a specified condition is true: • Syntax • while (condition) { • // code block to be executed • }
  • 50. • The Do/While Loop • The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true. • Syntax • do { • // code block to be executed • } • while (condition); • C++ For Loop • When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: • Syntax • for (Initialization; condition; Iteration) { • // code block to be executed • }
  • 51. C++ Arrays • Like other programming languages, array in C++ is a group of similar types of elements that have contiguous memory location. • In C++ std::array is a container that encapsulates fixed size arrays. In C++, array index starts from 0. We can store only fixed set of elements in C++ array.
  • 52. • Advantages of C++ Array • Code Optimization (less code) • Random Access • Easy to traverse data • Easy to manipulate data • Easy to sort data etc. • Disadvantages of C++ Array • Fixed size • C++ Array Types • There are 2 types of arrays in C++ programming: 1.Single Dimensional Array 2.Multidimensional Array
  • 53. C++ Single Dimensional Array #include <iostream> using namespace std; int main() { int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array //traversing array for (int i = 0; i < 5; i++) { cout<<arr[i]<<"n"; } }
  • 54. C++ Multidimensional Arrays • The multidimensional array is also known as rectangular arrays in C++. It can be two dimensional or three dimensional. The data is stored in tabular form (row ∗ column) which is also known as matrix.
  • 55. include <iostream> using namespace std; int main() { int test[3][3]; //declaration of 2D array test[0][0]=5; //initialization test[0][1]=10; test[1][1]=15; test[1][2]=20; test[2][0]=30; test[2][2]=10; //traversal for(int i = 0; i < 3; ++i) { for(int j = 0; j < 3; ++j) { cout<< test[i][j]<<" "; } cout<<"n"; //new line at each row } return 0; }
  • 56. String and String manipulations • C++ supports a wide range of functions that manipulate null-terminated strings. These are: • strcpy(str1, str2): Copies string str2 into string str1. • strcat(str1, str2): Concatenates string str2 onto the end of string str1. • strlen(str1): Returns the length of string str1. • strcmp(str1, str2): Returns 0 if str1 and str2 are the same; less than 0 if str1<str2; greater than 0 if str1>str2. • strchr(str1, ch): Returns a pointer to the first occurrence of character ch in string str1. • strstr(str1, str2): Returns a pointer to the first occurrence of string str2 in string str1.
  • 57. Operators used for String Objects Operator Meaning = Assignment + Concatenation == Equality != Inequality < Less than <= Less than or equal > Greater than >= Greater than or equal [] Subscription << Output >> Input
  • 58. dot (.) operator in C/C++ • The dot (.) operator is used for direct member selection via object name. • In other words, it is used to access the child object. Syntax: • object.member; • The dot and arrow operator are both used in C++ to access the members of a class. • They are just used in different scenarios. • In C++, types declared as a class, struct, or union are considered "of class type".
  • 59. • Eg. #include <stdio.h> struct Point { int x, y; }; int main() { struct Point p1 = { 0, 1 }; // Accessing members of point p1 // using the dot operator p1.x = 20; Cout<<"x = %d, y = %d", p1.x, p1.y; return 0; }
  • 60. Data members and Member functions in C++ • "Data Member" and "Member Functions" are the new names/terms for the members of a class, which are introduced in C++ programming language. • The variables which are declared in any class by using any fundamental data types (like int, char, float etc) or derived data type (like class, structure, pointer etc.) are known as Data Members. And the functions which are declared either in private section of public section are known as Member functions. • There are two types of data members/member functions in C++: • There are two types of data members/member functions in C++: 1.Private members 2.Public members
  • 61. 1) Private members • The members which are declared in private section of the class (using private access modifier) are known as private members. Private members can also be accessible within the same class in which they are declared. 2) Public members • The members which are declared in public section of the class (using public access modifier) are known as public members. Public members can access within the class and outside of the class by using the object name of the class in which they are declared.