0% found this document useful (0 votes)
65 views98 pages

History of C++

The document discusses various C++ concepts like functions, classes, structures, object oriented programming principles, and differences between C and C++. It covers topics like function definition and declaration, inline functions, function overloading, default arguments, structures, classes, access specifiers, static variables and functions.

Uploaded by

Pubg Pubg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views98 pages

History of C++

The document discusses various C++ concepts like functions, classes, structures, object oriented programming principles, and differences between C and C++. It covers topics like function definition and declaration, inline functions, function overloading, default arguments, structures, classes, access specifiers, static variables and functions.

Uploaded by

Pubg Pubg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 98

C++

ANSI standards for c++ language says explicit declaration of a function is compulsory

#include<iostram> is pre-processing directives.

endl- it mean end line.

History of C++
C++ was developed by Bjarne Stroustrup at Bell Laboratories over a period starting in 1979

C++ is a middle level language

C++ supports principles of object-oriented paradigm

C++ joins three separate programming traditions

 the procedural language traditions, represented by C;


 the object-oriented language traditions, represented by the class enhancements c++ add to c;
 generic programming, supported by c++ templates

Comparison between c and C++


 C++ is a super set of C language
 C++ programs can use existing C software libraries
 C follows top-down approach of programming
 C++ follows bottom-up approach of programming
 C adopts Procedure Oriented Programming
 C++ adopts Object Oriented Programming

Object Oriented Programming


OOPs is a programming approach which revolves around the concept of “Object”.

 Encapsulation
 Data Handling
 Abstraction
 Polymorphism
 Inheritance

Concept of Classes and Objects


Class is a blueprint of an Object

Class is a description of Object’s property set and set of operations

Creating Class is as good as defining a new data type

class is a means to achieve encapsulation

Object is a run time entity

Object is an instance of a class


Header File
Predefined functions are declared in header files, so whenever you are using any predefined function in your code, you have
to include specific header file that contains its declaration.

iostream.h
We need to include header file iostream.h, it contains declarations for the identifier cout and the operator <<. And also for
the identifier cin and the operator >>.

Header file contains declaration of identifiers

Identifiers can be function name, variables, objects, Macros etc.

endl
Inserting endl into the output stream cause the screen cursor to move to the beginning of the next line

endl is a manipulator and it is declared in iostream.h

‘\n’ character also works as it works in c

Reference Variable
Reference means address

Reference variable is an internal pointer

Declaration of Reference variables preceded with ‘&’ symbol (but do not read is as ‘address of’)s

Reference variable must be initialized during declaration.

It can be initialized with already declared variable only

Reference variables can not be updated


Function in c++

What is Function
 Function is block of code performing a unit task.
 Function has a name, return type and arguments.
 Function is a way to achieve modularization
 Functions are Predefined and user-defined
 Predefined functions are declared in header files and defined in library files

Definition, Declaration and Call

Declaration
 Function Declaration is also known as Function prototype
 Function needs to be declared before use (just like variable)
 Function can be declared locally or globally
 Return Type functionName (argumentList)
 Function definition is a block of code

Ways to define a function


 Takes Nothing, Returns Nothing
 Takes Something, Returns Nothing
 Takes Nothing, Returns Something
 Takes Something, Returns Something

Formal and Actual Arguments

Types of Formal Arguments


 Formal arguments can be of three types
1. Ordinary variables of any type
2. Pointer variables
3. Reference variables

Call by Value

Call by address
Call by reference

Benefits of function
 Easy to read
 Easy to modify
 Avoids rewriting of same code
 Easy to debug
 Better memory utilization
 Function in a program is to save memory space which become appreciable when a function is likely to be called many
times
 However, every time a function is called, it takes lot of extra time executing a series of instructions for takes such as
jumping to the function, saving registers, pushing arguments into the stack and returning to the calling function.
 So when function is small it is small it is worthless to spend so much extra time in such takes in cost of saving
comparatively small space

Inline function
 To eliminate the cost of calls to small functions, C++ proposes a new feature called Inline function
 An Inline function is a function that is expanded in line when it is invoked
 Compiler replaces the function call with the corresponding function code.

Inline is a request
 Inline is a request not a command.
 The benefit of speed of inline functions reduce as the function grows in size.
 So, the compiler may ignore the request in some situations.
 Few of them:
