zoukankan      html  css  js  c++  java
  • Output of C++ Program | Set 9

      Predict the output of following C++ programs.

    Question 1

     1 template <class S, class T> class Pair
     2 {
     3 private:
     4     S x;
     5     T y;
     6     /* ... */
     7 };
     8 
     9 template <class S> class Element
    10 {
    11 private:
    12     S x;
    13     /* ... */
    14 };
    15 
    16 int main ()
    17 {
    18     Pair <Element<int>, Element<char>> p;
    19     return 0;
    20 }

      Output:

      Compiler Error: '>>' should be '> >' within a nested template argument list
      When we use nested templates in our program, we must put a space between two closing angular brackets, otherwise it conflicts with operator >>.

      For example, following program compiles fine.

     1 template <class S, class T> class Pair
     2 {
     3 private:
     4     S x;
     5     T y;
     6     /* ... */
     7 };
     8 
     9 template <class S> class Element
    10 {
    11 private:
    12     S x;
    13     /* ... */
    14 };
    15 
    16 int main ()
    17 {
    18     Pair <Element<int>, Element<char> > p;   // note the space between '>' and '>'
    19     return 0;
    20 }


    Question 2

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test 
     5 {
     6 private:
     7     static int count;
     8 public:
     9     static Test& fun();
    10 };
    11 
    12 int Test::count = 0;
    13 
    14 Test& Test::fun() 
    15 {
    16     Test::count++;
    17     cout << Test::count << " ";
    18     return *this;
    19 }
    20 
    21 int main()  
    22 {
    23     Test t;
    24     t.fun().fun().fun().fun();
    25     return 0;
    26 }

      Output:

      Compiler Error: 'this' is unavailable for static member functions
      this pointer is not available to static member methods in C++, as static methods can be called using class name also. Similarly in Java, static member methods cannot access this and super (super is for base or parent class).
      If we make fun() non-static in the above program, then the program works fine.

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Test 
     5 {
     6 private:
     7     static int count;
     8 public:
     9     Test& fun();
    10 };
    11 
    12 int Test::count = 0;
    13 
    14 Test& Test::fun() 
    15 {
    16     Test::count++;
    17     cout << Test::count << " ";
    18     return *this;
    19 }
    20 
    21 int main()  
    22 {
    23     Test t;
    24     t.fun().fun().fun().fun();
    25     return 0;
    26 }

      Output:
      1 2 3 4

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-27  15:48:38

  • 相关阅读:
    求最大公约数伪代码
    XOR加密
    20201225张晓平第五周学习
    pep9课下作业 张晓平
    20201225 张晓平《信息安全专业导论》第四周学习总结
    寻找黑客偶像 20201225张晓平
    《信息安全专业导论》第九周学习总结
    《信息安全专业导论》第八周学习总结
    《信息安全专业导论》第七周学习总结
    《信息安全专业导论》第6周学习总结
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445882.html
Copyright © 2011-2022 走看看