本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

C++ 速查手冊 V1.00 - 單元 9 範例




u09.cpp


#include <iostream>

class Demo {
public:
   int a;
   int b;
   
   int do_something() {
      return a + b;
   }
};

int main() {
   Demo d;
   d.a = 11;
   d.b = 22;
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u09.cpp
   2014-02 */

u0901_1.cpp


#include <iostream>

class Demo {
public:
   int a;
   int b;

   Demo() {
      std::cout << "constructor.."
                << std::endl;

      a = 22;
      b = 33;
   }

   int do_something() {
      return a + b;
   }
};

int main() {
   Demo d;
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0901_1.cpp
   2014-02 */

u0901_2.cpp


#include <iostream>

class Demo2 {
public:
   int a;
   int b;

   Demo2() {
      a = 22;
      b = 33;
   }

   Demo2(int pa, int pb) {
      a = pa;
      b = pb;
   }

   int do_something() {
      return a + b;
   }
};

int main() {
   Demo2 d(33, 44);
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0901_2.cpp
   2014-02 */

u0902.cpp


#include <iostream>

class Demo {
public:
   int a;
   int b;

   Demo(int pa, int pb): a(pa), b(pb) {
      std::cout << "constructor.."
                << std::endl;
   }
    
   int do_something() {
      return a + b;
   }
};

int main() {
   Demo d(55, 44);
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0902.cpp
   2014-02 */

u0903_1.cpp


#include <iostream>

class Demo {
public:
   int a;

   Demo(int pa) {
      a = pa;
   }
    
   void do_something() {
      std::cout << a
                << std::endl;
   }
};

int main() {
   Demo d = 3;
   d.do_something();

   return 0;
}
 
/* Kaiching Chang 
   u0903_1.cpp
   2014-02 */

u0903_2.cpp


#include <iostream>

class Demo {
public:
   int a;

   explicit Demo(int pa) {
      a = pa;
   }
    
   void do_something() {
      std::cout << a
                << std::endl;
   }
};

int main() {
   Demo d = 3;
   d.do_something();

   return 0;
}
 
/* Kaiching Chang 
   u0903_2.cpp
   2014-02 */

u0904.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa, int pb) {
      a = pa;
      b = pb;
   }

   int do_something() {
      return a + b;
   }

private:
   int a;
   int b;
};

int main() {
   Demo d(12, 10);
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0904.cpp
   2014-02 */

u0904_1.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa, int pb) {
      a = pa;
      b = pb;
   }

   int do_something() {
      return a + b;
   }

private:
   int a;
   int b;
};

int main() {
   Demo d(12, 10);
   std::cout << d.a
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0904_1.cpp
   2014-02 */

u0905.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa, int pb) {
      a = pa;
      b = pb;
   }

   int do_something() {
      return a + b;
   }

protected:
   int a;
   int b;
};

int main() {
   Demo d(12, 10);
   std::cout << d.do_something()
             << std::endl;

    return 0;
}
 
/* Kaiching Chang 
   u0905.cpp
   2014-02 */

u0906.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa, int pb) {
      set_a(pa);
      set_b(pb);
   }

   int get_a() {
      return a;
   }
   
   int get_b() {
      return b;
   }
   
   void set_a(int pa) {
      a = pa;
   }
   
   void set_b(int pb) {
      b = pb;
   }

private:
   int a;
   int b;
};

int main() {
   Demo d(12, 10);
   std::cout << d.get_a() + d.get_b()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0906.cpp
   2014-02 */

u0907.cpp


#include <iostream>

class Demo {
public:
   Demo(int a, int b) {
      this->a = a;
      this->b = b;
   }
 
   int do_something() {
      return this->a + this->b;
   }

private:
   int a;
   int b;
};

int main() {
   Demo d(22, 60);
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0907.cpp
   2014-02 */

u0908.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa, int pb) {
      a = pa;
      b = pb;
   }

   friend int do_something(Demo& d) {
      return d.a + d.b;
   }

   friend class Demo2;
    
private:
   int a;
   int b;
};

class Demo2 {
public:
   int do_something2(Demo& d) {
      return d.a + d.b;
   }
};

int main() {
   Demo d(32, 22);
   std::cout << do_something(d)
             << std::endl;
   Demo2 d2;
   std::cout << d2.do_something2(d)
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0908.cpp
   2014-02 */

u0909.cpp


#include <iostream>

class Demo {
public:
   Demo() {
      std::cout << "constructor"
                << std::endl;
      count++;
   }

   static int get_count() {
      return count;
   }

private:
   static int count;
};

int Demo::count = 0;

int main() {
   Demo d1;
   std::cout << Demo::get_count()
             << std::endl;
   Demo d2;
   std::cout << d2.get_count()
             << std::endl;
   Demo d3;
   std::cout << d3.get_count()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0909.cpp
   2014-02 */

u0910.cpp


#include <iostream>

class Demo {
public:
   static int getDay() {
      return days;
   }

private:
   static const int days = 30;
};

int main() {
   std::cout << Demo::getDay()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0910.cpp
   2014-02 */

u0911.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa, int pb) {
      a = pa;
      b = pb;
   }

   int get_sum() const {
      return a + b;
   }

private:
   int a;
   int b;
};

int main() {
   Demo d(24, 25);
   std::cout << d.get_sum()
             << std::endl;

    return 0;
}
 
/* Kaiching Chang 
   u0911.cpp
   2014-02 */

u0912.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa) {
      a = pa;
   }

   int do_something() const {
      ++a;

      return a;
   }

