学了C再看C++(1)函数



/*
内联函数:
会将函数调用直接替换为代码,增加空间,节省时间。
使用时声明或定义前加关键字inline
跟宏类似,个人感觉不如宏。

引用:
相当于const指针,跳转函数时不需要解引用,
一个变量的地址不会改变,除非让它消失,所以引用无法改变,跟随变量。
int &a;  int const *b;a相当于*b
相比*b,a的好处是少写了个*其它都和int const * 一样。(&a)++是无效的

默认参数:
不传参时候用默认参数。
声明定义只有一个能带默认参数

重载:同一个函数名不同功能的意思
重载函数名、作用域、相同。参数的类型个数应该不同

模板:对于一个函数,即使传入不同类型的参数也能实现相似功能
声明如下:
 template<typename T>
void swap(T a,T b){}//class T(C98)
*/

#include <iostream>
#include <fstream>

#define DAY2
using namespace std;
void day2();


#ifdef DAY2
void day2()
{

    string s1 = "内联函数:\n会将函数调用直接替换为代码,增加空间,节省时间。\n使用时声明或定义前加关键字inline\n跟宏类似,个人感觉不如宏。\n";
    string s2 = "\n引用:\n相当于const指针,跳转函数时不需要解引用,\n一个变量的地址不会改变,除非让它消失,所以引用无法改变,跟随变量。\nint &a;  int const *b;a相当于*b\n相比*b,a的好处是少写了个*其它都和int const * 一样。(&a)++是无效的\n";
    string s3 = "\n默认参数:\n不传参时候用默认参数。\n声明定义只有一个能带默认参数\n";
    string s4 = "\n重载:同一个函数名不同功能的意思\n重载函数名、作用域、相同。参数的类型个数应该不同\n";
    string s5 = "\n模板:对于一个函数,即使传入不同类型的参数也能实现相似功能\n声明如下:\n template<typename T>\nvoid swap(T a,T b){}//class T(C98)\n";
    std::cout << (s1 + s2 + s3 + s4 + s5)<<endl;
}
//小写变大写
void day2_struper(string & s)
{
    for (unsigned int i = 0;i< s.length();i++)
    {
        s[i]=toupper(s[i]);
    }
    cout << s << endl;
}

//找最大值
template<typename T>
T* max5(T *a ,int  n)
{
    T* p = new(T);
    T temp;
    for (; n >0; n--)
    {
        if (a[n] > a[n - 1])temp = a[n];
        else temp = a[n - 1];
    }
    *p = temp;
    return p;
}
//重载魔板,称为具体实现
template<>
char* max5<char>(char* a, int n)
{
    int location=0;
    int n1=0, n2=0;
    for (int i=0; i<n; i++)
    {
        if (a[i] == ' ' || a[i] == '\n')
        {
            if (n1 > n2)
            {
                n2 = n1; location = i;
            }
            n1 = 0;       
        }
        else
        {  
           n1++;     
        }
    }
    cout << location-n2 << endl;
    //n = 1;
    return (a + location-n2);
}
//测试
void max_test() 
{
    char c[60] = "like best all laplace newbi hello world";
    cout << "重载测试" << endl;
    int a[5] = { 10,90,60,80,70 };
    double b[5] = { 3.14,1.41,1.73,1.14514,1.7 };
    cout << *(max5(a, 5))<<endl;
    cout << *(max5(b, 5)) << endl;
    cout << (max5(c, 60)) << endl;
    //cout << len << endl;
}
#endif

int main()
{
#ifdef DAY1
    cout << "Hello World!\n" << endl;
    string s = "世界你好";
    int i = 0;
    string a = "!";
    //char a[100];
    double wt = 125.8;
    cout << s + a;
    string text;
    ofstream file;
    ifstream ifile;
    ifile.open("stduy.txt");
    file.open("stduy.txt");
    if (file.is_open())
    {
        file.precision(4);
        file << wt << endl;
        file << "this is a test program." << endl;
        cout << "input your data:";
        cin >> wt;
        file << "users:" << wt << endl;
        file.close();
    }
    while (ifile.good())
    {
        ifile >> s;
        cout << endl << "flie:" << endl;
        cout << s << endl;
        i += s.length();
        cout << "file length:" << i << endl;

    }
    ifile.close();
#endif   
#ifdef DAY2

    day2();
    string s = "upper";
    day2_struper(s);
    max_test();
#endif

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值