Escolar Documentos
Profissional Documentos
Cultura Documentos
INSTITUTE OF TECHNOLOGY
1/7/17
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
String Handling
String is nothing but collection of characters. That means
an array of characters can hold a string and we can perform
various operations on such a string. For example we can find
the length of the string, we can compare or concatenate two
strings, we can reverse a given string and so on.
In ANSI C++ use of strings is possible not simply by array of
characters but there is a special class called string is available
using which string can be manipulated. For that we have to
use # include <string> at the beginning of the program.
1/7/17
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
String ClassFunctions
1/7/17
Purpose
append
at
begin
compare
empty
end
erase
find
insert
length
replace
Size
Swap
For swapping
the two strings
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Operator
+
==
!=
<
Less than
<=
>
1/7/17
Purpose
>=
<<
Output
>>
Input
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
#<include<string>
main()
{
string str1;
string str3;
cout<< Enter some text \n;
cin >> str1;
string str2(India);
str3=str1+str2;
cout<< The concatenated string is <<str3<<endl;
}
Output
Enter some string
Hello
The concatenated string is HelloIndia
1/7/17
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Manipulating strings
cout<< str1;
#include<string>
main()
{
string str1(Hello);
string str2(India);
string str3;
str1+=str2;
cout<<Enter some string\n:;
cin>>str3;
str1.insert(5,str3);
cout<< After insert function srt1 becomes;
str1.erase(5,6);
1/7/17
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
class STRING
{
string str[10];
int n;
public:
void getdata();
{
cout<<string needed;
cin>>n;
cout<< Enter the strings;
for (int i=0; i<n; i++)
{
cin>>str[i];
}
}
Void display()
{
cout<<the strings are ;
for ( int I =0; i< n; i++)
{
cout<< \n<<str[i];
}
}
1/7/17
void sort()
{
string temp;
for(int i=0;i<n;i++)
{
for(j=i+1; j<n; j++)
{
if(str[i]>str[j])
{
temp=str[i];
str[i] =str[j];
str[j]=temp;
}
}
}
}
};
main() {
{
STRING obj;
obj.getdata();
obj.display();
obj.sort();
obj.display();
}
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
clude <iostream>
ng namespace std;
s printData
public:
d print(int i)
cout << "Printing int: " << i << endl;
d print(double f)
cout << "Printing float: " << f << endl;
int main(void)
{ printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;}
OUTPUT:
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
d print(char* c)
cout << "Printing character: " << c << endl;
1/7/17
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Operators overloading in C++
Overloaded operators are functions with special names the
keyword operator followed by the symbol for the operator
being defined. Like any other function, an overloaded
operator has a return type and a parameter list.
Box operator+(const Box&);
declares the addition operator that can be used to add two
Box objects and returns final Box object. Most overloaded
operators may be defined as ordinary non-member functions
or as class member functions.
1/7/17
R.Nagendran
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
#include <iostream>
class Box
{ public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
void setBreadth( double bre )
{
breadth = bre;
}
void setHeight( double hei )
{
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length;
// Length of a box
double breadth;
// Breadth of a box
double height;
// Height of a box
1/7/17
};
R.Nagendran
10
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
UNARY OPERATORS:
#include <iostream>
using namespace std;
class Distance
{ private:
int feet;
// 0 to infinite
int inches;
// 0 to 12
public:
// required constructors
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
// method to display distance
void displayDistance()
{
cout << "F: " << feet << " I:" << inches <<endl;
}
1/7/17
int main()
{
Distance D1(11, 10), D2(-5, 11);
-D1;
// apply negation
D1.displayDistance(); // display D1
-D2;
// apply negation
D2.displayDistance(); // display D2
return 0;
}
OUTPUT:
F: -11 I:-10F: 5 I:-11
R.Nagendran
11
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Overloadable/Non-overloadableOperators:
1/7/17
&
<
>
<=
>=
++
--
<<
>>
==
!=
&&
||
+=
-=
/=
%=
^=
&=
|=
*=
<<=
>>=
[]
()
->
->*
new
new []
delete
delete []
R.Nagendran
12
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Following is the list of operators, which cannot be overloaded:
::
1/7/17
.*
R.Nagendran
?:
13
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Member
Function
Example:
vector operator+(vector); // vector
vector operator-(); // unary minus
Friend
Function
One Argument
Binary
Operator
Two Arguments
One Argument
R.Nagendran
14
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
#include<iostream.h>
class complex
{
float x;
float y;
public:
complex(){ }
complex(float real, float imag)
{
x= real ; y=imag;
}
complex operator+(complex)
void display(void);
};
complex complex : : operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y = y+c.y;
return(temp);
}
1/7/17
R.Nagendran
15
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
complex
operator +
(complex)
temp
4.10
temp.x =
c.x
+
6.20
temp.y =
c.y
+
return
(temp)
}
C3
1/7/17
4.10
C1
R.Nagendran
2.50
C2
1.60
16
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Pointers to Derived Objects
#include<iostream.h>
class BC
{
public :
int b;
void show()
{
cout<<b= << b << \n;
}
};
class DC : public BC
{
public :
int d;
void show()
{
cout<<b= << b << \n << d=<< d << \n;
}
};
1/7/17
main()
{
BC *bptr;
BC base;
bptr =&base;
Wont work
R.Nagendran
17
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
DC *dptr;
dptr = &derived;
dptr -> = 300;
dout<<dptr is derived type pointer;
dptr -> show();
}
OUTPUT
bptr points to base object;
b=100
bptr points to derived object;
b=200
dptr is derived type pointer;
b=200
b=400
1/7/17
R.Nagendran
18
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Virtual functions
class base
class derived
public:
public:
void display()
void display()
void show()
{
cout<< \n Show derived ;
{
cout<< \n Show base ;
}
};
}
};
1/7/17
R.Nagendran
19
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
main()
{
base B;
derived D;
base *bptr;
cout<< \n bptr points to base ;
bptr =&B;
bptr -> display();
bptr -> show();
cout<< \n bptr points to Derived ;
bptr =&D;
bptr -> display();
bptr -> show();
}
1/7/17
R.Nagendran
20
SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Pure virtual functions
A pure virtual function is a function declared in the base class
that has no definitive relative to the base class.
virtual void display() =0;
1/7/17
R.Nagendran
21