summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2012-04-11 21:36:55 -0500
committerKyle Kaminski <kyle@kkaminsk.com>2012-04-11 21:36:55 -0500
commite8244aab814168304da11283b52608c81f75d346 (patch)
treee5ccbde1c5469583f890a67011c76334cea4e684
downloadc++-e8244aab814168304da11283b52608c81f75d346.tar.gz
c++-e8244aab814168304da11283b52608c81f75d346.tar.bz2
c++-e8244aab814168304da11283b52608c81f75d346.zip
initial import
-rw-r--r--0-class_sample.cc36
-rw-r--r--abstract.cpp74
-rw-r--r--class_ref.cpp59
-rw-r--r--mytime0.h60
-rw-r--r--primerplus11.6-oprt_ovrlding.cc30
-rw-r--r--sec16.1-template_func.cc22
-rw-r--r--sec3.4-vector_string_iterator.cpp23
-rw-r--r--sec4.4-dynamic_mem.cpp47
-rw-r--r--sec6.13-throw_try_catch.cpp19
-rw-r--r--template_definitions.cpp37
10 files changed, 407 insertions, 0 deletions
diff --git a/0-class_sample.cc b/0-class_sample.cc
new file mode 100644
index 0000000..1a2d87b
--- /dev/null
+++ b/0-class_sample.cc
@@ -0,0 +1,36 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+class Simple
+{
+ public:
+ Simple(const int &c_age, const string &c_dest) : age(c_age), dest(c_dest) {}
+ ~Simple() { cout << "Bye!\n"; }
+
+ const int& ageRet(void) const;
+ const string& destRet(void) const;
+ private:
+ const int age;
+ const string dest;
+};
+
+const int& Simple::ageRet(void) const
+{
+ return age;
+}
+
+const string& Simple::destRet(void) const
+{
+ return dest;
+}
+
+int main(void)
+{
+ Simple *ppl_me = new Simple(21, "Truth");
+ Simple ppl_cpy = *ppl_me;
+ cout << ppl_cpy.ageRet() << endl;
+
+ return 0;
+}
diff --git a/abstract.cpp b/abstract.cpp
new file mode 100644
index 0000000..99edd52
--- /dev/null
+++ b/abstract.cpp
@@ -0,0 +1,74 @@
+#include <iostream>
+#include <string>
+
+/* below tweak needs to know wbout this class */
+template <class T>
+class Polygon;
+
+/* convine the compiler that overloaded operators are templates themselves before
+ * we reach the declaration of the class */
+template <class T>
+std::ostream& operator<<(std::ostream &, const Polygon<T> &);
+
+template <class T>
+class Polygon
+{
+ public:
+ /* notice the <> after function name, we're letting g++ know this is a template */
+ friend std::ostream& operator<<<>(std::ostream &, const Polygon<T> &);
+
+ /* pure virtual, subclass must DEFINE it. otherwise that subclass itself
+ * will remain abstract and won't be able to be instantiated */
+ virtual T area(void) = 0;
+
+ T getWidth(void) {
+ this->width;
+ }
+
+ T getHeight(void) {
+ this->height;
+ }
+
+ protected:
+ T width, height;
+ std::string type; /* name of polygon */
+};
+
+template <class T>
+class Triangle : public Polygon<T>
+{
+ public:
+ Triangle() {
+ this->width = 0;
+ this->height = 0;
+ this->type = std::string("triangle");
+ }
+
+ Triangle(int b, int h) {
+ this->width = b;
+ this->height = h; /* this keyword has to be used? */
+ this->type = std::string("triangle");
+ }
+
+ virtual T area(void) {
+ return (this->width * this->height) / 2;
+ }
+};
+
+template <class T>
+std::ostream& operator<<(std::ostream& stream, const Polygon<T>& p)
+{
+ stream << p.type << " dimensions, width: " << p.width << ", height: " << p.height
+ << ", area: "<< const_cast<Polygon<T> *>(&p)->area(); /* lol at the cast! */
+
+ return stream;
+}
+
+int main(int argc, char *argv[])
+{
+ Triangle<int> tri(6, 4);
+ std::cout << tri << std::endl;
+
+ return 0;
+}
+
diff --git a/class_ref.cpp b/class_ref.cpp
new file mode 100644
index 0000000..179c967
--- /dev/null
+++ b/class_ref.cpp
@@ -0,0 +1,59 @@
+#include <iostream>
+#include <cstdlib>
+
+class Drink
+{
+ public:
+ Drink(int cost)
+ {
+ this->cost = cost;
+ }
+
+
+ int get_cost(void);
+
+ private:
+
+ protected:
+ int cost;
+};
+
+int Drink::get_cost(void)
+{
+ return cost;
+}
+
+class Juice : public Drink
+{
+ public:
+ Juice(int shelf, int cost) : Drink(cost), shelf(shelf)
+ {
+ std::cout << "got called\n";
+ }
+
+ int get_shelf(void);
+
+ private:
+ int shelf;
+
+ protected:
+};
+
+int Juice::get_shelf(void)
+{
+ return shelf;
+}
+
+int main(int argc, char **argv)
+{
+ Juice *dubjuice = new Juice(1, 5);
+ std::cout << dubjuice->get_cost() << std::endl;
+ std::cout << dubjuice->get_shelf() << std::endl;
+
+ Juice ref = *dubjuice;
+ std::cout << ref.get_cost() << std::endl;
+ std::cout << ref.get_shelf() << std::endl;
+
+ return 0;
+}
+
diff --git a/mytime0.h b/mytime0.h
new file mode 100644
index 0000000..eeffe55
--- /dev/null
+++ b/mytime0.h
@@ -0,0 +1,60 @@
+#ifndef MYTIME0_H_
+#define MYTIME0_H_
+
+class Time
+{
+private:
+ int hours;
+ int minutes;
+public:
+ Time();
+ Time(int h, int m = 0);
+ void AddMin(int m);
+ void AddHr(int h);
+ void Reset(int h = 0, int m = 0);
+ Time operator+(const Time & t) const;
+ void Show() const;
+};
+
+Time::Time()
+{
+ hours = minutes = 0;
+}
+
+Time::Time(int h, int m )
+{
+ hours = h;
+ minutes = m;
+}
+void Time::AddMin(int m)
+{
+ minutes += m;
+ hours += minutes / 60;
+ minutes %= 60;
+}
+void Time::AddHr(int h)
+{
+ hours += h;
+}
+
+void Time::Reset(int h, int m)
+{
+ hours = h;
+ minutes = m;
+}
+
+Time Time::operator+(const Time & t) const
+{
+ Time sum;
+ sum.minutes = minutes + t.minutes;
+ sum.hours = hours + t.hours + sum.minutes / 60;
+ sum.minutes %= 60;
+ return sum;
+}
+
+void Time::Show() const
+{
+ std::cout << hours << " hours, " << minutes << " minutes";
+}
+
+#endif
diff --git a/primerplus11.6-oprt_ovrlding.cc b/primerplus11.6-oprt_ovrlding.cc
new file mode 100644
index 0000000..6c4b147
--- /dev/null
+++ b/primerplus11.6-oprt_ovrlding.cc
@@ -0,0 +1,30 @@
+#include <iostream>
+#include "mytime0.h"
+
+int main(void)
+{
+ using std::cout;
+ using std::endl;
+
+ Time planning;
+ Time coding(2, 40);
+ Time fixing(5, 55);
+ Time total;
+
+ cout << "planning time = "; planning.Show(); cout << endl;
+ cout << "coding time = "; coding.Show(); cout << endl;
+ cout << "fixing time = "; fixing.Show(); cout << endl;
+ total = coding + fixing;
+
+ // operator notation
+ cout << "coding + fixing = "; total.Show(); cout << endl;
+
+ Time morefixing(3, 28);
+ cout << "more fixing time = "; morefixing.Show(); cout << endl;
+ total = morefixing.operator+(total);
+
+ // function notation
+ cout << "morefixing.operator+(total) = "; total.Show(); cout << endl;
+
+ return 0;
+}
diff --git a/sec16.1-template_func.cc b/sec16.1-template_func.cc
new file mode 100644
index 0000000..3c5d293
--- /dev/null
+++ b/sec16.1-template_func.cc
@@ -0,0 +1,22 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename T>
+int compare(const T &v1, const T &v2)
+{
+ if (v1 < v2)
+ return -1;
+ if (v2 < v1)
+ return 1;
+ return 0;
+}
+
+int main(void)
+{
+ cout << compare(1, 0) << endl;
+ cout << compare(string("hi"), string("world")) << endl;
+
+ return 0;
+}
diff --git a/sec3.4-vector_string_iterator.cpp b/sec3.4-vector_string_iterator.cpp
new file mode 100644
index 0000000..23ddb83
--- /dev/null
+++ b/sec3.4-vector_string_iterator.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+using std::string;
+using std::vector;
+using std::cin;
+using std::cout;
+using std::endl;
+
+int main(int argc, char **argv)
+{
+ string word;
+ vector<string> text;
+
+ while (cin >> word)
+ text.push_back(word);
+
+ for (vector<string>::const_iterator iter = text.begin(); iter != text.end(); ++iter)
+ cout << *iter << endl;
+
+ return 0;
+}
diff --git a/sec4.4-dynamic_mem.cpp b/sec4.4-dynamic_mem.cpp
new file mode 100644
index 0000000..9e7b3e4
--- /dev/null
+++ b/sec4.4-dynamic_mem.cpp
@@ -0,0 +1,47 @@
+#include <iostream>
+#include <string>
+#include <vector>
+
+using std::string;
+using std::vector;
+using std::cin;
+using std::cout;
+using std::endl;
+
+static void a_func(void)
+{
+ string name("Kamil Kaminski");
+ name += "\n";
+ cout << name;
+}
+
+static void b_func(void)
+{
+ string dog = "Hera";
+ const char *str = dog.c_str();
+ cout << str << endl;
+}
+
+int main(int argc, char **argv)
+{
+ int *ip = new int[21];
+
+ for (int i = 0; i != 21; ++i)
+ ip[i] = i;
+
+ for (int i = 0; i != 21; ++i)
+ {
+ if (((i+1) % 4 == 0) && i != 0)
+ cout << ip[i] << "\n";
+ else
+ cout << ip[i] << " ";
+ }
+ cout << endl;
+
+ delete [] ip;
+
+ a_func();
+ b_func();
+
+ return 0;
+}
diff --git a/sec6.13-throw_try_catch.cpp b/sec6.13-throw_try_catch.cpp
new file mode 100644
index 0000000..bb6d16e
--- /dev/null
+++ b/sec6.13-throw_try_catch.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+
+int main(void)
+{
+ const int x = 0x0;
+
+ try
+ {
+ if (x == 0)
+ throw "throwing a string";
+ }
+
+ catch (const char *str)
+ {
+ std::cerr << str;
+ }
+
+ return 0;
+}
diff --git a/template_definitions.cpp b/template_definitions.cpp
new file mode 100644
index 0000000..ddacfb6
--- /dev/null
+++ b/template_definitions.cpp
@@ -0,0 +1,37 @@
+/* template definition should reside in a header file, just like inline functions */
+
+template <class T>
+class Foo
+{
+ public:
+ Foo();
+ Foo(const Foo &);
+ Foo(T &);
+ private:
+ T e;
+};
+
+template <class T>
+Foo<T>::Foo() : e(0)
+{
+}
+
+template <class T>
+Foo<T>::Foo(const Foo &f)
+{
+ this->e = f.e;
+}
+
+template <class T>
+Foo<T>::Foo(T& e)
+{
+ this->e = e;
+}
+
+int main(int argc, char *argv[])
+{
+ Foo<int> foo;
+
+ return 0;
+}
+