Week3

static, const, friend, operator overloading

letter.h
1     	#ifndef LETTER_H
2     	#define LETTER_H
3     	#include <iostream>
4     	#include "word.h"
5     	
6     	using namespace std;
7     	
8     	class Word;
9     	class Letter {
10    	 public:
11    	  Letter();
12    	  Letter(char c);
13    	  Word operator+(const Letter& a);
14    	  bool operator==(const Letter& a);
15    	  bool operator!=(const Letter& a);
16    	  friend ostream &operator<<(ostream &o, const Letter &a);
17    	  void setMyChar(char a);
18    	  void setSharedChar(char a);
19    	 private:
20    	  static char sharedChar;
21    	  char myChar;
22    	  static int letterCount = 0;
23    	};
24    	
25    	#endif
letter.cpp
1     	#include <iostream>
2     	#include "letter.h"
3     	#include "word.h"
4     	const char INVALID = '*';
5     	
6     	ostream &operator << (ostream &o, const Letter &a) {
7     	  return o << a.myChar;
8     	}
9     	Letter::Letter() {
10    	  sharedChar = INVALID;
11    	  myChar = INVALID;
12    	}
13    	Letter::Letter(char c) {
14    	  sharedChar = INVALID;
15    	  myChar = INVALID;
16    	  if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
17    	    myChar = c;
18    	  }
19    	  letterCount++;
20    	}
21    	
22    	Word Letter::operator+(const Letter &a) {
23    	  Letter* w;
24    	  w = new Letter[2];
25    	  w[0] = *this;
26    	  w[1] = a;
27    	  return Word(w,2);
28    	}
29    	
30    	bool Letter::operator==(const Letter &a) {
31    	  return myChar == a.myChar;
32    	}
33    	bool Letter::operator!=(const Letter &a) {
34    	  return myChar != a.myChar;
35    	}
36    	void Letter::setMyChar(char a) {
37    	  myChar = a;
38    	}
39    	void Letter::setSharedChar(char a) {
40    	  sharedChar = a;
41    	}
word.h
1     	#ifndef WORD_H
2     	#define WORD_H
3     	
4     	#include "letter.h"
5     	
6     	using namespace std;
7     	
8     	class Letter;
9     	class Word {
10    	 public:
11    	  Word(Letter* w, int len);
12    	  ~Word();
13    	  bool operator==(const Word* w);
14    	  friend ostream &operator<<(ostream &o, const Word &a);
15    	 private:
16    	  Letter* letters;
17    	  int length; 
18    	};
19    	
20    	#endif
word.cpp
1     	#include <iostream>
2     	#include "word.h"
3     	
4     	using namespace std;
5     	
6     	ostream &operator << (ostream &o, const Word &a) {
7     	  for(int i=0;i<a.length; i++) {
8     	    o << a.letters[i];
9     	  }
10    	  return o;
11    	}
12    	Word::Word(Letter* w, int len) {
13    	  length = len;
14    	  letters = w;
15    	}
16    	Word:: ~Word() {
17    	  delete[] letters;
18    	}
19    	bool Word::operator==(const Word* w) {
20    	  if (w->length != length) {
21    	    return false;
22    	  }
23    	
24    	  for(int i=0;i<length; i++) {
25    	    if (w->letters[i] != letters[i]) {
26    	      return false;
27    	    }
28    	  }
29    	  return true;
30    	}
  1. What is the value of letterCount if we call:
    a = new Letter('a'); b = new Letter('b'); c = new Letter('c');
  2. What happens to b when we call:
    a.setMyChar('b'); a.setSharedChar('b');
  3. What happens when we call
    a.INVALID = '#';
  4. Why is << a friend function?
  5. What happens in
    Word w1 = b + c; Word w2 = a + w1;

Strings

strings.cpp
1:	 #include <string>
2:	 #include <iostream>
3:	 
4:	 using namespace std;
5:	 
6:	 int main() {
7:	   string s1 = "Smile.  It makes the world wonder what you're up to.";
8:	   string s2, s3, s4;
9:	   int a, b, c;
10:	 
11:	   a = s1.find("w");
12:	   cout << a << endl;
13:	   b = s1.find("w", a+1);
14:	   cout << b << endl;
15:	   s2 = s1.substr(a, 5);
16:	   cout << s2 << endl;
17:	   s3 = s1.substr(b, 6);  
18:	   cout << s3 << endl;
19:	   s4 = s3;
20:	   cout << s4 << endl;
21:	   assert(s4 == s3);
22:	   s4 = s4 + s2;
23:	   cout << s4 << endl;
24:	   c = s4.length();
25:	   cout << c << endl;
26:	 
27:	 }

Programming Assignment

Some tips for the PA2 env.cpp
1:	 #include <iostream>
2:	 #include <fstream>
3:	 #include <string>
4:	 
5:	 using namespace std;
6:	 
7:	 int main (int argc, char* argv[], char* envp[]) {
8:	   const int MAX_LINE = 1024;
9:	   string homepath;
10:	   fstream filestream;
11:	   char curline[MAX_LINE];
12:	 
13:	   // print out all the environment variables
14:	   for(int i=0; envp[i] != NULL; i++) {
15:	     cout << envp[i] << endl;
16:	   }
17:	 
18:	   // get the value of a single environment variable
19:	   cout << getenv("HOME") << endl;
20:	 
21:	   homepath = getenv("HOME");
22:	   homepath += "/.bashrc";
23:	   filestream.open(homepath.c_str(), fstream::in);
24:	   while (filestream.getline(curline, MAX_LINE)) {
25:	     cout << curline << endl;
26:	   }
27:	 }