about summary refs log tree commit diff
path: root/usth/ICT2.2/labwork/3/C++
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-12-15 10:34:58 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-12-15 15:00:00 +0700
commit67393f42f41ab92219deb549f711121c4dab845b (patch)
treeebd0eb6c8a3d3bd69937312179aeaf273ea29c80 /usth/ICT2.2/labwork/3/C++
parentb38d9929f7a015b56b847fde7e83f814f354497e (diff)
downloadcp-67393f42f41ab92219deb549f711121c4dab845b.tar.gz
[usth/ICT2.2] Object Oriented Programming
Diffstat (limited to 'usth/ICT2.2/labwork/3/C++')
-rw-r--r--usth/ICT2.2/labwork/3/C++/README.md10
-rw-r--r--usth/ICT2.2/labwork/3/C++/automobile.cc56
-rw-r--r--usth/ICT2.2/labwork/3/C++/automobile.h20
-rw-r--r--usth/ICT2.2/labwork/3/C++/button-test.cc14
-rw-r--r--usth/ICT2.2/labwork/3/C++/button.h14
-rw-r--r--usth/ICT2.2/labwork/3/C++/cow-test.cc9
-rw-r--r--usth/ICT2.2/labwork/3/C++/cow.cc11
-rw-r--r--usth/ICT2.2/labwork/3/C++/cow.h17
-rw-r--r--usth/ICT2.2/labwork/3/C++/name-card-test.cc14
-rw-r--r--usth/ICT2.2/labwork/3/C++/name-card.cc47
-rw-r--r--usth/ICT2.2/labwork/3/C++/name-card.h18
-rw-r--r--usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc19
-rw-r--r--usth/ICT2.2/labwork/3/C++/shopping-cart.h16
-rw-r--r--usth/ICT2.2/labwork/3/C++/vector-test.cc20
-rw-r--r--usth/ICT2.2/labwork/3/C++/vector.cc19
-rw-r--r--usth/ICT2.2/labwork/3/C++/vector.h17
16 files changed, 321 insertions, 0 deletions
diff --git a/usth/ICT2.2/labwork/3/C++/README.md b/usth/ICT2.2/labwork/3/C++/README.md
new file mode 100644
index 0000000..8cd208f
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/README.md
@@ -0,0 +1,10 @@
+# Labwork 3: Implementations in C++
+
+At this point I've come to realized that like Thanos, C++ is inevitable.
+Despite my disgust toward the language (TBH I don't like Java any better),
+I need to know it enough, at least at the read-only level.
+
+Compilation of tests could be done as follows (on Unix-like system of course,
+since poor Windows users don't even have a standard C/C++ compiler):
+
+    c++ <classname>*.cc
diff --git a/usth/ICT2.2/labwork/3/C++/automobile.cc b/usth/ICT2.2/labwork/3/C++/automobile.cc
new file mode 100644
index 0000000..d274a60
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/automobile.cc
@@ -0,0 +1,56 @@
+#include <regex>
+#include <stdexcept>
+#include <string>
+
+#include "automobile.h"
+
+using namespace std;
+
+const regex license_pattern ("[0-9A-Z]+");
+
+void
+Automobile::set_license (string s)
+{
+  smatch m;
+  if (!regex_match (s, m, license_pattern))
+    throw invalid_argument{"invalid license plate"};
+  license = s;
+}
+
+void
+Automobile::set_fuel (double x)
+{
+  if (x < 0)
+    throw invalid_argument{"negative fuel"};
+  fuel = x;
+}
+
+void
+Automobile::set_speed (double x)
+{
+  speed = (x < 0) ? 0 : x;
+}
+
+Automobile::Automobile (string l, double f, double s)
+{
+  set_license (l);
+  set_fuel (f);
+  set_speed (s);
+}
+
+void
+Automobile::accelerate (double v)
+{
+  if (v < 0)
+    throw invalid_argument{"negative acceleration"};
+  if (fuel)
+    set_speed (speed + v);
+}
+
+void
+Automobile::decelerate (double v)
+{
+  if (v < 0)
+    throw invalid_argument{"negative deceleration"};
+  set_speed (speed - v);
+}
diff --git a/usth/ICT2.2/labwork/3/C++/automobile.h b/usth/ICT2.2/labwork/3/C++/automobile.h
new file mode 100644
index 0000000..0e65865
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/automobile.h
@@ -0,0 +1,20 @@
+#include <string>
+
+using namespace std;
+
+class Automobile
+{
+  double fuel;
+  double speed;
+  string license;
+public:
+  Automobile (string, double, double);
+  string get_license () { return license; }
+  double get_fuel () { return fuel; }
+  double get_speed () { return speed; }
+  void set_license (string);
+  void set_fuel (double);
+  void set_speed (double);
+  void accelerate (double);
+  void decelerate (double);
+};
diff --git a/usth/ICT2.2/labwork/3/C++/button-test.cc b/usth/ICT2.2/labwork/3/C++/button-test.cc
new file mode 100644
index 0000000..7164b2f
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/button-test.cc
@@ -0,0 +1,14 @@
+#include <iostream>
+
+#include "button.h"
+
+using namespace std;
+
+int
+main ()
+{
+  Button butt ("foo", "bar");
+  butt.depress ();
+  butt.undepress ();
+  cout << "button " << butt.label << " color " << butt.color << endl;
+}
diff --git a/usth/ICT2.2/labwork/3/C++/button.h b/usth/ICT2.2/labwork/3/C++/button.h
new file mode 100644
index 0000000..92fa685
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/button.h
@@ -0,0 +1,14 @@
+#include <string>
+
+using namespace std;
+
+class Button
+{
+  bool state;
+public:
+  string label;
+  string color;
+  Button (string l, string c) : label {l}, color {c}, state {false} {}
+  void depress () { state = true; }
+  void undepress () { state = false; }
+};
diff --git a/usth/ICT2.2/labwork/3/C++/cow-test.cc b/usth/ICT2.2/labwork/3/C++/cow-test.cc
new file mode 100644
index 0000000..ea7b86d
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/cow-test.cc
@@ -0,0 +1,9 @@
+#include "cow.h"
+
+int
+main ()
+{
+  Cow cow ("foo", "bar", 7);
+  cow.age = -4;   // some casting happens here
+  cow.moo ();
+}
diff --git a/usth/ICT2.2/labwork/3/C++/cow.cc b/usth/ICT2.2/labwork/3/C++/cow.cc
new file mode 100644
index 0000000..84fabc8
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/cow.cc
@@ -0,0 +1,11 @@
+#include <iostream>
+
+#include "cow.h"
+
+using namespace std;
+
+void
+Cow::moo ()
+{
+  cout << "Moo...!" << endl;
+}
diff --git a/usth/ICT2.2/labwork/3/C++/cow.h b/usth/ICT2.2/labwork/3/C++/cow.h
new file mode 100644
index 0000000..df7ffd9
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/cow.h
@@ -0,0 +1,17 @@
+#include <string>
+
+using namespace std;
+
+class Cow
+{
+  // The reason these have private access
+  // is that their no way a cow's name and breed can be changed.
+  string name;
+  string breed;
+public:
+  unsigned age;
+
+  Cow (string n, string b, unsigned a) : name {n}, breed {b}, age {a} {}
+  Cow (string n, string b) : name {n}, breed {b}, age {0} {}
+  void moo ();
+};
diff --git a/usth/ICT2.2/labwork/3/C++/name-card-test.cc b/usth/ICT2.2/labwork/3/C++/name-card-test.cc
new file mode 100644
index 0000000..b459d0d
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/name-card-test.cc
@@ -0,0 +1,14 @@
+#include <iostream>
+
+#include "name-card.h"
+
+using namespace std;
+
+int
+main ()
+{
+  NameCard card ("Foobar Baz", "420-69", "foo@bar.baz");
+  cout << "Name: " << card.get_name () << endl
+       << "Phone: " << card.get_phone () << endl
+       << "Email: " << card.get_email () << endl;
+}
diff --git a/usth/ICT2.2/labwork/3/C++/name-card.cc b/usth/ICT2.2/labwork/3/C++/name-card.cc
new file mode 100644
index 0000000..b6c0751
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/name-card.cc
@@ -0,0 +1,47 @@
+#include <regex>
+#include <stdexcept>
+#include <string>
+
+#include "name-card.h"
+
+using namespace std;
+
+const regex name_pattern ("[ A-Za-z]+");
+const regex phone_pattern ("[-0-9]+");
+// This should be good enough I guess?
+const regex email_pattern ("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
+
+void
+NameCard::set_name (string s)
+{
+  // I miss Raku so much
+  smatch m;
+  if (!regex_match (s, m, name_pattern))
+    throw invalid_argument{"invalid name"};
+  name = s;
+}
+
+void
+NameCard::set_phone (string s)
+{
+  smatch m;
+  if (!regex_match (s, m, phone_pattern))
+    throw invalid_argument{"invalid number"};
+  phone = s;
+}
+
+void
+NameCard::set_email (string s)
+{
+  smatch m;
+  if (!regex_match (s, m, email_pattern))
+    throw invalid_argument{"invalid name"};
+  email = s;
+}
+
+NameCard::NameCard (string n, string p, string e)
+{
+  set_name (n);
+  set_phone (p);
+  set_email (e);
+}
diff --git a/usth/ICT2.2/labwork/3/C++/name-card.h b/usth/ICT2.2/labwork/3/C++/name-card.h
new file mode 100644
index 0000000..08e76a7
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/name-card.h
@@ -0,0 +1,18 @@
+#include <string>
+
+using namespace std;
+
+class NameCard
+{
+  string name;
+  string phone;
+  string email;
+public:
+  string get_name () { return name; }
+  string get_phone () { return phone; }
+  string get_email () { return email; }
+  void set_name (string);
+  void set_phone (string);
+  void set_email (string);
+  NameCard (string, string, string);
+};
diff --git a/usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc b/usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc
new file mode 100644
index 0000000..4833e70
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/shopping-cart-test.cc
@@ -0,0 +1,19 @@
+#include <iostream>
+
+#include "shopping-cart.h"
+
+using namespace std;
+
+int
+main ()
+{
+  ShoppingCart cart;
+  cart.add_to_cart ("foo");
+  cart.add_to_cart ("foo");
+  cart.add_to_cart ("bar");
+  cart.remove_from_cart ("baz");
+  cout << cart.count ("foo") << ' '
+       << cart.count ("bar") << ' '
+       << cart.count ("baz") << endl;
+  cart.check_out ();
+}
diff --git a/usth/ICT2.2/labwork/3/C++/shopping-cart.h b/usth/ICT2.2/labwork/3/C++/shopping-cart.h
new file mode 100644
index 0000000..c1450e7
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/shopping-cart.h
@@ -0,0 +1,16 @@
+#include <string>
+#include <unordered_map>
+
+using namespace std;
+
+class ShoppingCart
+{
+  // quite of an improvement over the Java version
+  unordered_map<string, int> contents;
+public:
+  void add_to_cart (string item) { contents[item]++; }
+  void remove_from_cart (string item) { contents[item] = 0; }
+  // to make this class useful anyhow
+  int count (string item) { return contents[item]; }
+  void check_out () { contents.clear (); }
+};
diff --git a/usth/ICT2.2/labwork/3/C++/vector-test.cc b/usth/ICT2.2/labwork/3/C++/vector-test.cc
new file mode 100644
index 0000000..647c8a6
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/vector-test.cc
@@ -0,0 +1,20 @@
+#include <iostream>
+
+#include "vector.h"
+
+using namespace std;
+
+int
+main ()
+{
+  Vector u (4, 20);
+  Vector v (6, 9);
+  cout << "u = (" << u.x << ", " << u.y << ")" << endl;
+  cout << "v = (" << v.x << ", " << v.y << ")" << endl;
+  Vector w {u + v};
+  cout << "u + v = (" << w.x << ", " << w.y << ")" << endl;
+  w = u - v;
+  cout << "u - v = (" << w.x << ", " << w.y << ")" << endl;
+  w = u * v;
+  cout << "u * v = (" << w.x << ", " << w.y << ")" << endl;
+}
diff --git a/usth/ICT2.2/labwork/3/C++/vector.cc b/usth/ICT2.2/labwork/3/C++/vector.cc
new file mode 100644
index 0000000..5ac56de
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/vector.cc
@@ -0,0 +1,19 @@
+#include "vector.h"
+
+Vector
+operator+ (Vector u, Vector v)
+{
+  return u += v;
+}
+
+Vector
+operator- (Vector u, Vector v)
+{
+  return u -= v;
+}
+
+Vector
+operator* (Vector u, Vector v)
+{
+  return u *= v;
+}
diff --git a/usth/ICT2.2/labwork/3/C++/vector.h b/usth/ICT2.2/labwork/3/C++/vector.h
new file mode 100644
index 0000000..8433e79
--- /dev/null
+++ b/usth/ICT2.2/labwork/3/C++/vector.h
@@ -0,0 +1,17 @@
+class Vector
+{
+public:
+  int x;
+  int y;
+
+  Vector (int ex, int why) : x {ex}, y {why} {}
+  Vector () : x {0}, y{0} {}
+
+  Vector& operator+= (Vector v) { x += v.x, y += v.y; return *this; }
+  Vector& operator-= (Vector v) { x -= v.x, y -= v.y; return *this; }
+  Vector& operator*= (Vector v) { x *= v.x, y *= v.y; return *this; }
+};
+
+Vector operator+ (Vector, Vector);
+Vector operator- (Vector, Vector);
+Vector operator* (Vector, Vector);