커밋
841041a859
8개의 변경된 파일305개의 추가작업 그리고 0개의 파일을 삭제
  1. 20
    0
      Makefile
  2. 1
    0
      README.md
  3. 35
    0
      inh01.cpp
  4. 40
    0
      inh02.cpp
  5. 43
    0
      inh03.cpp
  6. 47
    0
      inh04.cpp
  7. 63
    0
      inh05.cpp
  8. 56
    0
      inh06.cpp

+ 20
- 0
Makefile 파일 보기

@@ -0,0 +1,20 @@
1
+all: inh01 inh02 inh03 inh04 inh05 inh06
2
+
3
+inh01: inh01.cpp
4
+	g++ -o inh01 inh01.cpp -std=c++11
5
+
6
+inh02: inh02.cpp
7
+	g++ -o inh02 inh02.cpp -std=c++11
8
+
9
+inh03: inh03.cpp
10
+	g++ -o inh03 inh03.cpp -std=c++11
11
+
12
+inh04: inh04.cpp
13
+	g++ -o inh04 inh04.cpp -std=c++11
14
+
15
+inh05: inh05.cpp
16
+	g++ -o inh05 inh05.cpp -std=c++11
17
+
18
+inh06: inh06.cpp
19
+	g++ -o inh06 inh06.cpp -std=c++11
20
+

+ 1
- 0
README.md 파일 보기

@@ -0,0 +1 @@
1
+### Examples for inheritance CCOM 3034

+ 35
- 0
inh01.cpp 파일 보기

@@ -0,0 +1,35 @@
1
+// Example showing a base class (Shape) and a derived 
2
+// class Rectangle. Notice how Rectangle inherits Shape's
3
+// display function.
4
+
5
+#include <iostream>
6
+
7
+using namespace std;
8
+
9
+class Shape {
10
+  protected:
11
+    int width, height;
12
+  public:
13
+    Shape() { width = height = 0; }
14
+    Shape(int w, int h) { width = w; height = h; }
15
+    void display() const;
16
+};
17
+
18
+void Shape::display() const {
19
+  cout << width << " " << height << endl; 
20
+}
21
+
22
+
23
+class Rectangle : public Shape {
24
+  public:
25
+    Rectangle() {}
26
+    Rectangle(int w, int h) : Shape(w, h) {}
27
+};
28
+
29
+int main() {
30
+  Rectangle r;
31
+  Rectangle q(4,5);
32
+  r.display();
33
+  q.display();
34
+  return 0;
35
+} 

+ 40
- 0
inh02.cpp 파일 보기

@@ -0,0 +1,40 @@
1
+// We have added a new member function (perimeter) to the 
2
+// subclass Rectangle. Subclasses can have many more data members
3
+// and member functions besides the ones they inherit from their
4
+// base class.
5
+
6
+#include <iostream>
7
+using namespace std;
8
+
9
+class Shape {
10
+  protected:
11
+    int width, height;
12
+  public:
13
+    Shape() { width = height = 0; }
14
+    Shape(int w, int h) { width = w; height = h; }
15
+    void display() const;
16
+};
17
+
18
+void Shape::display() const {
19
+  cout << width << " " << height << endl; 
20
+}
21
+
22
+class Rectangle : public Shape {
23
+  public:
24
+    Rectangle() {}
25
+    Rectangle(int w, int h) : Shape(w, h) { }
26
+    int perimeter() const;
27
+};
28
+
29
+int Rectangle::perimeter() const{
30
+  return 2 * width + 2 * height;
31
+}
32
+
33
+int main() {
34
+  Rectangle r;
35
+  Rectangle q(4,5);
36
+  r.display();
37
+  q.display();
38
+  cout << "Perimeter of q is: " << q.perimeter() << endl;
39
+  return 0;
40
+} 

+ 43
- 0
inh03.cpp 파일 보기

@@ -0,0 +1,43 @@
1
+// We have overloaded the display function of Rectangle
2
+
3
+#include <iostream>
4
+using namespace std;
5
+
6
+class Shape {
7
+  protected:
8
+    int width, height;
9
+  public:
10
+    Shape() { width = height = 0; }
11
+    Shape(int w, int h) { width = w; height = h; }
12
+    void display() const;
13
+};
14
+
15
+void Shape::display() const {
16
+  cout << width << " " << height << endl; 
17
+}
18
+
19
+class Rectangle : public Shape {
20
+  public:
21
+    Rectangle() {}
22
+    Rectangle(int w, int h) : Shape(w, h) { }
23
+    int perimeter() const;
24
+    void display() const;
25
+};
26
+
27
+int Rectangle::perimeter() const{
28
+  return 2 * width + 2 * height;
29
+}
30
+
31
+void Rectangle::display() const {
32
+  cout << "I am rectangle of width " << width 
33
+       << " and height " << height << endl;
34
+}
35
+
36
+int main() {
37
+  Rectangle r;
38
+  Rectangle q(4,5);
39
+  r.display();
40
+  q.display();
41
+  cout << "Perimeter of q is: " << q.perimeter() << endl;
42
+  return 0;
43
+} 