o Function contains loops switch, goto.
o Functions with recursion
o Containing static variable

Default Arguments

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling
function doesn’t provide a value for the argument. In case any value is passed, the default value is overridden.
Function Overloading
How Function Overloading is Resolved
1. Fist C++ tries to find an exact match. This is the case where the actual argument exactly matches the parameter type
of one of the overloaded functions.
2. If no exact match is found, C++ tries to find a match through promotion
o Char, unsigned char, and shot is promoted to an int.
o Float is promoted to double
3. If no promotion is found, C++ tries to find a match through standard conversion

Structure in C++

Three important points


1. Structure is collection of dissimilar elements
2. Structure is a way to group variables
3. Structure is used to create data type
Taking input from the user 1st way to not good way because this program not follow OOPs guideline

#include <iostream>

#include<cstring>

using namespace std;

struct person{

char name[40];

int age;

char blood_group[3];

};

void show(person); //define

person input(); //define

int main()

person p1;

p1=input(); // input function call

cout<<"Print"<<endl;

show(p1);

return 0;

// Input from user

person input(){

person p;

cout<<"Enter name, age, and blood_group: ";

cin>>p.name>>p.age>>p.blood_group;

return (p);

// data Display fucntion

void show(person p){

cout<<p.name<<endl<<p.age<<endl<<p.blood_group;

}
2nd way using OOPs guidance

#include <iostream>

#include<cstring>

using namespace std;

struct person{

char name[40];

int age;

char blood_group[3];

void input(){

cout<<"Enter name, age, and blood_group: ";

cin>>name>>age>>blood_group;

void show(){

cout<<name<<endl<<age<<endl<<blood_group;

};

int main()

person p1;

p1.input();

p1.show();

return 0;

3rd or most best way to store , this you can secure your data jo no one access structure member by using there name

In C++, there are three access specifiers:

 public - members are accessible from outside the class


 private - members cannot be accessed (or viewed) from outside the class
 protected - members cannot be accessed from outside the class, however, they can be
accessed in inherited classes.
#include <iostream>

#include<cstring>

using namespace std;

struct person{

private:

char name[40];

int age;

char blood_group[3];

public:

void input(){

cout<<"Enter name, age, and blood_group: ";

cin>>name>>age>>blood_group;

void show(){

cout<<name<<endl<<age<<endl<<blood_group;

};

int main()

person p1;

p1.input();

p1.show();

return 0;

Class and structure difference


The only difference between structure and class is that

 the members of structures are by default public and


 the members of class are by default private.

Class does not consumed memory object consumed memory


We can also define member function outside of class by this way.
Complex number addition
Technical jargons

Static Local Variable


Concept it is taken form C

They are by default initialized Zero

Their lifetime is throughout the program

Static Member Variable


 Declared inside the class body
 Also known as class member variable
 They must be defined outside the class
 Static member variable does not belong to any object, but to the whole class.
 There will be only one copy of static member variable for the whole class
 Any object can use the same copy of class variable
 They can also be used the class name

1st way to
2nd way

Static void setroi hoga


static

Static Member Function


 They are qualified with the keyword static
 They are also called class member function
 They can be invoked with or without object
 They can only access static members of the class
Constructor
 Constructor is a member function of a class.
 The name of the constructor is same as the name of the class
 It has no return type, so can’t use return keyword.
 It must be an instance member function, that is, it can never be static.

How to call Constructor


 Constructor is implicitly invoked when an object created.
 Constructor is used to solve problem of initialization.

The class

Access specifier

Constructor

As many objects are there will be times print (c1,c2,c3 these are objects).
And object ke bante hi call ho jata hai upper screenshot mai example hai .

IMPORTANT POINTS
 A constructor in C++ is a special method that is automatically called when an object of a class is
created.
 To create a constructor, use the same name as the class, followed by parentheses ():
 If their programmer not created any constructor and try to call it (without parameter) the compiler
not gives error because by default constructor was created and also copy constructor

Copy Constructor
Destructor
 Destructor is an instance member function of a class.
 The name of the destructor is same as the name of a class but preceded by tilde (`~’) symbol.
 Destructor can never be static.
 Destructor has no return type.
 Destructor takes no argument (No overloading is possible).
 It is invoked implicitly when object is going to destroy.

Why destructor?
 It should be defined to release resources allocated to an object.

Operator overloading
 When an operator is overloaded with multiple jobs, it is known as operator overloading.
 It is a way to implement compile time polymorphism.

Rules to Remeber
 Any symbol can be used as function name
o If it is a valid operator in C language
o If it is preceded by operator keyword
 You can not overload sizeof and ?: operator

Overloading of binary operator


You can use operator symbol as a function name but you can first type operator than operator symbol
For Example:

1st way is c3=c1.operator+(c2) or 2nd way you can also write like this c3=c1+c2;

1st way example

2nd way this is best way


Urinary operator
Overloading of unary operator ++(Pre and Post)

For Pre increment do not use argument write like this “class name operator++()”
For post increment use argument write like this “class name operator++(int)”
Friend Function

 Friend function can access any member of the class to which it is friend
 Friend function cannot access members of the class directly
 It has no called object
 It should not be defined with membership label
 Friend function can become friend to more than one class

 Overloading of operators as a friend function


 This bottom code is in the form of binary operator

 Now using unary operator as a friend function


 Overloading of insertion and extraction operator
 Member function of one class can become friend to another class
o There are two ways
o If you want one function then write like this friend function name jaha
se chaiye us class ka name example

o
o If you want all function of class to become another class friend function
then write like this
o

Inheritance

Understand the need of Inheritance

Definition- Inheritance is the capability of one class of things to inherit


capabilities or properties from another class.
Existing class = old class = parent class = base class
New class = child class derived class
Syntax
Class base_class{

};
Class Derived_Class: Visibility_Mode base_class{

};
 Class is used to describe properties and behaviour of an object.
 Property names and values

Types of Inheritance

 Single Inheritance
 Multilevel Inheritance

 Multiple Inheritance
 Hierarchical Inheritance

 Hybrid Inheritance

Visibility Modes
Example:
isme private value inherit hua hai but accessible nhi hai

Is a relationship

Is a relationship is always implemented as a public inheritance


In above program Stack can access insert function of array class
But stack object can’t access insert function.

Constructor and Destructor in Inheritance

We know that constructor is invoked implicitly when an object is created.

Constructor pahle parent class ka run hota hai than child class ka
Assign value in parent class using constructor.
Destructor first child class ka run hota hai than parent class ka

Object Pointer

A pointer contains address of an object is called Object pointer.

Now using with pointer --


If you want access object member using pointer than you not use. use “-> arrow
operator.”
//p is pointer

This pointer

 this is a keyword
 This is a local object pointer in every instance member function containing
address of called object.
 This pointer cannot be modify
 It is used to refer called object in member function.
SMA Vs DMA

 SMA: Static Memory Allocation


 DMA: Dynamic Memory Allocation

For creating DMA, you need to use ‘new’ keyword.


Delete: With the help of the delete keyword, we can get the memory
released from the new, which is created with the help of a new keyword.

Method Overriding
 It redefines a function of the base class inside the derived class, which
overrides the base class function. Function overriding is an implementation
of the run-time polymorphism. So, it overrides the function at the run-time
of the program
 As we know, inheritance is a feature of OOP that allows us to create derived
classes from a base class. The derived classes inherit features of the base
class.
 Suppose, the same function is defined in both the derived class and the
based class. Now if we call this function using the object of the derived class,
the function of the derived class is executed.
 This is known as function overriding in C++. The function in derived class
overrides the function in base class

Method Overriding

Base class Pointer


 Base class pointer can point to the object of any of its descendant class.
Example: -

 But its converse is not true.


If you want to pointer point B class then write
virtual keyword at class A f1 function
To do late binding, the "virtual" keyword is written, which binds at run time.
Example:

Where: - * vptr created by compiler


EB means early binding
LB means Late binding

Abstract class

In class, any pure virtual function is present then that class will be called Abstract
class
 A class containing pure virtual function is an abstract class
 We can not instantiate abstract class
Pure Virtual Function

 A do-nothing function is pure virtual function


 Such classes which have to do Nothing function cannot be created objected.

Template

 The keyword template is used to define function template and class


template
 It is a way to make your function or class generalize as far data type is
concern

Template

 Function Template
Example: - 👇

 Class Template

File Handling

Strems
ifstream = input stream – we use
ifstream when we need to read data
from the file.
ofstream = output stream- we use
ofstream when we need to write data
in the file
fstream:- fstream can do both work
reading and writing both

Creating file Example: - 👇


Opening File Example: - 👇

Opening String Space File Example: - 👇


Example: -
File Opening Modes

ios::in :- this is use from input/read

ios::out :- this is user for output/write.

ios::app :- If you want to add more data on file without erase previous data
then use this.
Text mode vs Binary mode

tellg()
 Defined in ostream class
 Its prototype is
o -streampos tellg();
 Returns the position of the current character in the output stream.
Example of tellg (); – we use tellg with ifstream.

input file ‘h’ index 0 so tellg return 0

Output

tellp()

 Defined in ostream class


 Its prototype is
o -streampos tellp()
 Returns the position of the current character in the ouput stream.
Example of tellp (); - We use tellp() with ofstream

After append new word “suraj” here ios::app means append


Output

seekg and seekp

Seekg()
We use seekg() to point tellg at any position
 Deined in istream class
 Its prototype is
o -istream & seekp(streampos pos);
o -istream & seekp(streamoff off, ios_base::seekdir way);
 pos is new absolute position within the stream (relative to the beginning).
 off is offset value, relative to the way parameter
 way value ios_base::beg, ios_base::cur and ios_base::end
Example

Output is
If we use like this fin.seekg(2,ios_base::cur) it mean current position se 2 aage
chale jao Example

Output is:-

seekp();

 Defined in ostream class


 Its prototype is
o – ostream & seekp(streampos pos);
o – ostream & seekp(streamoff, ios_base::seekdirway);
 pos is new absolute position within the stream( relative to the beginning).
 off is offset value, relative to the way parameter
 way value ios_base::beg, ios_base::cur and ios_base::end
Example
Output is:

Initializers

 Initializer List is used to initialize data members of class.


 The list of members to be initialized is indicated with constructor as a
comma separated list followed by a colon.

We use Initializers when variable is const Example:-


And we also use when we declare reference variable example:-

 There are situations where initialization of data members inside


constructor doesn’t work and Initializer List must be used.
o For initialization of non-static const data members
o For initialization of reference members

Deep Copy and Shallow Copy


Output of code:
Type Conversion Primitive to class

 Int, char, float double are primitive types


 Class type is any class you defined
This can be solve like this
figure in below

Output of code:
 Primitive type to class type can be implemented through constructor

Type Conversion Class type to primitive

This can be solved like this figure in below

Example: -
Output of code:

Type Conversion one Class type to another class


Output of code:

Exception Handling

Exceptions
 Exception is any abnormal behaviour, run time error.
 Exceptions are off beat situation in your program where your program
should be ready to handle it with appropriate response

Exception Handling
 C++ provides a built-in error handling mechanism that is called exeption
handling
 Using exception handline. You a more easily manage and respond to runtime
errors.

try, catch, throw

 Program statements that you want to monitor for exceptions are contained
in a try block.
 If any exception occurs within the try block, it is thrown (using throw).
 The exception is caught, using catch, and processed
Simple Example

Output of code:

Some important thing


 If throw is int than catch data type should be int
 Or if there is to catch one data type double and another data type int then
program will run example: -
Output of code:

 this code also run and output is:


You can also throw through function see below image

Output of code:

Catch
 When an exception is caught, arg will receive its value.
 If you don’t need access to the exception itself, specify only type in the catch
clasuse-arg is optional.
 Any type of data can be caught, including classes that you create.

Throw

 The general from of the throw statement is: throw exception;


 Throw must be executed either within the try block proper or from any
function that the code within the block calls.

Dynamic Constructor

 Constructor can allocate dynamically created memory to the object


 Thus, object is going to use memory region, which is dynamically created by
constructor
 Example: -
namespace

 Namespace is a container for identifiers


 It puts the name of its members in a distinct space so that they don’t conflict
with the names in other namespace or global namespace
 Namespace is not a class, you cannot create instance of namespace

How to create namespace?

Namespace can be extended


Example:

Output of code:
Accessing members of namespace

 Any name (identifier) declared in a namespace can be explicitly specified


using the namespace’s name and the scope resolution :: operator with the
identifier.

The using directive

 using keyword allows you to import an entire namespace into your program
with a global scope
 It can be used to import a namespace into another namespace or any
program
Nested Class

 Class inside a class called nested class


 A nested class is a member and as such has the same access rights as any
other member
 The member of an enclosing class has no special access to members of a
nested class; the useful access rules shell be obeyed.
Example: -
Output of code:

Nested class

 Class inside a class is called nested class


 A nested class is member and as such has the same access rights as any
other member
 The member of an enclosing class have no special access to members of a
nested class; the usual access rules shell be obeyed

Introduction to STL

 STL is Standard Template Library


 It is powerful set of C++ template classes
 At the core of the c++ Standard Template Library are following three well-
structured components
o Containers
o Algorithms
o Iterators

Containers

 Containers are used to manage collection of objects of a certain kind.


 Container library in STL provide containers that are used to create data
structures like arrays, linked list, tree etc
 These containers are generic, they can hold elements of any data types
Example: -
 Vector can be used for creating dynamic arrays of char, int, float, and
other types

Algorithms

 Algorithms act on containers. They provide the means by which you will
perform initialization, sorting, searching, and transforming of the contents of
containers.
 Algorithms library contains built in function that performs complex
algorithms on the data structures
Iterators

 Iterators are used to step through the elements of collections of objects.


These collection may be containers or subject of containers.
 Iterators in STL are used to point the containers
 Iterators actually acts as a bridge between containers and algorithms

STL Containers

Containers

 Container library is a collection of classes


 These containers are implemented as generic class templates
 Containers help us to implement and replicate simple and complex data
structures very easily like arrays, list, tress, associative arrays and many
more.
 Containers can be used to hold different kind of object.

Common Containers

 vector: replicates arrays


 queue: replicates queues
 stack: replicates heaps
 priority_queue: replicates heaps
 list: replicates linked list
 set: replicates trees
 map: associative arrays

Classification of Containers

How to use container


Library ?
 When we use list containers to implement linked list, we just have o include
the list header file and use list constructor to initialize the list
Array in STL

Array

 Array is a linear collection of similar elements.


 Array container in STL provides us the implementation of static array
 Use header array
o #include<array>

Creating array objects


 array <object_type, array_size>array_name;
 It creates an empty array of object_type with maximum size of array_Size

Member functions
 Following are the important and most used member function of array
template
 at: -
o This method returns value in the array at given range
o If the given range is greater than the array size.out_of_range exception is
thrown
Example: -
Output of code:

 [] operator
o The use of operator [] is same as it was for normal arrays
o Example: -

Output of code:

 front()
o front() method returns the first element in the array
o Example: -

Output of code:

 back()
o back() method returns the last element in the array
o Example: -
Output of code:

 fill()
o This method assign the given value to every element of the array
o Example: -

Output of code:

 swap()
o This method swaps the content of two arrays of same type and same
size
o It swaps index wise, thus element of index I of first array will be
swapped with the element of index i of the second array
o first_array.swap(second_array)
o Example: -
Output of code:

 size()
o This, method returns the number of elements present in the array
o Example: -

Output of code:

 begin()
o begin() method returns the iterator pointing to the first element of the
array
 end()
o end() method returns an iterator pointing an element next to the last
element in the array

Pair in STL

 pair is a template class in Standard Template Library


 pair is not a part of container

Syntax of using pair object

 pair<T1,T2>pair1;
 Example:
o pair <string, int>p1;

Inserting Value
p1=make_pair(“Suraj”,16);

Accessing pair members

pair<string,int>p1;
p1=make_par(“Suraj”,19);
cout<<p1.first<<endl;
cout<<p1.second<<endl;

Example: -
Output of code:

Comparison two pairs

We can also use comparison operator in pair

Example: -

Output of code:
Tuple in STL

 Just like in pair, we can pair two heterogeneous objects, in tuple we can pair
multiple object

Syntax of using tuple object

 tuple<T1, T2, T3> tuple1;


 Example
o Tupel<string,int,int>t1;

Inserting Value

t1=make_tuple<”India”,16,10);

Accessing tuple members

Example: -
Output of code:

Comparison two tuples

We can also Compare the tuple like pair

Vector Class in STL

Vector

 The most general-purpose container is the vector


 It supports a Dynamic Array
 Using .capacity() you can check length of vector example: -

Output of code:

Array and Vector


 Array size is fixed, no flexibility to grow or shrink during execution
 Vector can provide memory flexibility

Vector Size

 Vector being a dynamic array, doesn’t need size during declaration


 Code below will crate a blank vector
o vector<int>v1;

Initialize during declartion

vector<string>V{“Suraj”,”Patel”};

More vector declarations

Subscript operator []

Subscript operator is also defined for vector


Example: -
Output of code:

push_back()

push_back() is a member function, which can be used to add value to the vector
at the end
Example: -

Output of code:

pop_back()

It removes the last element


Example: -
Output of code:

capacity()

 It returns the capacity of the vector


 This is the number of elements it can hold before it will need to allocate
more memory

size()

 It returns the number of elements currently in the vector


 Example: -

Output of code:
clear()

 It removes all elements from the vector


 Example: -

Output of code:

at()

 This method works same in case of vector as it works for array


 It returns the element at ith index in the vector vector_name
 Example: -
Output of code:

front() and back()

 front() returns the first element of the vector


 back() returns the last element of the vector
 Example: -

Output of code:

Iterator

Now we want to place particular value at index 2


Marked line is important
example: -
Output of code:

List Class in STL

 List class supports a bidirectional, linear list.


 Vector supports random access but a list can be accessed sequentially only
 List can be accessed front to back or back to front

Creating List object

 list<int>l1;
 list<int>l2 {11,22,33,44};
 list<string>l3{“Dellhi”,”Lucknow”,”Dhaka” }

Printing a list

Example: -
Output of code:

Useful functions of list class

 size()
It tells the how many elements in the list Example: -

Output of code:

 push_back() and push_front()


push_back store new value in the end and push_front store new value in the
front Example: -

Output of code:

 pop_back() and pop_front()


pop_back remove last element of list and pop front remove first element of
list
Example: -
Output of code:

 sort()
This function sort the list Example: -

Output of code:
 reverse()
reverse function reverse the list of element. Example: -

Output of code:

 remove ()
remove function is used to remove particular elment in the list Example: -
Output of code:

 clear ()
clear function removes all element of the list Example: -

Output of code:
map class in STL

map

 Maps are used to replicate associative arrays


 Maps contain sorted key-value pair, in which each key is unique and cannot
be changed, and it can be inserted or deleted but cannot be altered

Map property

 maps always arrange its keys in sorted order


 In case the keys are of string type, they are sorted in dictionary order

How to create a map?

Or you can also write like this

Example: -
How to print map

Output of code:
Useful functions of map class

 at()
at function print particular index value Example:

Output of code:

 square []
Same like at if you want print particular index value Example: -

Output of code:

 size()
size function tells the how many element there Example: -
Output of code: -

 empty()
empty function tell that the map is empty or not If empty than print 1 if not
then print 0 example: -

Output of code:

 insert()
Insert function use to insert data Example: -
Output of code:

 clear()
remove all the element of the map example: -
Output of code:

String class

 The string class is a specialization of a more general template class called


basic_string
 Since defining class in C++ is creating a new data type, string is derived data
type
 This means operators can be overloaded for the class

Useful functions of map class

 Careless programmer can overrun the end of an array that holds a null
terminated string
 For example, using strcpy()
 String class handles such issues

string is in STL

 String is another container class


 To use string class, you have to include string header
o #include <string>

Constructors

 String class supports many constructor, some of them are


o string()
o string(const char *str)
o string(const string &str)

Operators
Insertion and extraction

 << Insertion (for output)


 >> Extraction (for input)

Mixed operations

 You can mix string object with another string object or c style string
 C++ string can also be concatenated with character constant
 Example: -

Output of code:
Useful methods

 assign()
assign function assign value in the variable Example: -

Output of code:

 append()
append function like concatenated Example: -

Output of code:

 insert()
This function is used to insert a new character, before the character
indicated by the position pos. Example:-

Output of code:
 replace()
replace function use to replace example: -

Output of code:

 erase()
erase function removes all data from the variable Example: -

No output because all data variable data is removed


 find()
find function tells the index number ( if it tells -1 that means there no such
element) Example: -

Output of code:

 rfind()
similar like find but it search from back if there is two similar word than it
count second word index and tell Example: -

Output of code:

 compare ()
it compares two strings
o If answer is 0 than it means both strings have same string
o If answer is 1 or -1 it means both string is different
Example: -

Output of code:

 c_str()
c str is used to string copy in the array Example: -
Output of code:

 size()
it tells the length of the string Example: -

Output of code:

You might also like