以下面這個範例來說
如果y是大於0的話,就把y丟給this->y,如果不是大於0就把1丟給this->y
this->y=(y>0)?y:1;
#include <iostream> #include <cstdlib> using namespace std; //物件導向 //這邊一開始宣告Room類別 class Room{ //分別有 public, protect, private public: Room(){ //建構式: 特殊函示成員,在類別物件建立時執行, //白話講,就是給他預設值啦~ cout<<"Constructor called!!"< x=x; this->y=y; this->z=z; set(x, y, z); } int RoomSize(){ return x*y*z; } void print(){ cout<<"x:"< x=(x>0)?x:1; this->y=(y>0)?y:1; this->z=(z>0)?z:1; } private: int x, y, z; }; int main(){ Room room1(3, 4, 5), room2(6, 7, 8); Room* ptrRoom; ptrRoom = (room1.RoomSize()> room2.RoomSize())?&room1:&room2; ptrRoom->print(); system("pause"); return 0; }
輸出結果:
沒有留言:
張貼留言