/* * point.cpp * * Created on: Dec 1, 2013 * Author: gaojie */ #include <iostream> using namespace std; class Point3 { private : int x ; int y ; int z ; public: Point3(); ~Point3(); Point3(int x, int y, int z); void Print() const { cout << "x:" << x <<", y:"<<y <<", z:"<<z<<endl; } int getX() const { return x; } int getY() const { return y; } int getZ() const { return z; } }; Point3::Point3(){ x = 10, y = 20; z = 30; } //析構函數, 對象銷燬時調用 Point3::~Point3(){ x = 0; y =0; z = 0; cout << "destory" <<endl; } Point3::Point3(int x, int y, int z) { this->x = x; this->y = y; this->z = z; }; int main(void){ //建立local變量 Point3 p1(1, 3, 5); p1.Print(); //建立堆對象(malloc) Point3* p = new Point3(5, 15, 25); p->Print(); delete p; return 0; }