Advertisement
Maplewing

5/28 C++: class Sqaure

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