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 = , y = , s1;
double m = 4.2, n =1.5, s2;
complex complex1, complex2, complex3;
complex1.real = , complex1.imaginary = ;
complex2.real = , complex2.imaginary = ;
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 ;
}
2.函数模板编程
编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。
#ifndef Quicksort
#define Quicksort
template <class T>
void quicksort(T s[], int low, int high) {
int a, b, c = ;
T f, ex;
a = low; b = high - ; f = s[(low + high) / ];
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 + , high);
}
}
#endif
#include <iostream>
#include <iomanip>
#include "quicksort.h"
using namespace std;
int main()
{
int i;
int a[] = { ,,,, };
double b[] = { 5.5,8.5,9.9,2.5,3.6 };
quicksort(a, , );
quicksort(b, , );
for (i=;i<;i++)
cout << setw() << a[i];
cout << endl;
for (i=;i<;i++)
cout << setw() << b[i];
cout << endl;
system("pause");
return ;
}
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 = "";
email = "";
}
void setInfo(string yourname = "", string yourpasswd = "", 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 = ;
cout << "please input password:";cin >> yourpassword;
while (password != yourpassword && i < )
{
cout << "wrong,please input it again:";cin >> yourpassword;
i++;
}
if (password != yourpassword && i == )
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", "", "xyz@hotmail.com");
user2.printInfo();
system("pause");
return ;
}
实验总结:
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
C++ 实验2:函数重载、函数模板、简单类的定义和实现的更多相关文章
- C++ 类的多态二(函数重载--函数重写--函数重定义)
//函数重载--函数重写--函数重定义 #include<iostream> using namespace std; /* 函数重载: 必须在一个类中进行(子类无法重载父类中的函数) 子 ...
- singleton 类模板限制类只能定义一个对象
singleton 类模板限制类只能定义一个对象 singleton 类模板限制类只能定义一个对象 singleton 类模板限制类只能定义一个对象 ???
- C++实验二——函数重载、函数模板、简单类的定义和实现
一.实验过程 函数重载编程练习 实验要求:编写重载函数add(),实现对int型,double型,complex型数据的加法.在main函数中定义不同类型的数据,调用测试. 代码实现: 先是简单的体验 ...
- c++学习笔记之函数重载和模板理解
1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...
- c++之函数重载(函数匹配)
Case void f(); void f(int); void f(int, int); void f(double, double = 3.14); 匹配原则: 1)其形参数量与本次调用提供的实参 ...
- JS高级之简单类的定义和继承
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C++学习笔记之模板(1)——从函数重载到函数模板
一.函数重载 因为函数重载比较容易理解,并且非常有助于我们理解函数模板的意义,所以这里我们先来用一个经典的例子展示为什么要使用函数重载,这比读文字定义有效的多. 现在我们编写一个交换两个int变量值得 ...
- C++ primer(八)--内联函数 引用变量 引用传递函数参数 函数重载/模板/模板具体化
一.内联函数 常规函数和内联函数的区别在于C++编译器如何将他们组合到程序中.编译过程的最终产品是可执行程序--由一组机器语言指令组成.运行程序时,操作系统将这些指令载入到计算机内存中,因此每 ...
- C++函数重载和函数模板(04)
函数重载 函数重载可以使一个函数名具有多种功能,即具有“多种形态”,这种特性称为多态性. C++的多态性又被直观地称为“一个名字,多个函数”.源代码只指明函数调用,而不说明具体调用哪个函数.编译器的这 ...
随机推荐
- 3.对神经网络训练中Epoch的理解
代表的是迭代的次数,如果过少会欠拟合,反之过多会过拟合 EPOCHS 当一个完整的数据集通过了神经网络一次并且返回了一次,这个过程称为一个 epoch. 然而,当一个 epoch 对于计算机而言太 ...
- The Die Is Cast(poj 1481简单的双dfs)
http://poj.org/problem?id=1481 The Die Is Cast Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- Thinkpad机器BIOS下清除安全芯片和指纹数据的方法
清除安全芯片: 首先在刚开机出现ThinkPad图标时,按F1进入BIOS界面,然后长按关机按钮关机(注意一定是断电的关机,不是重新启动)然后开机再按F1键进入BIOS设置.选择“Securiy”-〉 ...
- Oracle的FIXED_DATE参数
今天发现一个有意思的问题, 我们知道,在Oracle数据库中正常执行 select sysdate from dual 都可以返回当前主机的系统时间. 正常修改系统时间,对应的查询结果也会变成修改后的 ...
- js时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- MySQL从删库到跑路(四)——MySQL数据库创建实例
作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.创建数据库 1.创建数据库 创建数据库,指定数据库的默认字符集为utf8.create database sch ...
- python的subprocess的简单使用和注意事项
subprocess是python在2.4引入的模块, 主要用来替代下面几个模块和方法: os.systemos.spawn*os.popen*popen2.*commands.* 可以参考PEP32 ...
- python webdriver 从无到有搭建混合驱动自动化测试框架的过程和总结
一步一步实现混合驱动自动化测试框架的搭建 混合驱动自动化测试框架,是一个非常高级的框架,非常好用,但也很难,不好掌握,需要多练习,就像搭建数据驱动框架一样,需要自己去一点一点的写,一边搭建一边做思路整 ...
- CentOS安装JDK的三种办法
方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创建java目录 [root@localhost ~]# mkdir/usr/java[root@localhost ~]# c ...
- CPU负载过高异常排查实践与总结
昨天下午突然收到运维邮件报警,显示数据平台服务器cpu利用率达到了98.94%,而且最近一段时间一直持续在70%以上,看起来像是硬件资源到瓶颈需要扩容了,但仔细思考就会发现咱们的业务系统并不是一个高并 ...