Maplewing

9/2 C++: Square (private & constructor)

Sep 1st, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Square{
  5.     private:
  6.         // 資料(變數部分)
  7.         int len;
  8.        
  9.     public:
  10.         // 操作(函式部分)
  11.         int area(){
  12.             return len * len;
  13.         }
  14.             // 建構式
  15.             Square(){
  16.                 len = 0;
  17.             }
  18.        
  19.             // set函式
  20.             void setLen(int a){
  21.                 len = a;
  22.             }
  23.            
  24.             // get函式
  25.             int getLen(){
  26.                 return len;
  27.             }
  28. };
  29.  
  30.  
  31. int main(){
  32.     Square s1, s2;
  33.     s2.setLen(20);
  34.    
  35.     cout << s1.area() << endl;
  36.     cout << s2.area() << endl;
  37.  
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment