重载函数

#include<iostream>
using namespace std;
struct Complex
{
    double real;
    double imaginary;
};
int add(int ,int );
double add(double ,double );
Complex add(Complex ,Complex );
int main()
{
        int a,b;
        double c,d;
        Complex s1,s2;
        cin>>a>>b;
        cout<<add(a,b)<<endl;
        cin>>c>>d;
        cout<<add(c,d)<<endl;
        cin>>s1.real>>s1.imaginary>>s2.real>>s2.imaginary;
        cout<<add(s1,s2).real<<"+"<<add(s1,s2).imaginary<<"i"<<endl;
        return 0;
        
}
int add(int x,int y)
{
    return x+y;
}
double add(double x,double y)
{
    return x+y;
}
Complex add(Complex x,Complex y)
{
    Complex s3;
    s3.real=x.real+y.real;
    s3.imaginary=(x.imaginary+y.imaginary);
    return s3;
}
重载函数代码

#include<iostream>
using namespace std;
template<class T>
void quicksort(T a[], int l,int r)
{
    int t=a[l], k , i = l, j = r - 1;
    if(i>j)
        return ;
    while(i<j)
    {
        while(a[j]>=t&&i<j)
             j--;
        while(a[i]<=t&&i<j)
             i++;
        if(i<j)
        {
            k = a[i];
            a[i] = a[j];
            a[j] = k;
        }
    }
    a[l] = a[i];
    a[i] = t;
    quicksort(a, l, j );
    quicksort(a, i + 1, r);
}
int main()
{
    int a[10]={3,4,6,7,8,9,10,2,5,1},n=0,m=10;
     quicksort(a, n, m);
    for (n = 0; n < 10; n++)
        cout << a[n] << " ";
    return 0;
}
快速排序

数据是1-10打乱的排序。

#include<iostream>
#include<string>
using namespace std;
class User
{   
    public:
         void setinfo(string name1, string passwd1 = "111111", string email1 = " ");
         void printfinfo();
         void changePasswd();

    private:
        string name;
        string passwd;
        string email;
};
void User::setinfo(string name1, string passwd1,string email1)
{
    name=name1;
    passwd = passwd1;
    email = email1;
}
void User::printfinfo()
{
          cout << "name: " << name << "\n"
               << "passwd: ******"<<"\n"
               << "email: " << email << endl;
}
void User::changePasswd()
{
           string pswd;
           int count = 0;
           while (count < 3)
           {
               cout << "Enter old password :" << endl;
               cin >> pswd;
               if (pswd==passwd)
               {
                   cout << "New password" << endl;
                   cin >> passwd;
                   break;
               }
               else
               {
                   count++;
                   continue;
               }
           }
           if (count == 3)
               {
                   cout << "Please try it later" << endl;
               }
    }

int main()
{
    cout << "testing 1……" << endl;
    User user1;
    user1.setinfo("Leonard");
    user1.printfinfo();
    user1.changePasswd();
    user1.printfinfo();

    cout << endl
         << "testing 2 ……" << endl
         << endl;
    User user2;
    user2.setinfo("Jonny", "92197", "xyz@hotmail.com");
    user2.printfinfo();

    return 0;
}
类的尝试

上图是输入密码错误的情形

————————————————————

下图是输入密码正确的情形

 

实验总结

1、重载函数很方便,相比C可以少写很多的代码,极大的增加了效率。

2、快速排序的思路参考了一片文章,后续的修改也请同学帮忙看了一下,因为自己写的老是其中一个数排序出问题。另一问题就是,如果我把quicksort这个函数用另一个头文件写,再包含进主程序,它会提示quicksort为 未定义的函数,用了两个编译器,都提示的同样的问题。下来我再看看。

3、类的尝试也用了不少的时间,不熟悉是一回事,有些东西理解错了,在定义内函数的时候老是出问题,在例题和编译器的帮助下,完成了这个程序,看了几遍程序,还是大致明白怎么用了,可能不是很会变通,比较死板。

实验一互评的博客:

https://www.cnblogs.com/Yyaoyyy/p/10547742.html

https://www.cnblogs.com/jzgjzg/p/10555540.html

https://www.cnblogs.com/sq102217/p/10527710.html