練習用です。いろんなものがごちゃまぜです。
(empty log message)
| @@ -0,0 +1,25 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class myclass { | |
| 5 | + int x; | |
| 6 | +public: | |
| 7 | + myclass(){x = 0;} | |
| 8 | + myclass(int n){x = n;} | |
| 9 | + int getx(){return x;} | |
| 10 | +}; | |
| 11 | + | |
| 12 | +int main() | |
| 13 | +{ | |
| 14 | + myclass o1[10]; | |
| 15 | + myclass o2[10] = {1,2,3,4,5,6,7,8,9,10}; | |
| 16 | + | |
| 17 | + for(int i = 0; i < 10; i++){ | |
| 18 | + cout << "o1[" << i << "]: " << o1[i].getx(); | |
| 19 | + cout << "\n"; | |
| 20 | + cout << "o2[" << i << "]: " << o2[i].getx(); | |
| 21 | + cout << "\n"; | |
| 22 | + } | |
| 23 | + | |
| 24 | + return 0; | |
| 25 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,33 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +#include <cstdio> | |
| 3 | +using namespace std; | |
| 4 | + | |
| 5 | +class date { | |
| 6 | + int day, month , year; | |
| 7 | +public: | |
| 8 | + date(char *str); | |
| 9 | + date(int m,int d, int y){ | |
| 10 | + day = d; | |
| 11 | + month = m; | |
| 12 | + year = y; | |
| 13 | + } | |
| 14 | + void show(){ | |
| 15 | + cout << month << '/' << day << '/'; | |
| 16 | + cout << year << "\n"; | |
| 17 | + } | |
| 18 | +}; | |
| 19 | +date::date(char *str) | |
| 20 | +{ | |
| 21 | + sscanf(str, "%d%*c%d%*c%d", &month, &day, &year); | |
| 22 | +} | |
| 23 | + | |
| 24 | +int main() | |
| 25 | +{ | |
| 26 | + date sdate("12/31/99"); | |
| 27 | + date idate(12,31,99); | |
| 28 | + | |
| 29 | + sdate.show(); | |
| 30 | + idate.show(); | |
| 31 | + | |
| 32 | + return 0; | |
| 33 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,61 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +#include <cstring> | |
| 3 | +#include <cstdlib> | |
| 4 | +using namespace std; | |
| 5 | + | |
| 6 | +class strtype { | |
| 7 | + char *p; | |
| 8 | + int len; | |
| 9 | +public: | |
| 10 | + strtype(); | |
| 11 | + strtype(char *ptr,int bytes); | |
| 12 | + ~strtype(); | |
| 13 | + void show(); | |
| 14 | + char *getstring(){return p;} | |
| 15 | + char getlength(){return len;} | |
| 16 | +}; | |
| 17 | + | |
| 18 | +strtype::strtype(char *ptr,int bytes) | |
| 19 | +{ | |
| 20 | + len = bytes; | |
| 21 | + p = new char[len]; | |
| 22 | + if (!p){ | |
| 23 | + cout << "メモリ割り当てエラー\n"; | |
| 24 | + exit(1); | |
| 25 | + } | |
| 26 | + strncpy(p, ptr, len); | |
| 27 | +} | |
| 28 | +strtype::strtype() | |
| 29 | +{ | |
| 30 | + len = 255; | |
| 31 | + p = new char[len]; | |
| 32 | + for(int i = 0;i < len; i++) | |
| 33 | + { | |
| 34 | + p[i] = '\0'; | |
| 35 | + } | |
| 36 | +} | |
| 37 | + | |
| 38 | +strtype::~strtype() | |
| 39 | +{ | |
| 40 | + cout << "pを解放する。\n"; | |
| 41 | + delete p; | |
| 42 | +} | |
| 43 | + | |
| 44 | + | |
| 45 | +void strtype::show() | |
| 46 | +{ | |
| 47 | + cout << p << "-長さ:" << len << "\n"; | |
| 48 | +} | |
| 49 | + | |
| 50 | +int main() | |
| 51 | +{ | |
| 52 | + strtype s1("This is a test.",10), s2("I like C++.",5),s3; | |
| 53 | + s1.show(); | |
| 54 | + s2.show(); | |
| 55 | + s3.show(); | |
| 56 | + cout << s1.getstring() << "\n"; | |
| 57 | + cout << s2.getstring() << "\n"; | |
| 58 | + cout << s3.getstring() << "\n"; | |
| 59 | + | |
| 60 | + return 0; | |
| 61 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,26 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class myclass { | |
| 5 | + int x; | |
| 6 | +public: | |
| 7 | + myclass(){x = 0;} | |
| 8 | + myclass(int n){x = n;} | |
| 9 | + int getx(){return x;} | |
| 10 | + void setx(int n){x = n;} | |
| 11 | +}; | |
| 12 | + | |
| 13 | +int main() | |
| 14 | +{ | |
| 15 | + myclass *p; | |
| 16 | + myclass ob(10); | |
| 17 | + | |
| 18 | + p = new myclass[10] {1,2,3,4,5,6,7,8,9,10}; | |
| 19 | + | |
| 20 | + for(int i = 0; i < 10; i++){ | |
| 21 | + cout << "p[" << i << "]: " << p[i].getx(); | |
| 22 | + cout << "\n"; | |
| 23 | + } | |
| 24 | + | |
| 25 | + return 0; | |
| 26 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,21 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class myclass { | |
| 5 | + int x; | |
| 6 | +public: | |
| 7 | + myclass(){x = 0;} | |
| 8 | + myclass(int n){x = n;} | |
| 9 | + int getx(){return x;} | |
| 10 | +}; | |
| 11 | + | |
| 12 | +int main() | |
| 13 | +{ | |
| 14 | + myclass o1(10); | |
| 15 | + myclass o2; | |
| 16 | + | |
| 17 | + cout << "o1: " << o1.getx() << "\n"; | |
| 18 | + cout << "o2: " << o2.getx() << "\n"; | |
| 19 | + | |
| 20 | + return 0; | |
| 21 | +} | |
| \ No newline at end of file |