首页 > 其他分享 >函数重载

函数重载

时间:2023-04-21 17:56:07浏览次数:23  
标签:调用 函数 int print 重载 string

函数重载是指在同一作用域内,可以有一组具有相同函数名,不同参数列表的函数,这组函数被称为重载函数。重载函数通常用来命名一组功能相似的函数,这样做减少了函数名的数量,避免了名字空间的污染,对于程序的可读性有很大的好处。

#include<iostream>
using namespace std;

void print(int i)
{
        cout<<"print a integer :"<<i<<endl;
}

void print(string str)
{
        cout<<"print a string :"<<str<<endl;
}

int main()
{
        print(12);
        print("hello world!");
        return 0;
}

通过上面代码的实现,可以根据具体的print()的参数去调用print(int)还是print(string)。上面print(12)会去调用print(int),print("hello world")会去调用print(string)

标签:调用,函数,int,print,重载,string
From: https://www.cnblogs.com/jmhyyds/p/17341270.html

相关文章

  • js 时间格式化函数
    functiondateFormat(time,fmStr){constweekCN='一二三四五六日'constweekEN=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']letyear......
  • C++——string 成员函数 length(),size() 和 C strlen() 的区别
    //方法一:调用length()或size()stringstrTest="test";strTest.length();//4strTest.size();//4//方法二:转为C风格字符串,调用strlen()strlen(strTest.c_str());//4注意:当string中含有空字符’\0’,使用 strlen()获取stri......
  • 7、带输入的复数类-加-减-乘运算符重载
    如题,要求实现:1、复数类含两个参数的构造函数,一个为实部,一个为虚部。2、实现-加-减-乘运算符重载运算符重载功能,并实现返回值为复数类。3、Show出结果。例如输入78输出:(10+12i)(-4-4i)(-11+52i)#include<iostream>usingnamespacestd;classCmycomplex{private:......
  • 6、带输入的复数类加法运算符重载
    如题,要求实现:1、复数类含两个参数的构造函数,一个为实部,一个为虚部。2、实现加法运算符重载功能,并实现返回值为复数类。3、Show出结果。#include<iostream>usingnamespacestd;classCmycomplex{private:doublereal,imag;public:Cmycomplex(doubler=0,doubl......
  • 5、复数类加法运算符重载
    如题,要求实现:1、复数类含两个参数的构造函数,一个为实部,一个为虚部。2、实现加法运算符重载功能,并实现返回值为复数类。3、Show出结果。#include<iostream>usingnamespacestd;classCmycomplex{private:intreal,imag;public:Cmycomplex(intr=0,inti=0);//......
  • react 生命周期钩子函数
    1、挂载:construct、getDerivedStateFromProps、render、componentDidMounted2、更新:getDerivedStateFromProps、componentWillUpdate、render、getSnapshotBeforeUpdate、componentDidUpdated3、卸载:componentWillUnmounted4、请求放在componentDidMount里react生命周期图:h......
  • 3、复数类add成员函数
    如题,要求实现:1、复数类含两个参数的构造函数,一个为实部,一个为虚部2、实现Add()功能。3、Show出结果。#include<iostream>usingnamespacestd;classCmycomplex{private:intreal,imag;public:Cmycomplex(intr=0,inti=0);//构造函数设默认值,或者重载构造函......
  • python8:函数:函数的参数
    函数格式:defname():函数体 函数的参数:必选参数,默认参数[缺省参数](在调用的时候如果未赋值,就会用定义函数是给的默认值)、可选参数、关键字参数参数:其实就是函数为了实现某项特定的功能,进而为了得到所实现功能所需要的数据。默认参数【缺省参数】defsum1(a=20,b=30):在调......
  • 直接写和放在函数中不同的R语言用法
    索引数据框中的某一列df$A可以索引数据框df中列名为A的列的所有值。那么假如列名是一个R对象怎么做?df<-data.frame(A=1:5,B=(1:5)*2)df$A##[1]12345needed_column='A'#df$needed_column?Wrong#注意是双方括号df[[needed_column]]##[1]12345ggplo......
  • oracle function 函数
    oraclefunction的几个实例,只限入门,高手路过 --********thesimplestexampleoffunction--tip:canusesqltodirectlycallthefunctionwithinmoduleparametercreateorreplacefunctionget_customer_namereturnvarchar2isv_uservarchar2(100);begins......