object pointer // object array // new and delete
#include <iostream>
class Object {
public:
int x;
int y;
};
int main() {
Object pt1;
Object* pt2;
pt2 = &pt1;
pt2->x=1; // same as (*pt2).x=1;
return 0;
}
#include <iostream>
class Object {
int x;
int y;
public :
Object(){x=0;y=0;};
void setObject(int x, int y){
this->x=x;
this->y=y;
}
};
int main() {
Object p1;
p1.setObject(3,5);
}
#include <iostream>
class Object {
public:
int x;
int y;
};
int main() {
Object arr[3]; // Object array
arr[0].x = 1;
arr[0].y = 2;
arr[1].x = 3;
arr[1].y = 4;
arr[2].x = 5;
arr[2].y = 6;
for (int i = 0; i < 3; i++) {
std::cout << "Object " << i+1 << ": x = " << arr[i].x << ", y = " << arr[i].y << std::endl;
}
return 0;
}
#include <iostream>
class Object {
public:
int x;
int y;
};
int main() {
Object* ptr = new Object; // Dynamic memory allocation
ptr->x = 1;
ptr->y = 2;
std::cout << "Object: x = " << ptr->x << ", y = " << ptr->y << std::endl;
delete ptr; // Release allocated memory
return 0;
}