+ 47
- 0
inh04.cpp 파일 보기

@@ -0,0 +1,47 @@
1
+// Trying to invoke the display function from a pointer.
2
+
3
+#include <iostream>
4
+using namespace std;
5
+
6
+class Shape {
7
+  protected:
8
+    int width, height;
9
+  public:
10
+    Shape() { width = height = 0; }
11
+    Shape(int w, int h) { width = w; height = h; }
12
+    void display() const;
13
+};
14
+
15
+void Shape::display() const {
16
+  cout << width << " " << height << endl; 
17
+}
18
+
19
+class Rectangle : public Shape {
20
+  public:
21
+    Rectangle() {}
22
+    Rectangle(int w, int h) : Shape(w, h) { }
23
+    int perimeter() const;
24
+    void display() const;
25
+};
26
+
27
+int Rectangle::perimeter() const{
28
+  return 2 * width + 2 * height;
29
+}
30
+
31
+void Rectangle::display() const {
32
+  cout << "I am rectangle of width " << width 
33
+       << " and height " << height << endl;
34
+}
35
+
36
+int main() {
37
+
38
+    Shape *p;
39
+    Shape *s;
40
+    p = new Rectangle(4,8);
41
+    s = new Shape(3,5);
42
+
43
+    s->display();
44
+    p->display();
45
+
46
+  return 0;
47
+} 

+ 63
- 0
inh05.cpp 파일 보기

@@ -0,0 +1,63 @@
1
+// Trying to invoke the display function from a pointer, but
2
+// now display is virtual function.
3
+
4
+#include <iostream>
5
+using namespace std;
6
+
7
+class Shape {
8
+  protected:
9
+    int width, height;
10
+  public:
11
+    Shape() { width = height = 0; }
12
+    Shape(int w, int h) { width = w; height = h; }
13
+    virtual void display() const;
14
+};
15
+
16
+void Shape::display() const {
17
+  cout << width << " " << height << endl; 
18
+}
19
+
20
+class Rectangle : public Shape {
21
+  public:
22
+    Rectangle() {}
23
+    Rectangle(int w, int h) : Shape(w, h) { }
24
+    int perimeter() const;
25
+    void display() const;
26
+};
27
+
28
+int Rectangle::perimeter() const{
29
+  return 2 * width + 2 * height;
30
+}
31
+
32
+void Rectangle::display() const {
33
+  cout << "I am rectangle of width " << width 
34
+       << " and height " << height << endl;
35
+}
36
+
37
+class Square: public Rectangle {
38
+  public:
39
+    Square() {}
40
+    Square(int w) : Rectangle(w,w) {}
41
+    void display() const;
42
+};
43
+
44
+void Square::display() const {
45
+  cout << "I am square of side " << width << endl;
46
+}
47
+
48
+
49
+int main() {
50
+
51
+    Shape *p;
52
+    Shape *s;
53
+    p = new Rectangle(4,8);
54
+    s = new Shape(3,5);
55
+
56
+    s->display();
57
+    p->display();
58
+
59
+    Shape *q = new Square(10);
60
+    q->display();
61
+
62
+  return 0;
63
+} 

+ 56
- 0
inh06.cpp 파일 보기

@@ -0,0 +1,56 @@
1
+#include <iostream>
2
+using namespace std;
3
+
4
+class Shape {
5
+  protected:
6
+    int width, height;
7
+  public:
8
+    Shape() { width = height = 0; }
9
+    Shape(int w, int h) { width = w; height = h; }
10
+    virtual void display() const;
11
+};
12
+
13
+void Shape::display() const {
14
+  cout << width << " " << height << endl; 
15
+}
16
+
17
+class Rectangle : public Shape {
18
+  public:
19
+    Rectangle() {}
20
+    Rectangle(int w, int h) : Shape(w, h) { }
21
+    int perimeter() const;
22
+    void display() const;
23
+};
24
+
25
+int Rectangle::perimeter() const{
26
+  return 2 * width + 2 * height;
27
+}
28
+
29
+void Rectangle::display() const {
30
+  cout << "I am rectangle of width " << width 
31
+       << " and height " << height << endl;
32
+}
33
+
34
+class Triangle: public Shape {
35
+  public:
36
+    Triangle() {}
37
+    Triangle(int w, int h) : Shape(w,h) {}
38
+    void display() const;
39
+};
40
+
41
+void Triangle::display() const {
42
+  cout << "I am triangle of side " << width << endl;
43
+}
44
+
45
+
46
+int main() {
47
+    Shape* a[4];
48
+    a[0] = new Rectangle(3,4);
49
+    a[1] = new Triangle(5,7);
50
+    a[2] = new Rectangle(10,5);
51
+    a[3] = new Triangle(2,8);
52
+
53
+    for (auto e: a) {
54
+        e->display();
55
+    }
56
+}