You are on page 1of 33

Lecture 9_II:

Introduction on Object Oriented


Programming (OOP)

Dr. Fadi Alzhouri

Concordia University

Created by Rose Gomar, modified and presented by Fadi Alzhouri


Textbook (Copyright)

“C++ How to Program”, 10th edition, by Harvey and


Paul Deitel Print ISBN: 0-13-444823-5, Chapter 9,
10.

10/24/2022 2
Specification – Implementation

• Separating specification from implementation


• Use a header file (.h file) to describe the class
• E.g. time.h can contain the description of the class Time

• Use a .cpp file to describe the implementation of the member


functions of the class
• E.g., time.cpp can contain the member function of class Time

• The file time.cpp must include time.h

• Any other cpp file that you create can use the class Time if you
include both time.h and time.cpp

3
Definition of Class: Cylinder

4
Implementation of Class Cylinder

5
Using the Class Cylinder

6
Class string

• C++ class that represents strings


• string declaration and initialization
• string s1{"Hello"}; // with 1 constructor argument

• string s2{8, 'x'}; // with 2 constructor arguments

• String of 8 'x' characters

• string month = "March";


• =: implicit call to string class constructor for conversion

• Same as: string month{"March"};


• s1 and s2: objects of the class string
• You need to include the header file <string>
#include <string>

7
Using Strings

• length member function: s1.length()


• Use [] to access individual characters: s1[0]
• 0 to length-1
• Stream extraction
• cin >> stringObject;
• Assignment
• s2 = s1;
• Makes a separate copy
• s2.assign(s1);
• Same as s2 = s1;
• myString.assign(s, start, N);
• Copies N characters from s, beginning at index start
• Individual characters: s2[0] = s3[2];

8
Using Strings (cont.)

• Range checking
• s3.at( index );
• Returns character at index
• [] has no range checking

• Concatenation
• s3.append( "pet" );
• s3 += "pet";
• Both add "pet" to end of s3
• s3.append( s1, start, N );
• Appends N characters from s1, beginning at index start

9
Using Strings (cont.)

Output
Line 182, 186: String initialization and assignment.
string1: cat
string2: cat
string3: cat
10
Using Strings (cont.)

Line 195: After modification of


string2 and string3:
string1: cat
string2: rat
string3: car
Line 215: After
concatenation:
string1: catacomb
string2: rat
string3: carpet
string4: catapult
string5: comb
Line 200: Note use of member function at instead of [].

11
Using Strings (cont.)

string1: cat
string2: cat
string3: cat

After modification of string2 and string3:


string1: cat
string2: rat
string3: car

After concatenation:
string1: catacomb
string2: rat
string3: carpet
string4: catapult
string5: comb

12
Comparing Strings

• You can use the following operators


• ==, !=, <, >, <= and >=
• Return bool
• s1.compare(s2)
• Returns positive if s1 lexicographically greater
• Compares letter by letter
• 'B' lexicographically greater than 'A'
• Returns negative if less, zero if equal
• s1.compare(start, length, s2, start,
length)
• Compare portions of s1 and s2
• s1.compare(start, length, s2)
• Compare portion of s1 with all of s2

13
Comparing Strings (cont.)

string1: Testing the comparison functions.


string2: Hello
string3: stinger
string4: Hello

string1 > string4

14
Comparing Strings (cont.)

Note use of
compare.

15
Comparing Strings (cont.)

16
Comparing Strings (cont.)

string1: Testing the comparison functions.


string2: Hello
string3: stinger
string4: Hello

string1 > string4


string1.compare( string2 ) > 0
string1.compare( 2, 5, string3, 0, 5 ) == 0
string4.compare( 0, string2.length(), string2 ) == 0
string2.compare( 0, 3, string4 ) < 0

17
Substrings

• Function substr gets substring


• s1.substr(start, N);
• Gets N characters, beginning with index start
• Returns substring

18
Substrings (cont.)

Note usage of substr.

plane

19
Swapping Strings

• Function swap for swapping strings


s1.swap(s2);
• String object s1 whose value is swapped with that of string s2

20
Swapping Strings (cont.)

Before swap:
first one
Second two

After swap:
first two
Second one
21
String Characteristics

• Member functions
• s1.size() and s1.length()
• Number of characters in string
• s1.capacity()
• Number of elements that can be stored without reallocation
• s1.max_size()
• Maximum possible string size
• s1.empty()
• Returns true if empty
• s1.resize(newlength)
• Resizes string to newlength

22
String Characteristics (cont.)

23
String Characteristics (cont.)

Resize string.

Display various string


characteristics.

24
String Characteristics (cont.)
Statistics before input:
capacity: 0
max size: 4294967293
size: 0
length: 0
empty: true

Enter a string: tomato soup


The string entered was: tomato
Statistics after input:
capacity: 31
max size: 4294967293
size: 6
length: 6
empty: false

The remaining string is: soup


capacity: 31
max size: 4294967293
size: 4
length: 4
empty: false

string1 is now: soup1234567890abcdefghijklmnopqrstuvwxyz1234567890


capacity: 63
max size: 4294967293
size: 50
length: 50
empty: false

Stats after resizing by (length + 10):


capacity: 63
max size: 4294967293
size: 60
length: 60
empty: false
25
Finding Strings and Characters

• Find functions
• If found, index returned
• If not found, string::npos returned
• Public static constant in class string
• s1.find( s2 )
• s1.rfind( s2 )
• Searches right-to-left
• s1.find_first_of( s2 )
• Returns first occurrence of any character in s2
s1.find_first_of( "abcd" )
• Returns index of first 'a', 'b', 'c' or 'd'

26
Finding Strings and Characters (cont.)

• Find functions
• s1.find_last_of( s2 )
• Finds last occurrence of any character in s2
• s1.find_first_not_of( s2 )
• Finds first character NOT in s2
• s1.find_last_not_of( s2 )
• Finds last character NOT in s2

27
Finding Strings and Characters (cont.)

Note call to function find.

Find first occurrence


of m, i, s, o or p.

Find last occurrence


of m, i, s, o or p.

28
Finding Strings and Characters (cont.)

Searching is done from


the beginning of string1.

string::npos (public
static constant defined in
class string) is returned.

29
Finding Strings and Characters (cont.)

Original string:
noon is 12 pm; midnight is not.

(find) "is" was found at: 5


(rfind) "is" was found at: 24

(find_first_of) found 'o' from the group "misop" at: 1

(find_last_of) found ‘o' from the group "misop" at: 28

(find_first_not_of) '1' is not contained in "noi spm" and was found at:8

(find_first_not_of) '.' is not contained in " noon is 12 pm; midnight is not. " and was found
at:13

find_first_not_of(" noon is 12 pm; midnight is not.") returned: -1

30
String Manipulation Summary

31
String Manipulation Summary (cont.)

32
String Manipulation Summary (cont.)

33

You might also like