0% found this document useful (0 votes)
78 views5 pages

6 - A User-Defined String Class

The document discusses the C++ string class and provides examples of how to use it. It covers defining and assigning string objects, input/output with strings, finding substrings within strings, modifying strings, comparing strings, and accessing individual characters in strings. Member functions of the string class like getline(), find(), erase(), replace(), insert(), append(), and at() are demonstrated.

Uploaded by

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

6 - A User-Defined String Class

The document discusses the C++ string class and provides examples of how to use it. It covers defining and assigning string objects, input/output with strings, finding substrings within strings, modifying strings, comparing strings, and accessing individual characters in strings. Member functions of the string class like getline(), find(), erase(), replace(), insert(), append(), and at() are demonstrated.

Uploaded by

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

AJ/Handout 6 -1- Object Oriented Programming

Lesson 6

Objectives

 A User-Defined String Type


 The Standard C++ string Class

 Features & Member Functions of C++ string Class

A User-Defined String Type

There are some features which are not available in C++ string class. Like
strDest = str Src; but we can obtain it by object oriented approach. Though the other
possible solution we will see later. So let we have our own ‘String’ class which is capable of
resolving said problem.
------------------------------------------------------------------------------------------------
#include <iostream> //String example , A Strings as a Class
#include <cstring>
using namespace std;
class String
{
private:
char str[80]; //name of widget part as string
public:
String ()
{str[0]=’\0’;}
String (char s[] )
{strcpy(str,s);}
Void display()
{cout<<str;}
Void concat(String s2)
{
If ( strlen(str) + strlen (s2.str) < 80)
Strcat(str,s2.str);
Else
Cout<<”\nString too long”;
}
};
Void main ( )
{
String s1(“Hello!”);
String s2 = ”How are you?”
String s3;

Cout<<”\n S1= ”; s1.display();


Cout<<”\n S2= ”; s2.display();
Cout<<”\n S3= ”; s3.display();

S3=s1;
AJ/Handout 6 -2- Object Oriented Programming

Cout<<”\n S3= ”; s3.display();

S3.concat(s2);
Cout<<”\n Concated S3= ”; s3.display();
}
Related Problems
Do strcpy, strcat by using other methods instead of using built-in function.

The Standard C++ string Class


Standard C++ includes string class which improves the traditional c-string class we have
discussed thus far. Now one need not to worry about the creating a string array of right
size, string class takes all responsibilities of memory management. It also allows the
operator overloading for concatenating two strings using ‘+’ operator. S3 = S1 + S2;
It is more reliable and safer as old one. Although there are still some situations where we
prefer to use the old class but yet new one is more elegant. We will discuss various famous
functions and operators.

Defining and Assigning string objects


We can define a string object in various ways. We can use constructor with no argument
and also constructor with one argument as c-string constant delimited by double quotes.
Performing the same functionality as previous program we can do it now more easily.
//defining and assigning string objects
#include <iostream>
#include<string>
Using namespace std;
Void main()
{ string s1(“Hello!”);
string s2(“dude”);
string s3;

s3=s1;
cout<<”\nS3=”<<s3;

s3 = “Ohh Faraz” + s1 + “how are you”; //concatenating


s3 +=s2; //concatenating
cout<<”\nNew S3”<<s3;

s1.swap(s2); //swaps strings s1 and s2


cout<<s2<<s1<<” nice to see u”;
}
Input/Output with string Objects
Though the input/output are handled in the same way we have already seen in c-string class
but in contrast to other facilities like reading embedded blanks and multiple lines input we
use function of string class named getline():
//sstrio.cpp
//string class i/o
#include<iostream>
#include<string>
Using namespace std;

Void main()
{
AJ/Handout 6 -3- Object Oriented Programming

string full_name, nickname, address; //object of no arg ctor


string greeting(“Hello, ”) //object by one arg ctor

cout<<”Enter your full name: ”;


getline(cin, full_name); //reads embedded blanks

cout<<”Your full name is: ”<<full_name<<endl;

cout<<”Enter your nick name: ”


cin>>nickname; //old style

greeting +=nickname;
cout<<greeting<<endl;

cout<<”Enter your address on multiple lines terminate with $:\n”;


getline(cin, address, ‘$’); //reads multiple lines
cout<<”your address is: ”<<address<<endl;
}

The function getline() is same as get() with c-strings but not a member function. Instead its
first arguments is the stream object from which the input will come and second is the string
object where the text will placed.
Finding string Objects
The string class contains variety of member functions for finding srtings and substrings in
string object.

//Finding string objects


#include<iostream>
#include<string>
Using namespace std;

void main()
{
string s1=”In the name of Allah, the most beneficent the merciful.”;
int n;

n=s1.find(“Allah”);
cout <<”Found Allah at ::”<<n<<endl;

n=s1.find_first_of(“Atmb”); //looks for any of the group ‘A’,‘t’,’m’,’b’


cout<<”First of Atmb is at: ”<<n<<endl;

n=s1.find_first_not_of(“aeiouAEIOU”);
cout<<”First Consonant at: ”<<n<<endl;
}
Modifying string Objects
//changing part of string objects
#include<iostream>
#include<string>
using namespace std;

void main()
AJ/Handout 6 -4- Object Oriented Programming

{
string s1="A thing of beauty is a joy forever.";
string s2("Rose is a");
string s3("which ");

s1.erase(0,7); //removes "A thing"


s1.replace(0,3,s2);
s1.insert(17,s3);
s1.append(7,'.');

int x=s1.find(' ');


while(x<s1.size())
{
s1.replace(x,1,"|");
x=s1.find(' ');
}
cout<<"Your string is: "<<s1<<endl;
}
Comparing string objects
//comparing string objects
#include<iostream>
#include<string>
using namespace std;

void main()
{
string aName=”Somro”;
string username;

cout<<”Enter ur first name:\n”;


cin>>username;

if(username==aName)
cout<<”Greetings, Somro\n”;
else if (username<aName)
cout<<”You come before Somro\n”;
else if (username>aName)
cout<<”You come after Somro\n”;
int n= username.compare(0,2,aName,0,2)
cout<<”The first two letters of your name”;

if(n==0)
cout<<”match”;
else if(n<0)
cout<<”come before”;
else
cout<<”come after”;
cout<<aName.substr(0,2)<<endl;
}
Accessing character in string objects
//accessing characters in string objects
#include<iostream>
AJ/Handout 6 -5- Object Oriented Programming

#include<string>
using namespace std;

void main()
{
char charary[80];
string word;

cout<<"Enter a word:\n";
cin>>word;

int wlen = word.length ();

cout<<"One character at a time:";


for(int j=0;j<wlen;j++)
cout<<endl<<word.at(j);//cout<<word[j];
word.copy (charary,wlen,0);//copy string object to array
charary[wlen]=0; //terminate with \0
cout<<"\nArray contains:"<<charary<<endl;

You might also like