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




u11.cpp


#include <iostream>

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

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

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

u1101.cpp


#include <iostream>

namespace demo {
   namespace demo2 {
      class Demo {
      public:
         int a;
         int b;

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

int main() {
   demo::demo2::Demo d;
   d.a = 33;
   d.b = 44;
   std::cout << d.do_something()
             << std::endl;

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

u1102.cpp


#include <iostream>

namespace {
   class Demo {
   public:
      int a;
      int b;

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

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

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

u1103.cpp


#include <iostream>

inline namespace {
   class Demo {
   public:
      int a;
      int b;

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

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

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

u1104.cpp


#include <iostream>

namespace demo {
   class Demo {
   public:
      int a;
      int b;

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

using namespace std;
using namespace demo;

int main() {
   Demo d;
   d.a = 77;
   d.b = 88;
   cout << d.do_something()
        << endl;

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

the end

沒有留言: