C++ 入門指南 V2.00 - 單元 10 範例及練習程式碼



class_demo.cpp


#include <iostream>
 
using namespace std;

// 宣告類別 
class Demo {
   // 宣告 public 成員
   public:
      int a;
      int b;
      int DoSomething();
};

// 實作 Demo 的 DoSomething() 成員函數
int Demo::DoSomething() {
   return a + b;
}

// 程式執行的 main() 
int main(void) {
   // 宣告並建立 Demo 型態的物件 t
   Demo t;
   t.a = 11; // 直接設定成員變數值
   t.b = 22;
 
   cout << endl;
   // 呼叫並印出 do_something() 的回傳值
   cout << t.DoSomething() << endl;
   cout << endl;
    
   return 0;
}
 
/* 檔名: class_demo.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1001.cpp


#include <iostream>
 
using namespace std;

class IntegerDemo {
public:
   int value;
   void Add(int);
};

void IntegerDemo::Add(int p) {
   value += p;
}
 
int main(void) {
   cout << endl;
   IntegerDemo a;
   a.value = 25;
   cout << "a = " << a.value << endl;
   a.Add(24);
   cout << "a = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1001.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */


exercise1002.cpp


#include <iostream>
 
using namespace std;

class IntegerDemo {
public:
   int value;
   void add(int);
};

void IntegerDemo::add(int p) {
   value += p;
}
 
int main(void) {
   int input1;
   int input2;
   
   cin >> input1;
   cin >> input2;
    
   cout << endl;
   IntegerDemo a;
   a.value = input1;
   cout << "a = " << a.value << endl;
   a.Add(input2);
   cout << "a = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1002.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1003.cpp


#include <iostream>
 
using namespace std;

class IntegerDemo {
public:
   int value;
   void Add(int);
   void Subtract(int);
};

void IntegerDemo::Add(int p) {
   value += p;
}

void IntegerDemo::Subtract(int p) {
   value -= p;
}
 
int main(void) {
   int input1;
   int input2;
   int input3;
    
   cin >> input1;
   cin >> input2;
   cin >> input3;
    
   cout << endl;
   IntegerDemo a;
   a.value = input1;
   cout << "a = " << a.value << endl;
   a.Add(input2);
   cout << "a = " << a.value << endl;
   a.Subtract(input3);
   cout << "a = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1003.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */


exercise1004.cpp


#include <iostream>
 
using namespace std;

class IntegerDemo {
public:
   int value;
   void Add(int);
   void Subtract(int);
   void Multiply(int);
};

void IntegerDemo::Add(int p) {
   value += p;
}

void IntegerDemo::Subtract(int p) {
   value -= p;
}

void IntegerDemo::Multiply(int p) {
   value *= p;
}
 
int main(void) {
   int input1;
   int input2;
   int input3;
   int input4;
    
   cin >> input1;
   cin >> input2;
   cin >> input3;
   cin >> input4;
    
   cout << endl;
   IntegerDemo a;
   a.value = input1;
   cout << "a = " << a.value << endl;
   a.Add(input2);
   cout << "a = " << a.value << endl;
   a.Subtract(input3);
   cout << "a = " << a.value << endl;
   a.Multiply(input4);
   cout << "a = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1004.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1005.cpp


#include <iostream>
 
using namespace std;

class IntegerDemo {
public:
   int value;
   void Add(int);
   void Subtract(int);
   void Multiply(int);
   void Divide(int);
};

void IntegerDemo::Add(int p) {
   value += p;
}

void IntegerDemo::Subtract(int p) {
   value -= p;
}

void IntegerDemo::Multiply(int p) {
   value *= p;
}

void IntegerDemo::Divide(int p) {
   value /= p;
}
 
int main(void) {
   int input1;
   int input2;
   int input3;
   int input4;
   int input5;
    
   cin >> input1;
   cin >> input2;
   cin >> input3;
   cin >> input4;
   cin >> input5;
    
   cout << endl;
   IntegerDemo a;
   a.value = input1;
   cout << "a = " << a.value << endl;
   a.Add(input2);
   cout << "a = " << a.value << endl;
   a.Subtract(input3);
   cout << "a = " << a.value << endl;
   a.Multiply(input4);
   cout << "a = " << a.value << endl;
   a.Divide(input5);
   cout << "a = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1005.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */

exercise1006.cpp


#include <iostream>
 
using namespace std;

class FactorialDemo {
public:
   int value;
   void Run(int);
};

void FactorialDemo::Run(int p) {
   int result = 1;
    
   for (int i = 1; i <= p; i++) {
      result *= i;
   }
   
   value = result;
}
 
int main(void) {
   cout << endl;
   FactorialDemo a;
   a.Run(10);
   cout << "10! = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1006.cpp
   作者: Kaiching Chang 
   時間: 2014-5 */


exercise1007.cpp


#include <iostream>
 
using namespace std;

class FibonacciDemo {
public:
   int value;
   void Run(int);
};

void FibonacciDemo::Run(int n) {
   int f0 = 0;
   int f1 = 1;
   int f2;
    
   if (n == 0) {
      value = f0;
   }
   else if (n == 1) {
      value = f1;
   }
   else {
      for (int i = 2; i <= n; i++) {
         f2 = f0 + f1;
         f0 = f1;
         f1 = f2;
      }
        
      value = f2;
   }
}
 
int main(void) {
   cout << endl;
   FibonacciDemo a;
   a.Run(10);
   cout << "f(10) = " << a.value << endl;
   cout << endl;
         
   return 0;
}
 
/* 檔名: exercise1007.java
   作者: Kaiching Chang 
   時間: 2014-5 */

the end

沒有留言: