n/a
Dependencies: Circle Rectangle Shape Square mbed
main.cpp@1:83e4a7cb4516, 2016-03-22 (annotated)
- Committer:
- kch78
- Date:
- Tue Mar 22 09:34:10 2016 +0000
- Revision:
- 1:83e4a7cb4516
- Parent:
- 0:2f3ce9b79d39
Przyk?ad tworzenia klas, funkcji wirtualnej.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kch78 | 0:2f3ce9b79d39 | 1 | #include "Shape.h" |
kch78 | 0:2f3ce9b79d39 | 2 | #include "Circle.h" |
kch78 | 0:2f3ce9b79d39 | 3 | #include "Rectangle.h" |
kch78 | 0:2f3ce9b79d39 | 4 | #include "Square.h" |
kch78 | 0:2f3ce9b79d39 | 5 | #include "mbed.h" |
kch78 | 0:2f3ce9b79d39 | 6 | #include <string> |
kch78 | 0:2f3ce9b79d39 | 7 | |
kch78 | 0:2f3ce9b79d39 | 8 | extern Serial pc(USBTX, USBRX); |
kch78 | 0:2f3ce9b79d39 | 9 | |
kch78 | 0:2f3ce9b79d39 | 10 | DigitalOut led(LED1); |
kch78 | 0:2f3ce9b79d39 | 11 | DigitalIn pb(USER_BUTTON); |
kch78 | 0:2f3ce9b79d39 | 12 | DigitalIn pb2(PC_8); |
kch78 | 0:2f3ce9b79d39 | 13 | |
kch78 | 0:2f3ce9b79d39 | 14 | int count = 0; |
kch78 | 0:2f3ce9b79d39 | 15 | int counter() |
kch78 | 0:2f3ce9b79d39 | 16 | { |
kch78 | 0:2f3ce9b79d39 | 17 | return ++count; |
kch78 | 0:2f3ce9b79d39 | 18 | } |
kch78 | 0:2f3ce9b79d39 | 19 | |
kch78 | 0:2f3ce9b79d39 | 20 | int counter2() |
kch78 | 0:2f3ce9b79d39 | 21 | { |
kch78 | 0:2f3ce9b79d39 | 22 | static int count2 = 0; |
kch78 | 0:2f3ce9b79d39 | 23 | return ++count2; |
kch78 | 0:2f3ce9b79d39 | 24 | pc.printf("\n\r Count2 : %d", count2); |
kch78 | 0:2f3ce9b79d39 | 25 | } |
kch78 | 0:2f3ce9b79d39 | 26 | |
kch78 | 0:2f3ce9b79d39 | 27 | |
kch78 | 0:2f3ce9b79d39 | 28 | int main() |
kch78 | 0:2f3ce9b79d39 | 29 | { |
kch78 | 0:2f3ce9b79d39 | 30 | pc.format(8,SerialBase::None,1); |
kch78 | 0:2f3ce9b79d39 | 31 | pc.baud(115200); |
kch78 | 0:2f3ce9b79d39 | 32 | |
kch78 | 0:2f3ce9b79d39 | 33 | |
kch78 | 0:2f3ce9b79d39 | 34 | |
kch78 | 0:2f3ce9b79d39 | 35 | pb.mode(PullUp); |
kch78 | 0:2f3ce9b79d39 | 36 | bool off = 0; |
kch78 | 0:2f3ce9b79d39 | 37 | |
kch78 | 0:2f3ce9b79d39 | 38 | pc.printf("\n\r START\n\r"); // napisz------------------ |
kch78 | 0:2f3ce9b79d39 | 39 | |
kch78 | 0:2f3ce9b79d39 | 40 | int licznikGuzika = 0; |
kch78 | 0:2f3ce9b79d39 | 41 | //int licznikGuzika2 = 0; |
kch78 | 0:2f3ce9b79d39 | 42 | |
kch78 | 0:2f3ce9b79d39 | 43 | struct fish { |
kch78 | 0:2f3ce9b79d39 | 44 | const char *name; |
kch78 | 0:2f3ce9b79d39 | 45 | const char *species; |
kch78 | 0:2f3ce9b79d39 | 46 | int teeth; |
kch78 | 0:2f3ce9b79d39 | 47 | int age; |
kch78 | 0:2f3ce9b79d39 | 48 | }; |
kch78 | 0:2f3ce9b79d39 | 49 | |
kch78 | 0:2f3ce9b79d39 | 50 | //-----------------------PETLA----------------------------------------------------- |
kch78 | 0:2f3ce9b79d39 | 51 | while(1) { |
kch78 | 0:2f3ce9b79d39 | 52 | |
kch78 | 0:2f3ce9b79d39 | 53 | static int licznikGuzika2 = 0; |
kch78 | 0:2f3ce9b79d39 | 54 | |
kch78 | 0:2f3ce9b79d39 | 55 | struct fish snappy = {"Snappy", "Piranha", 69, 4}; |
kch78 | 0:2f3ce9b79d39 | 56 | |
kch78 | 0:2f3ce9b79d39 | 57 | if (pb == 0) { |
kch78 | 0:2f3ce9b79d39 | 58 | off = !off; |
kch78 | 0:2f3ce9b79d39 | 59 | if (off) { |
kch78 | 0:2f3ce9b79d39 | 60 | licznikGuzika++; |
kch78 | 0:2f3ce9b79d39 | 61 | licznikGuzika2++; |
kch78 | 0:2f3ce9b79d39 | 62 | led = !led; |
kch78 | 0:2f3ce9b79d39 | 63 | |
kch78 | 0:2f3ce9b79d39 | 64 | pc.printf("\n\r Nazwa : %s", snappy.name); |
kch78 | 0:2f3ce9b79d39 | 65 | |
kch78 | 0:2f3ce9b79d39 | 66 | counter(); |
kch78 | 0:2f3ce9b79d39 | 67 | pc.printf("\n\r Count : %d", count); |
kch78 | 0:2f3ce9b79d39 | 68 | counter2(); |
kch78 | 0:2f3ce9b79d39 | 69 | |
kch78 | 0:2f3ce9b79d39 | 70 | |
kch78 | 0:2f3ce9b79d39 | 71 | Shape * papers[5]; |
kch78 | 0:2f3ce9b79d39 | 72 | |
kch78 | 0:2f3ce9b79d39 | 73 | Circle michael(1.0); |
kch78 | 0:2f3ce9b79d39 | 74 | Rectangle andrew(2.0,4.0); |
kch78 | 0:2f3ce9b79d39 | 75 | Circle ann(10); |
kch78 | 0:2f3ce9b79d39 | 76 | Square tim(100); |
kch78 | 0:2f3ce9b79d39 | 77 | Rectangle jonathan(20.0,25.0); |
kch78 | 0:2f3ce9b79d39 | 78 | |
kch78 | 0:2f3ce9b79d39 | 79 | papers[0] = &michael; papers[1] = &andrew; papers[2] = &ann; |
kch78 | 0:2f3ce9b79d39 | 80 | papers[3] = &tim; papers[4] = &jonathan; |
kch78 | 0:2f3ce9b79d39 | 81 | |
kch78 | 0:2f3ce9b79d39 | 82 | for (int k=0; k<5; k++) { |
kch78 | 0:2f3ce9b79d39 | 83 | pc.printf("\n\rArea is %f", papers[k]->getArea()); |
kch78 | 0:2f3ce9b79d39 | 84 | } |
kch78 | 0:2f3ce9b79d39 | 85 | |
kch78 | 0:2f3ce9b79d39 | 86 | |
kch78 | 0:2f3ce9b79d39 | 87 | } |
kch78 | 0:2f3ce9b79d39 | 88 | if (!off) { |
kch78 | 0:2f3ce9b79d39 | 89 | licznikGuzika++; |
kch78 | 0:2f3ce9b79d39 | 90 | licznikGuzika2++; |
kch78 | 0:2f3ce9b79d39 | 91 | led = !led; |
kch78 | 0:2f3ce9b79d39 | 92 | |
kch78 | 0:2f3ce9b79d39 | 93 | pc.printf("\n\rNazwa : %s", snappy.species); |
kch78 | 0:2f3ce9b79d39 | 94 | |
kch78 | 0:2f3ce9b79d39 | 95 | counter(); |
kch78 | 0:2f3ce9b79d39 | 96 | pc.printf("\n\r Count : %d", count); |
kch78 | 0:2f3ce9b79d39 | 97 | counter2(); |
kch78 | 0:2f3ce9b79d39 | 98 | |
kch78 | 0:2f3ce9b79d39 | 99 | |
kch78 | 0:2f3ce9b79d39 | 100 | |
kch78 | 0:2f3ce9b79d39 | 101 | } |
kch78 | 0:2f3ce9b79d39 | 102 | |
kch78 | 0:2f3ce9b79d39 | 103 | while (pb == 0); |
kch78 | 0:2f3ce9b79d39 | 104 | } |
kch78 | 0:2f3ce9b79d39 | 105 | |
kch78 | 0:2f3ce9b79d39 | 106 | |
kch78 | 0:2f3ce9b79d39 | 107 | //-------------------------CHECK------------------------------------------ |
kch78 | 0:2f3ce9b79d39 | 108 | if (pb2 == 0) { |
kch78 | 0:2f3ce9b79d39 | 109 | |
kch78 | 0:2f3ce9b79d39 | 110 | |
kch78 | 0:2f3ce9b79d39 | 111 | } |
kch78 | 0:2f3ce9b79d39 | 112 | } |
kch78 | 0:2f3ce9b79d39 | 113 | } |