zoukankan      html  css  js  c++  java
  • 函数的缺省参数和函数初始化示例以及布尔型参数的使用示例

    代码示例

     1 #include <iostream>
     2 using namespace std;
     3 class A
     4 {
     5 public:
     6     void set(int = 30, int = 5);//声明函数时,初始化参数
     7     void count(bool = false);//声明函数时,初始化参数
     8 private:
     9     int w;
    10     int h;
    11 };
    12 void A::set(int width, int height)
    13 {
    14     w = width;
    15     h = height;
    16 }
    17 void A::count(bool val)//类外定义函数 ps:布尔型参数的使用示例
    18 {
    19     if (val == true)
    20         cout << "当val的值为真时:" << w*h << endl;
    21     else
    22         cout << "当val的值为假时:" << w*h / 2 << endl;
    23 }
    24 int main()
    25 {
    26     A a;
    27     a.set();
    28     a.count();//使用初始化的函数
    29     a.count(true);//使用设定的参数
    30     return 0;
    31 }

    示例结果

  • 相关阅读:
    hdu1915
    2014年9月28日 18:35:01
    洛谷—— P1122 最大子树和
    洛谷——P1103 书本整理
    洛谷—— P2049 魔术棋子
    UVA_1575
    洛谷—— P2424 约数和
    中文乱码问题
    JSP标签
    include指令
  • 原文地址:https://www.cnblogs.com/table/p/4695909.html
Copyright © 2011-2022 走看看