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

  • 相关阅读:
    编译安装zabbix3.2
    编译安装PHP的参数 --with-mysql --with-mysqli --with-apxs2默认路径
    MySql中指定符号分割并分行展示
    Linux中vim编辑器常用命令
    Ubuntu中安装配置 JDK与apache
    Ubuntu中python链接本地数据库
    oracle函数笔记(1)
    Oracle的五种约束
    斐波那契数列
    python计算圆面积
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445882.html
Copyright © 2011-2022 走看看