private:
   mutable int a;
};

int main() {
   Demo d(24);
   std::cout << d.do_something()
             << std::endl;
   std::cout << d.do_something()
             << std::endl;

    return 0;
}
 
/* Kaiching Chang 
   u0912.cpp
   2014-02 */

u0913.cpp


#include <iostream>

class Demo {
public:
   class Demo2 {
   public:
      Demo2(int a) {
         d2a = a;
      }

      int d2a;
   };

   Demo2 *d2;

   void do_something() {
      d2 = new Demo2(34);
      std::cout << d2->d2a
                << std::endl;
   }
};

int main() {
   Demo d;
   d.do_something();

   return 0;
}
 
/* Kaiching Chang 
   u0913.cpp
   2014-02 */

u0914.cpp


#include <iostream>
#include <string>

class Demo {
public:
   Demo(std::string s) {
      std::cout << "constructor"
                << std::endl;
      a_ptr = new std::string;
      *a_ptr = s;
   }

   Demo(const Demo& obj) {
      std::cout << "copy constructor"
                << std::endl;
      a_ptr = new std::string;
      *a_ptr = *obj.a_ptr;
   }
    
   void set_a(std::string s) {
      *a_ptr = s;
   }
    
   void do_something() {
      std::cout << *a_ptr
                << std::endl;
   }

private:
   std::string *a_ptr;
};

int main() {
   Demo d1("There is no spoon.");
   d1.do_something();
   Demo d2 = d1;
   d2.do_something();

   d1.set_a("What's truth?");
   d1.do_something();
   d2.do_something();

   return 0;
}
 
/* Kaiching Chang 
   u0914.cpp
   2014-02 */

u0915.cpp


#include <iostream>

class Demo {
public:
   Demo(int pa) {
      std::cout << "constructor" << std::endl;
      a_ptr = new int;
      *a_ptr = pa;
   }

   ~Demo() {
      std::cout << "destructor" << std::endl;
      delete a_ptr;
   }

   void do_something() {
      std::cout << *a_ptr << std::endl;
   }

private:
   int *a_ptr;
};

int main(void) {
   Demo d(2048);
   d.do_something();

   return 0;
}
 
/* Kaiching Chang 
   u0915.cpp
   2014-02 */

u0916.cpp


#include <iostream>

class Demo {
public:
   Demo() {
      a = 1;
      b = 1;
   }

   Demo(int pa, int pb) {
      a = pa;
      b = pb;
   }

   Demo operator+(const Demo& p) {
      Demo demo;
      demo.a = this->a + p.a;
      demo.b = this->b + p.b;

      return demo;
   }             

   void do_something() {
      std::cout << a + b
                << std::endl;
   }

private:
   int a;
   int b;
    
};

int main(void) {
   Demo d1(2, 6);
   d1.do_something();
   Demo d2(10, 24);
   d2.do_something();
   Demo d3;
   d3 = d1 + d2;
   d3.do_something();

   return 0;
}
 
/* Kaiching Chang 
   u0916.cpp
   2014-02 */

u0917.cpp


#include <iostream>

class Demo {
public:
   Demo() {
      a = 10;
      b = 33;
   }

protected:
   int a;
   int b;
};

class Demo2: Demo {
public:
   int do_something() {
      return a + b;
   }   
};

int main() {
   Demo2 d;
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0917.cpp
   2014-02 */

u0918.cpp


#include <iostream>

class Demo {
public:
   Demo() {
      a = 702;
      b = 631;
   }

protected:
   int a;
   int b;
};

class Demo2 {
public:
   Demo2() {
      c = 548;
      d = 255;
   }

protected:
   int c;
   int d;
};


class Demo3: Demo, Demo2 {
public:
   int do_something() {
      return a + b + c + d;
   }   
};

int main() {
    Demo3 d;
    std::cout << d.do_something()
              << std::endl;

    return 0;
}
 
/* Kaiching Chang 
   u0918.cpp
   2014-02 */

u0919.cpp


#include <iostream>

class Demo {
public:
   virtual int do_something() {
      return 1;
   }
};

class Demo2: public Demo {
public:
   virtual int do_something() {
      return 2;
   }
};

int main() {
   Demo* d_ptr;
   Demo2 d2;
   d_ptr = &d2;

   std::cout << d_ptr->do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0919.cpp
   2014-02 */

u0920.cpp


#include <iostream>

class Demo {
public:
   Demo() {
      a = 1;
      b = 1;
   }

   int do_something() {
      return a + b;
   }
    
protected:
   int a;
   int b;
};

class Demo2: virtual public Demo {};
class Demo3: virtual public Demo {};
class Demo4: public Demo2,
             public Demo3 {};

int main() {
   Demo4 d;
   std::cout << d.do_something()
             << std::endl;

   return 0;
}
 
/* Kaiching Chang 
   u0920.cpp
   2014-02 */

the end

沒有留言: