練習用です。いろんなものがごちゃまぜです。
(empty log message)
| @@ -4,7 +4,7 @@ | ||
| 4 | 4 | class samp{ |
| 5 | 5 | int i,j; |
| 6 | 6 | public: |
| 7 | - void set_ij(int a, int b){i = a;j = b;} | |
| 7 | + samp(int a, int b){i = a;j = b;} | |
| 8 | 8 | int get_product(){return i*j;} |
| 9 | 9 | }; |
| 10 | 10 |
| @@ -11,13 +11,11 @@ | ||
| 11 | 11 | int main() |
| 12 | 12 | { |
| 13 | 13 | samp *p; |
| 14 | - p = new samp; | |
| 14 | + p = new samp(6,5); | |
| 15 | 15 | if(!p){ |
| 16 | 16 | cout << "メモリ割り当てエラー\n"; |
| 17 | 17 | return 1; |
| 18 | 18 | } |
| 19 | - | |
| 20 | - p->set_ij(4,5); | |
| 21 | 19 | cout << "積は:" << p->get_product() << "\n"; |
| 22 | 20 | delete p; |
| 23 | 21 | return 0; |
| @@ -4,13 +4,12 @@ | ||
| 4 | 4 | int main() |
| 5 | 5 | { |
| 6 | 6 | int *p; |
| 7 | - p = new int; | |
| 7 | + p = new int(9); | |
| 8 | 8 | if (!p){ |
| 9 | 9 | cout << "メモリ割り当てエラー\n"; |
| 10 | 10 | return 1; |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | - *p = 1000; | |
| 14 | 13 | cout << "pがさしている整数型は:" << *p << "\n"; |
| 15 | 14 | delete p; |
| 16 | 15 | return 0; |
| @@ -0,0 +1,31 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +void swapargs(int &x,int &y); | |
| 5 | + | |
| 6 | +int main() | |
| 7 | +{ | |
| 8 | + int i, j; | |
| 9 | + i = 10; | |
| 10 | + j = 19; | |
| 11 | + | |
| 12 | + cout << "i:" << i << ", "; | |
| 13 | + cout << "j:" << j << "\n"; | |
| 14 | + | |
| 15 | + swapargs(i,j); | |
| 16 | + | |
| 17 | + cout << "交換後: "; | |
| 18 | + cout << "i;" << i << ", "; | |
| 19 | + cout << "j:" << j << "\n"; | |
| 20 | + | |
| 21 | + return 0; | |
| 22 | +} | |
| 23 | + | |
| 24 | +void swapargs(int &x,int &y) | |
| 25 | +{ | |
| 26 | + int t; | |
| 27 | + t = x; | |
| 28 | + x = y; | |
| 29 | + y = t; | |
| 30 | +} | |
| 31 | + | |
| \ No newline at end of file |
| @@ -0,0 +1,18 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +void f(int &n); | |
| 5 | + | |
| 6 | +int main() | |
| 7 | +{ | |
| 8 | + int i = 0; | |
| 9 | + f(i); | |
| 10 | + cout << "iの新しい値:" << i << "\n"; | |
| 11 | + | |
| 12 | + return 0; | |
| 13 | +} | |
| 14 | + | |
| 15 | +void f(int &n) | |
| 16 | +{ | |
| 17 | + n = 100; | |
| 18 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,25 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +class myclass { | |
| 5 | + int who; | |
| 6 | +public: | |
| 7 | + myclass(int n){ | |
| 8 | + who = n; | |
| 9 | + cout << "コンストラクタの呼び出し " << who << "\n"; | |
| 10 | + } | |
| 11 | + ~myclass(){cout << "デストラクタの呼び出し" << who << "\n";} | |
| 12 | + int id(){return who;} | |
| 13 | +}; | |
| 14 | + | |
| 15 | +void f(myclass o) | |
| 16 | +{ | |
| 17 | + cout << "受け取り" << o.id() << "\n"; | |
| 18 | +} | |
| 19 | + | |
| 20 | +int main() | |
| 21 | +{ | |
| 22 | + myclass x(1); | |
| 23 | + f(x); | |
| 24 | + return 0; | |
| 25 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,17 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +int &f(); | |
| 5 | +int x; | |
| 6 | + | |
| 7 | +int main() | |
| 8 | +{ | |
| 9 | + f() = 100; | |
| 10 | + cout << x << "\n"; | |
| 11 | + return 0; | |
| 12 | +} | |
| 13 | + | |
| 14 | +int &f() | |
| 15 | +{ | |
| 16 | + return x; | |
| 17 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,26 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +int main() | |
| 5 | +{ | |
| 6 | + int *p; | |
| 7 | + p = new int [5]; | |
| 8 | + | |
| 9 | + if(!p){ | |
| 10 | + cout << "メモリ割り当てエラー\n"; | |
| 11 | + return 1; | |
| 12 | + } | |
| 13 | + | |
| 14 | + for(int i = 0; i < 5; i++){ | |
| 15 | + p[i] = i; | |
| 16 | + } | |
| 17 | + | |
| 18 | + for(int i = 0; i < 5; i++){ | |
| 19 | + cout << "整数型p[" << i << "]は:"; | |
| 20 | + cout << p[i] << "\n"; | |
| 21 | + } | |
| 22 | + | |
| 23 | + delete [] p; | |
| 24 | + | |
| 25 | + return 0; | |
| 26 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,14 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +#include <string.h> | |
| 3 | + | |
| 4 | +using namespace std; | |
| 5 | + | |
| 6 | +int main() | |
| 7 | +{ | |
| 8 | + char *p = new char[100]; | |
| 9 | + strcpy(p,"This is a test"); | |
| 10 | + | |
| 11 | + cout << p << "\n"; | |
| 12 | + | |
| 13 | + return 0; | |
| 14 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,23 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +void neg(int &num); | |
| 5 | +void neg(int *num); | |
| 6 | + | |
| 7 | +void neg(int &num){ | |
| 8 | + num = -num; | |
| 9 | +} | |
| 10 | + | |
| 11 | +void neg(int *num){ | |
| 12 | + *num = -*num; | |
| 13 | +} | |
| 14 | +int main() | |
| 15 | +{ | |
| 16 | + int num = 20; | |
| 17 | + neg(num); | |
| 18 | + cout << num << ' '; | |
| 19 | + neg(&num); | |
| 20 | + cout << num << ' '; | |
| 21 | + | |
| 22 | + return 0; | |
| 23 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,47 @@ | ||
| 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(char *ptr); | |
| 11 | + ~strtype(); | |
| 12 | + char *get(){return p;} | |
| 13 | +}; | |
| 14 | + | |
| 15 | +strtype::strtype(char *ptr) | |
| 16 | +{ | |
| 17 | + int l = strlen(ptr); | |
| 18 | + p = new char[l]; | |
| 19 | + if (!p) { | |
| 20 | + cout << "メモリ割り当てエラー\n"; | |
| 21 | + exit(1); | |
| 22 | + } | |
| 23 | + strcpy(this->p, ptr); | |
| 24 | + this->len = l; | |
| 25 | +} | |
| 26 | + | |
| 27 | +strtype::~strtype() | |
| 28 | +{ | |
| 29 | + cout << "pを解放する。\n"; | |
| 30 | + delete [] p; | |
| 31 | +} | |
| 32 | + | |
| 33 | +void show(strtype &x) | |
| 34 | +{ | |
| 35 | + char *s; | |
| 36 | + s = x.get(); | |
| 37 | + cout << s << "\n"; | |
| 38 | +} | |
| 39 | + | |
| 40 | +int main() | |
| 41 | +{ | |
| 42 | + strtype a("Hello"), b("There"); | |
| 43 | + show(a); | |
| 44 | + show(b); | |
| 45 | + | |
| 46 | + return 0; | |
| 47 | +} | |
| \ No newline at end of file |
| @@ -0,0 +1,29 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +#include <cmath> | |
| 3 | +using namespace std; | |
| 4 | + | |
| 5 | +void my_round(double &num); | |
| 6 | + | |
| 7 | +int main() | |
| 8 | +{ | |
| 9 | + double i = 100.4; | |
| 10 | + cout << i << "を丸めると "; | |
| 11 | + my_round(i); | |
| 12 | + cout << i << "\n"; | |
| 13 | + | |
| 14 | + i = 10.9; | |
| 15 | + cout << i << "を丸めると "; | |
| 16 | + my_round(i); | |
| 17 | + cout << i << "\n"; | |
| 18 | + | |
| 19 | + return 0; | |
| 20 | +} | |
| 21 | + | |
| 22 | +void my_round(double &num) | |
| 23 | +{ | |
| 24 | + double frac; | |
| 25 | + double val; | |
| 26 | + frac = modf(num, &val); | |
| 27 | + if(frac < 0.5) num = val; | |
| 28 | + else num = val + 1.0; | |
| 29 | +}; | |
| \ No newline at end of file |
| @@ -0,0 +1,9 @@ | ||
| 1 | +#include <iostream> | |
| 2 | +using namespace std; | |
| 3 | + | |
| 4 | +int main() | |
| 5 | +{ | |
| 6 | + double *d = new double(-123.099); | |
| 7 | + cout << *d << "\n"; | |
| 8 | + return 0; | |
| 9 | +} | |
| \ No newline at end of file |