重载函数

#include <iostream>
using namespace std;
struct complex
{
    double  real;
    double imaginary;

};
int add(int x1, int y1)
{
    return x1 + y1;
}
double add(double x2, double y2)
{
    return x2 + y2;
}
complex add(complex a1, complex b1)
{
    complex c1;
    c1.imaginary = a1.imaginary + b1.imaginary;
    c1.real = a1.real + b1.real;
    return c1;
}
int main()
{
    int x = 3, y = 5, s1;
    double m = 2.1, n = 4.5, s2;
    complex complex1, complex2, complex3;
    complex1.real = 4, complex1.imaginary = 2;
    complex2.real = 2, complex2.imaginary = 3;
    s1 = add(x,y);
    s2 = add(m,n);
    complex3.real = add(complex1.real, complex2.real);
    complex3.imaginary = add(complex1.imaginary, complex2.imaginary);
    cout << s1 << endl;
    cout << s2 << endl;
    cout << complex3.real << "+" << complex3.imaginary << "i" << endl;
    return 0;

}
View Code

快速排序函数模板

#include <iostream>
#include <iomanip>
#include "quicksort.h"
using namespace std;
int main()
{
    int i;
    int x[5] = { 6,3,8,1,4 };
    double y[5] = { 2.3,6.6,4.9,9.8,1.5 };
    quicksort(x, 0, 5);
    quicksort(y, 0, 5);
    for (i = 0;i < 5;i++)
        cout << setw(5) << x[i];
    cout << endl;
    for (i = 0;i < 5;i++)
        cout << setw(5) << y[i];
    cout << endl;
    system("pause");
    return 0;
}
main
#ifndef Quicksort
#define Quicksort
template <class T>
void quicksort(T s[], int low, int high) {
    int x,y,z = 0;
    T f, t;
    x = low; y = high - 1; f = s[(low + high) / 2];
    if (x < y) {
        while (x < y) {
            while (x < y&&f < s[y])
                y--;
            while (x < y&&f > s[x])
                x++;
            if (x >= y) z = y;
            else { t = s[x]; s[x] = s[y]; s[y] = t; }
        }
        quicksort(s, low, z);
        quicksort(s, z + 1, high);
    }
}
#endif
head

类的定义,实现和使用编程

#include <iostream>
#include <string>
using namespace std;
class User
{public:
    void setInfo(string name0, string passwd = "111111", string email0 ="");
    void changepasswd();
    void printInfo();
private:
    string name;
    string passwd;
    string email;

};
void User::setInfo(string name0, string password0, string email0) {
    name = name0;
    passwd = password0;
    email = email0;
}
void User::changepasswd() {
    string oldpasswd;
    int i = 1;
    cout << "please input your old password:";cin >> oldpasswd;
    while (oldpasswd != passwd && i < 3)
    {
        cout << "wrong,please input it again:";cin >> oldpasswd;
        i++;
    }
    if (oldpasswd != passwd && i == 3)
        cout << "please try later" << endl;
    if (oldpasswd == passwd)
    {
        cout << "please input your new password:";cin >> passwd;
    }
}
void User::printInfo() {
    cout << "name:  " << name << endl;
    cout << "password:****** " << endl;;
    cout << "email:  " << email << endl;
    cout << endl;
}
int main()
{cout << "testing 1......" << endl;
User user1;
user1.setInfo("Leonard");
user1.printInfo();
user1.changepasswd();
user1.printInfo();
cout << "testing2......" << endl;
User user2;
user2.setInfo("Jonny", "92197", "xyz@hotmail.com");
user2.printInfo();
return 0;
}
View Code

总结:快速排序模板不会,只记得以前写的快速排序,所以参考了同学的

           如果没有老师给的框架可能不知道从哪开始写

 posted on 2019-03-24 20:52  TOKISOKI  阅读(137)  评论(2编辑  收藏  举报