实验2:函数重载、函数模板、简单类的定义和实现

1.函数重载编程练习 编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。

#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 a1=3,b1=4;
    double a2=3.0,b2=3.0;
    struct complex num1={1,2},num2={2,3};
    cout<<add(a1,b1)<<endl;
    cout<<add(a2,b2)<<endl;
    cout<<add(num1,num2).real<<"+"<<add(num1,num2).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)
{
    struct complex m;
    m.real=x.real+y.real;
    m.imaginary=x.imaginary+y.imaginary;
    return m;
}

2.函数模板编程练习 编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。

头文件

quicksort.h

#ifndef QUICKSORT_H
#define QUICKSORT_H
template<class T>
int partition(T a[], int low, int high) {
    T x = a[high];
    int i = low;
    for (int j = low;j < high;j++)
    {
        if (a[j] < x)
        {
            T temp;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
        }
    }
    a[high] = a[i];
    a[i] = x;
    return i;
}template<class T>
void qsort(T a[], int low, int high)
{
    if (low < high)
    {
        int q = partition(a, low, high);
        qsort(a, low, q - 1);
        qsort(a, q + 1, high);
    }
}
template<class T>
void quicksort(T a[], int num) {
    qsort(a, 0, num - 1);
}


#endif

output.h

#ifndef output_h
#define output_h
#include<iostream>
using namespace std;
template<class T>
void output(T a[], int n) {
    int i;
    for (i = 0;i < n; i++)
        cout << a[i] << " ";
}
#endif

主函数

#include<iostream>
#include "quicksort.h"
#include "output.h"
using namespace std;
int main() {
    int a[5] = { 3,4,8,2,5 };
    double b[7] = { 3.1,2.7,6.8,4.3,9.5,8.5,4.4 };

    quicksort(a, 5);
    output(a, 5);
    cout << endl;
    quicksort(b, 7);
    output(b, 7);

    return 0;
}

3.类的定义、实现和使用编程练习 设计并实现一个用户类User,并在主函数中使用和测试这个类。

具体要求如下: 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信息)

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class User {
public: 
    void setInfo(string namei, string passwdi = "111111", string emaili = "");
    void printInfo();
    void changePasswd();
private:
    string name;
    string email;
    string passwd;
};
void User::setInfo(string namei, string passwdi, string emaili) {
    name = namei;
    passwd = passwdi;
    email = emaili;
}
void User::printInfo() {
    cout << "name:" << name << endl;
    int i = passwd.length(); 
    cout << "passwd:";
    for (int m = 0;m < i;m++)
        cout << "*";
    cout << endl;
    cout << "email:" << email << endl;
}
void User::changePasswd() {
    string rpass;
    cin >> rpass;
    while (rpass != passwd) //测试密码正确性。
    {
        cout << "password is wrong" << endl;
        cin >> rpass;
    }
    if (rpass == passwd) {
        string newpasswd;
        cin >> newpasswd;
        passwd = newpasswd;
    }
}
int main() {
    User usertest;
    usertest.setInfo("ccc");//设置初始姓名。
    usertest.printInfo();
    usertest.changePasswd();//更改密码。
    usertest.printInfo();
    system("pause");
    return 0;
}

posted on 2019-03-24 15:00  褚先生  阅读(191)  评论(6编辑  收藏  举报

导航