Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <iostream>
using namespace std;
class rock;
class ball {
int m; //質量
public:
void set_m(int x) { m = x; }
int get_m(){ return m;}
};
class rock {
int m; //質量
public:
rock(int x) { m = x; }
int get_m(){ return m;}
};
class image {
int width, height;
int pixel;
public:
image(int x, int y) { width = x, height = y; }
~image() { cout << "デストラクタの呼び出し" << endl; }
int get_width() { return width; }
int get_height() { return height; }
void set_pixels(int &p) { pixel = p; }
int get_pixels() { return pixel; }
};
int main() {
//オブジェクトの配列
ball ba[4];
rock ro[4] = {1039,3494,992,1167 };
for (int i = 0; i < 4; i++) { ba[i].set_m(i*i); }
for (int i = 0;i < 4; i++) {cout << ba[i].get_m() << endl; }
for (int i = 0;i < 4; i++) {cout << ro[i].get_m() << endl; }
image img[4] = {
image(320,160),
image(500,500),
image(1024,800),
image(9999,2222)
};
image *img_p;
img_p = img;
for (int i = 0; i < 4; i++) {
cout << "Image No." << i << " height:"<< img_p->get_height() << endl;
cout << "Image No." << i << " width:" << img_p->get_width() << endl;
img_p++; //次のオブジェクトを指す
}
//new演算子によるメモリ割当て
int *p;
p = new int;
if (!p) {
cout << "メモリ割当てエラー" << endl;
return 1;
}
*p = 19239;
cout << "pが指している整数型は: " << *p << endl;
delete p;
//new演算子によるメモリ割当て(オブジェクト)
image *img_ex;
img_ex = new image(9999, 2222);
if (!img_ex){
cout << "メモリ割当てエラー" << endl;
return 1;
}
cout << "Image" << " height:" << img_ex->get_height() << endl;
cout << "Image" << " width:" << img_ex->get_width() << endl;
delete img_ex;
//参照仮引数
int pxl = 100000;
image *img_ex2 = new image(100, 100);
img_ex2->set_pixels(pxl);
cout << img_ex2->get_pixels() << endl;
delete img_ex2;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment