C++ : An Introduction Lecture 11: Namespaces; Strings again - - PowerPoint PPT Presentation
C++ : An Introduction Lecture 11: Namespaces; Strings again - - PowerPoint PPT Presentation
C++ : An Introduction Lecture 11: Namespaces; Strings again Introducing Namespaces Namespaces are a relatively new C++ feature What problem do namespaces solve? What if you buy two different general-purpose class libraries from two
Introducing Namespaces
- Namespaces are a relatively new C++ feature
- What problem do namespaces solve?
What if you buy two different general-purpose class libraries from two different vendors #include "vendor1.h" #include "vendor2.h" and then it turns out that the headers have this in them: // vendor1.h class String { ... }; // vendor2.h struct String { ... }; Error at Compile time!!!
Namespaces
- Namespace defines scope
– Place identifiers and variables within namespace – Access with namespace_name::member – Guaranteed to be unique namespace Name { contents } – Unnamed namespaces are global -- need no qualification – Namespaces can be nested
Using namespaces
- using statement
using namespace namespace_name; – Members of that namespace can be used without preceding namespace_name:: – Can also be used with individual member – Examples
using namespace std; Discouraged by some programmers, because includes entire contents of std using std::cout; Can write cout instead of std::cout
Vendor problem solved
- // vendor1.h
namespace Vendor1 { class String { ... }; }
- // vendor2.h
namespace Vendor2 { struct String { ... }; }
- There are no longer two declarations
for String
- Instead declarations exist for
Vendor1::String and Vendor2::String.
Vendor problem solved 2
- What happens if you say:
using namespace Vendor1; using namespace Vendor2;
- No Problem!
String s1
ERROR
Vendor1::String s1; Vendor2::String s2;
OK
1 #include <iostream> 2 using namespace std; // use std namespace 3 4 int integer1 = 98; // global variable 5 6 // create namespace Example 7 namespace Example 8 { // declare two constants and one variable 9 const double PI = 3.14159; 10 const double E = 2.71828; 11 int integer1 = 8; 12 13 void printValues(); // prototype 14 15 namespace Inner // nested namespace 16 { enum Years { FISCAL1 = 1990, FISCAL2, FISCAL3 }; 17 } // end Inner 18 } // end Example
This integer1, different from the global integer1. This includes all of std, allowing us to use cout and endl.
19 namespace // unnamed namespace -- vars global 20 { double doubleInUnnamed = 88.22; // declare variable 21 } // end namespace 22 23 int main() 24 { // output value doubleInUnnamed of unnamed namespace 25 cout << "doubleInUnnamed = " << doubleInUnnamed; 26 // output global variable 27 cout << "\n(global) integer1 = " << integer1; 28 // output values of Example namespace 29 cout << "\nPI = " << Example::PI << "\nE = " 30 << Example::E << "\ninteger1 = " 31 << Example::integer1 << "\nFISCAL3 = " 32 << Example::Inner::FISCAL3 << endl; 33 Example::printValues(); 34 } // end main
More String Handling
- Strings in C++ are arrays of characters
- Are terminated by a NUL (\0)
character
- The string “hello” is really
‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’ = 6 bytes
Primitive Strings – String Literal
char *str = "Hello"; // static string literal for (int i = 0; str[i] != '\0'; i++) cout << str[i];
Primitive Strings – String Buffers char *str = new char[255]; cin >> str; // reads till a whitespace cout << str;
Automatic Type Conversion
Can do arithmetic operations on characters according to the ASCII table cout << (char) ('d' + 3) << “\n”; cout << (‘d’ + 3) << “\n”;
g 103
ASCII
- The ASCII character set is limited to
256 characters due to using only 1 byte
- Other character sets exist that use
more than one byte – Unicode, Kanji
- Due to ASCII being represented by an
integer this is legal
if (ch >= ‘a’ && ch <= ‘z’) …
The cstring Library
- Provides string processing functions (inherited
from C)
- Some functions in the library
int strlen(const char *s); char *strcpy(char *dest, const char *src); char *strcat(char *dest, const char *src); int strcmp(const char *s1, const char *s2); char *strchr(const char *s1, int c); char *strstr(const char *s1, const char *s2);
Example Implementations
int strlen (const char *s) { int n = 0; while (*s++) n++; return n; }
While the char pointed to by s is not the terminating NUL (and increment s anyway), then …
Example Implementations
char *strchr(const char *s, int c) { while (1) { if (*s == c) return (char *) s; if (*s++ == ‘\0’) return NULL; } }
Example Implementations
char *strcpy(char *dest, const char *src) { char *p = dest; while(*p++ = *src++) ; //empty loop return (dest); }
Example Implementations
char *strcat(char *dest, const char *src) { char *p = strchr(dest, 0); strcpy(p, src); return (dest); }
Is this valid?
int main() { char *str = "Hello World!"; char s[100]; char *p; p = strcat(str, " How are you today?"); cout << str << '\n'; cout << p << '\n'; }
NO! str is a static string
strcpy(s, str); p=strcat(s, “How are you today?”);
Exercise
- Explain the differences between the three
comparisons below:
char *p = "help"; char *q = "hello"; if (strcmp(p, q) >= 0) … if (*p >= *q) … if (p >= q) …
String p is greater or equal to string q Dereference p and q and compare first characters Compare the memory addresses
Exercise 8.1
Children often code messages by substituting letters by others further in the
- alphabet. For example, substituting letters
3 places further translates “hello yacht” into the secret message “khoor bdfkw”.
1. Write a function which takes a string, and puts its encrypted version into a second array parameter. 2. Can you write your decrypt method so that the same method can be used for encryption and decryption?
Exercise 8.1 Solution
#include <iostream.h> #include <ctype.h> int str_offset = 3; void crypt(char *src, char *dst) { for (int i = 0; src[i] != '\0'; i++) { if (tolower(src[i]) >= 'a' && tolower(src[i]) <= 'z') { dst[i] = tolower(src[i]) + str_offset; if (dst[i] > 'z') dst[i] = 'a' + dst[i] - 'z' - 1; if (dst[i] < 'a') dst[i] = 'z' - 'a' + dst[i] + 1; } else { dst[i] = src[i]; } dst[i] = '\0'; }
void crypt(char *src, char*dst, const int offset) ;
Exercise 8.1 Solution
void main() { char *str = "Hello, Zet"; char *enc = new char[sizeof(str)]; char *dec = new char[sizeof(str)]; crypt(str, enc); cout << "The original string contained: " << str << '\n'; cout << "The encrypted string contains: " << enc << '\n'; str_offset = -3; crypt(enc, dec); cout << "The decrypted string contains: " << dec << '\n'; delete []enc; delete []dec; }