c an introduction
play

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


  1. C++ : An Introduction Lecture 11: Namespaces; Strings again

  2. 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!!!

  3. 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

  4. 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

  5. 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.

  6. Vendor problem solved 2 • What happens if you say: using namespace Vendor1; using namespace Vendor2; • No Problem! � ERROR String s1 Vendor1::String s1; � OK Vendor2::String s2;

  7. This includes all of std , 1 #include <iostream> allowing us to use cout and 2 using namespace std; // use std namespace endl . 3 4 int integer1 = 98; // global variable 5 6 // create namespace Example This integer1 , different 7 namespace Example from the global integer1 . 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

  8. 19 namespace // unnamed namespace -- vars global double doubleInUnnamed = 88.22; // declare variable 20 { 21 } // end namespace 22 23 int main() 24 { // output value doubleInUnnamed of unnamed namespace cout << "doubleInUnnamed = " << doubleInUnnamed; 25 // output global variable 26 cout << "\n(global) integer1 = " << integer1; 27 // output values of Example namespace 28 cout << "\nPI = " << Example::PI << "\nE = " 29 << Example::E << "\ninteger1 = " 30 << Example::integer1 << "\nFISCAL3 = " 31 << Example::Inner::FISCAL3 << endl; 32 33 Example::printValues(); 34 } // end main

  9. 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

  10. Primitive Strings – String Literal char *str = "Hello"; // static string literal for (int i = 0; str[i] != '\0'; i++) cout << str[i];

  11. Primitive Strings – String Buffers char *str = new char[255]; cin >> str; // reads till a whitespace cout << str;

  12. 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

  13. 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’) …

  14. 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);

  15. 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 …

  16. Example Implementations char *strchr(const char *s, int c) { while (1) { if (*s == c) return (char *) s; if (*s++ == ‘\0’) return NULL; } }

  17. Example Implementations char *strcpy(char *dest, const char *src) { char *p = dest; while(*p++ = *src++) ; //empty loop return (dest); }

  18. Example Implementations char *strcat(char *dest, const char *src) { char *p = strchr(dest, 0); strcpy(p, src); return (dest); }

  19. Is this valid? int main() { char *str = "Hello World!"; char s[100]; char *p; strcpy(s, str); p = strcat(str, " How are you today?"); p=strcat(s, “How are you today?”); cout << str << '\n'; cout << p << '\n'; } NO! str is a static string

  20. Exercise • Explain the differences between the three comparisons below: char *p = "help"; char *q = "hello"; if (strcmp(p, q) >= 0) … String p is greater or equal to string q if (*p >= *q) … Dereference p and q and compare first characters if (p >= q) … Compare the memory addresses

  21. 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?

  22. 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) ;

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

Recommend


More recommend