C plus plus String Class-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Object, Oriented, Programming, C plus plus, String, Class, Data, Types, Overloaded, Operators, Constructors, Variable

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
5/4/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture – 13
C++ String Class
The C++ String Class
Makes easier to handle string data types, as
compared to conventional character array
compared to conventional character array
.
No need to worry about memory management
char name[50]; OR char name[56];
String objects can manage a dynamic memory
allocation
Overloaded operators are available;
Usman Younis
Overloaded operators are available;
+ (add two strings), e.g., “this” + “ ” + “example”
< (checks inequality), e.g., “this” < “yellow” (true)
== (checks equality), e.g., “yellow” == “Yellow” (false)
>= (checks inequality), e.g., “yellow” >= “Yellow” (true)
pf3
pf4
pf5

Partial preview of the text

Download C plus plus String Class-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

The C++ String Class „ „ Makes easier to handle string data types, ascompared to conventional character arraycompared to conventional character array.No need to worry about memory management^ Object OrientedProgramming using C++jLecture – 13C++ String Class

Usman Younis^ „^ „^ ‰String objects can manage a dynamic memoryallocationOverloaded operators are available;Overloaded operators are available;^ ‰^ ‰^ ‰^ ‰^ char name[50]; OR char name[56];+ (add two strings), e.g., “this” + “ ” + “example”< (checks inequality), e.g., “this” < “yellow” (true)== (checks equality), e.g., “yellow” == “Yellow” (false)>= (checks inequality), e.g., “yellow” >= “Yellow” (true)

Usman Younis^ Constructors^ „Input/output with String Object^ A string variable is an object of the string class,which has following constructors:which has following constructors:^ ‰^ ‰^ ‰^ ‰^ ‰^ string(); //an empty stringcout<<string(“this is a temporary string \n”);//temporary objectstring s1 = “hello”; //assignmentstring s3(s1);And many more, (about 17 constructors are availablein the standard C++ string class).^ //copy

Usman Younis^ „^ „^ „^ „^ cin‰^ ‰getline(cin, s, ‘.’);^ ‰^ ‰getline(cin, s);cout^ >> overloaded for string inputNext line (without newline) into sNext line (with newline) into s, finishes with fullstop<< overloaded for string output^ >>^ <<^ s;l^^ s;(d d f^ t i^ )^ i^ t,

Usman Younis^ Member functions^ „^ „^ „^ „Member functions^ find(‘c’); OR find(“hello”)‰find(s1);^ ‰compare(s1);^ ‰insert(int pos1, const string& str)^ ‰^ R tRcharacter c, OR string “hello”.Return Position of leftmost occurrence of s1Returns <0 if s < s1, 0 if s == s1, orinsert str at pos1.eturns position of the leftmost occurrence of^ iti^ f th^ l ft^ t^ >0 if s > s1.f

Usman Younis^ „^ „^ „^ „^ erase(int pos = 0, int n = npos)^ ‰‰replace(int pos1, int n1, const string& str).^ ‰swap(string&).^ ‰^ ‰And many more^ erase n characters starting at poserase n characters starting at pos, or till the endof the stringerase(pos1,n1) + insert(pos1,str)a swap(b) swaps the contents of strings a and ba.swap(b) swaps the contents of strings a and b.The standard also provides for a functionswap(a,b)^ or till the end

Usman Younis^ Example#include #include using namespace std;void main(){Example (contd..)string s1="Quick! Send for Count Graystone.";string s2("Lord");string s3("Don't");string s;s1.erase(0, 7);s1.replace(9, 5, s2);s1.insert(0, s3 + " ");cout<<"s1: "<<s1<<endl;g^ p^ ;^ //replace “Count” with “Lord”//remove “Quick! ”//insert “Don’t ” at beginning

Usman Younis} cout<<"Length of s1: "<<s1.length()<<endl;cout<<"Size of s1: "<<s1.size()<<endl;cout<<s1.find(s3)<<endl;cout<<s1.compare(s2)<<endl;cout<<s1.compare(s3)<<endl;cout<<s3.compare(s2)<<endl;s = s3 + " " + s2;cout<<"s: "<<s<<endl;s.swap(s1);cout<<"After swapping...\n";cout<<"s: "<<s<<endl; ()^ ;