Sie sind auf Seite 1von 21

SRI RAMAKRISHNA

INSTITUTE OF TECHNOLOGY

Programming and Data


Structures - II
UNIT II
String Handling Copy Constructor - Polymorphism compile time and run time polymorphisms function
overloading operators overloading dynamic memory allocation - Nested classes - Inheritance virtual
functions.

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

Appending one string to another

at

For getting the character at a specific location

begin

Getting the starting location of string

compare

Comparing two strings

empty

If string is empty then it returns true or otherwise false

end

Returning the ending location of string

erase

For removing the specified character

find

For searching the occurrence of specific substring

insert

For inserting the character at a specific location

length

Returns the length of the string

replace

Replaces the specific character

Size

Gives the size of the string

Swap

For swapping
the two strings
R.Nagendran

SRI RAMAKRISHNA
INSTITUTE OF TECHNOLOGY
Operator
+

Concatenation of two strings

Assigning one string to variable

==

For checking the equality

!=

For checking inequality

<

Less than

<=
>

1/7/17

Purpose

Less than or equal to


Greater than

>=

Greater than or equal to

<<

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

cout<< After erase function srt1 becomes;


cout<<str1<<endl;
string str4=str1.substr(2,3);
cout<<Finding the substring;
cout<<\n<<str4<endl;
}
Output
GREATE
After insert function srt1 becomes
HelloGREATEIndia
After erase function srt1 becomes
HelloIndia
Finding the substring
iio
R.Nagendran

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
};

// Main function for the program


int main( )
{
Box Box1;
// Declare Box1 of type Box
Box Box2;
// Declare Box2 of type Box
Box Box3;
// Declare Box3 of type Box
double volume = 0.0;
// Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
OUTPUT:
Volume of Box1 : 210Volume of Box2 : 1560Volume of Box3 : 5400

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

// overloaded minus (-) operator


Distance operator- ()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};

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

Unary operator No Arguments

One Argument

Binary
Operator

Two Arguments

friend vector operator+


(vector,vector); //vector addition
friend vector operator-(vector); //Unary
minus

One Argument

vector operator-(vector &a); //


subtraction
int vector==(vector) // Comparison
friend int operator==(vector,vector);
//comparison
1/7/17

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

void complex :: display (void)


{
cout<< x << +j << y << \n;
}
main()
{
complex C1,C2,C3;
C1= complex(2.5,3.5);
C2 =complex(1.6,2.7);
C3=C1+C2;
cout<< C1 = ; C1.display();
cout<< C2 = ; C2.display();
cout<< C3 = ; C3.display();
}
OUTPUT
C1 =2.5 +j3.5
C2 = 1.6 +j2.7

R.Nagendran

C3= 4.1 +j6.2

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;

Bptr -> =100;


cout<< bptr points to base object;
bptr -> show();
// derived class
DC derived;
bptr = &derived;
bptr -> b =200;
// bptr ->d =300

Wont work

cout<< bptr points to derived object;


bptr ->show();
// Accessing d using a pointer type derived class DC

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()

cout<< \n Display derived ;

cout<< \n Display base ;

void show()

virtual 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

Das könnte Ihnen auch gefallen