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 Quicksort
template <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@hotmail.com");user2.printInfo();system("pause");return 0;
}

实验总结:

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

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

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

评论:

https://www.cnblogs.com/nnn13579/p/10561474.html

https://www.cnblogs.com/xuexinyu/p/10585574.html

https://www.cnblogs.com/KOKODA/p/10566358.html

转载于:https://www.cnblogs.com/wjh1022/p/10589198.html

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

  1. 函数重载函数的引用算重载吗_了解C ++中的函数重载

    函数重载函数的引用算重载吗 介绍 (Introduction) Today in this tutorial, we are going to understand the concept of Fu ...

  2. java的函数重载函数_Java函数重载和重写

    版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创! 恰饭广告 函数也称为方法! 函数重载:在同一个类中存在多个函数,函数名称相同但参数列表不同.这就是函数的重载. 注意事项: ...

  3. 实验二——函数重载,快速排序,类对象

    函数重载: #include<iostream> using namespace std; struct complex{ double real; double imaginary; } ...

  4. C++笔记函数重载函数模板

    #include <iostream>using namespace std;class Student { private:int num;int score; public:void ...

  5. php连接数据库封装函数,PHP基于MySQLI函数封装的数据库连接工具类【定义与用法】...

    本文实例讲述了PHP基于MySQLI函数封装的数据库连接工具类.分享给大家供大家参考,具体如下: mysql.class.php: class mysql { private $mysqli; pri ...

  6. c++学习笔记内联函数,函数重载,默认参数

    c++学习笔记内联函数,函数重载,默认参数 1 inline内联函数 C++中的const常量可以替代宏常数定义,如: const int A = 3;  #define A 3 C++中是否有解决 ...

  7. 《C++初阶之路》函数重载

    一.本章重点 什么是函数重载? 函数重载的条件 为什么有函数重载? 为什么C不支持函数重载,C++确能支持函数重载? extern "C" 二.函数重载 2.1函数重载的概念 简单 ...

  8. C++学习笔记:(二)函数重载 常量与引用

    目录 3.函数重载 3.1 非成员函数重载 3.2 成员函数重载 3.3 函数的默认参数 3.4 内联函数 4.常量与引用 4.1 const的最初动机 4.2 const与指针 4.3 const与 ...

  9. 《C++ Primer 第五版》(第6.3~6.7节)——返回指向数组/函数的指针,函数重载,默认形参、inline函数和constexpr函数

    1.返回指向数组/函数的指针 顾名思义,就是函数返回值为指向数组/函数的指针. 数组的性质:不能被拷贝,函数也不能返回数组.但可以返回数组指针/引用,声明一个返回数组指针的函数,有四种方式,一种是直接 ...

最新文章

  1. 查车的行驶轨迹_怎么查车辆行驶轨迹?
  2. 老王学java之This()的用法
  3. 汇编入门学习笔记 (十二)—— int指令、port
  4. python内存管理机制错误_Python内存管理机制和垃圾回收机制的简单理解
  5. 动画函数优化,为任意元素添加任意多个属性
  6. verilog异步复位jk触发器_同步复位和异步复位常见问题总结
  7. Windows平台安装dlib方法汇总
  8. JS实现自动轮播图效果(自适应屏幕宽度+手机触屏滑动)
  9. centeros7网络服务无法启动_Center OS7网络设置
  10. 【MATLAB】图像分割
  11. 通信用特种光缆的选型
  12. 谈谈量化交易的一些“深坑”
  13. UML系列——时序图(顺序图)
  14. 23年教资面试开始啦个人报名流程
  15. iOS 真机测试错误解决An App ID with Identifier ...is not available
  16. Excel 中如何根据单元格内容删除行
  17. SpringBoot 注解原理,自动装配原理,图文并茂,万字长文!
  18. SpringBoot-自动配置
  19. 把三层交换机当成普通交换机来用
  20. 边缘计算 ai_什么是边缘AI计算?

热门文章

  1. linux中vi编辑器(转载)
  2. asp.net mvc源码分析-Action篇 Action的执行
  3. typo(ruby的开源blog)系统的安装
  4. 8月的最后一天,随意漫笔
  5. 商业智能BI和报表的区别?
  6. 报表session与应用session常识普及
  7. java3d翻转纪念相册_HTML5 3D旋转相册的实现示例
  8. 用python和pycharm能做什么_pycharm能干嘛
  9. pythonscrapy爬虫安装_零基础写python爬虫之爬虫框架Scrapy安装配置
  10. 工厂供电MATLAB仿真,工厂供电课程设计---基于MATLAB的电力电子系统仿真