100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > C++ 实验2:函数重载 函数模板 简单类的定义和实现

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

时间:2023-01-18 23:55:24

相关推荐

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

1.函数重载编程

编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型

数据,调用测试。

#include <iostream>using namespace std;struct complex {double real;double imaginary;};int add(int x2, int y2){return x2 + y2;}double add(double a1, double b1){return a1 + b1;}complex add(complex a, complex b){complex c;c.imaginary = a.imaginary + b.imaginary;c.real = a.real + b.real;return c;}int add(int x2, int y2);double add(double a1, double b1);complex add(complex a, complex b);int main(){int x = 4, y = 3, s1;double m = 4.2, n =1.5, s2;complex complex1, complex2, complex3;complex1.real = 2, complex1.imaginary = 3;complex2.real = 6, complex2.imaginary = 4;s1 = add(x, y);s2 = add(m, n);complex3 = add(complex1, complex2);cout << s1 << endl;cout << s2 << endl;cout << complex3.real << "+" << complex3.imaginary << "i" << endl;system("pause");return 0;}

2.函数模板编程

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

#ifndef Quicksort#define Quicksorttemplate <class T>void quicksort(T s[], int low, int high) {int a, b, c = 0;T f, ex;a = low; b = high - 1; f = s[(low + high) / 2];if (a < b) {while (a < b) {while (a < b&&f < s[b])b--;while (a < b&&f > s[a])a++;if (a >= b) c = b;else { ex = s[a]; s[a] = s[b]; s[b] = ex; }}quicksort(s, low, c);quicksort(s, c + 1, high);}}#endif

#include <iostream>#include <iomanip>#include "quicksort.h"using namespace std;int main(){int i;int a[5] = { 7,5,3,9,2 };double b[5] = { 5.5,8.5,9.9,2.5,3.6 };quicksort(a, 0, 5);quicksort(b, 0, 5);for (i=0;i<5;i++)cout << setw(5) << a[i];cout << endl;for (i=0;i<5;i++)cout << setw(5) << b[i];cout << endl;system("pause");return 0;}

3.类的定义、实现和使用编程

设计并实现一个用户类User,并在主函数中使用和测试这个类。

每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。

支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。

支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。

支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。

如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。

在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信

息)

#include<iostream>#include<string>using namespace std;class User {public:User(string yourname, string yourpasswd, string youremail);User() {name = "";password = "111111";email = "";}void setInfo(string yourname = "", string yourpasswd = "111111", string youremail = "");void changePasswd();void printInfo();private:string name;string password;string email;};void User::setInfo(string yourname, string yourpasswd, string youremail) {if (name == " ") cin >> yourname;name = yourname; if (password == " ") cin >> yourpasswd;password = yourpasswd;if (email == " ") cin >> youremail;email = youremail;}void User::changePasswd() {string yourpassword;int i = 1;cout << "please input password:";cin >> yourpassword;while (password != yourpassword && i < 3){cout << "wrong,please input it again:";cin >> yourpassword;i++;}if (password != yourpassword && i == 3)cout << "please try later" << endl;if (password == yourpassword){cout << "please input your password:";cin >> yourpassword;}}void User::printInfo() {cout << "Name: " << name << endl;cout << "Password: " << "******" << endl;cout << "Email: " << email << endl;}

#include<iostream>#include<iomanip>#include"user.h"int main() {cout << "testing 1......" << endl;User user1;user1.setInfo("Leonard");user1.printInfo();user1.changePasswd();user1.printInfo();cout << endl << "testing 2......" << endl << endl;User user2;user2.setInfo("Jonny", "92197", "xyz@");user2.printInfo();system("pause");return 0;}

实验总结:

1.对于快速排序不是很理解,所以不知道怎么写,去问了同学和看了她的程序还是不太理解,所以最后基本上参考了同学的程序;

2.函数重载和类的定义基本上用了老师给的程序框架,但在编程的过程中还是很费劲;

3.以上问题说明我还没有掌握这些知识点,还有以上程序出现的不足之处,还请各位多多指教。

评论:

/nnn13579/p/10561474.html

/xuexinyu/p/10585574.html

/KOKODA/p/10566358.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。