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:函数重载、函数模板、简单类的定义和实现的更多相关文章

  1. C++ 类的多态二(函数重载--函数重写--函数重定义)

    //函数重载--函数重写--函数重定义 #include<iostream> using namespace std; /* 函数重载: 必须在一个类中进行(子类无法重载父类中的函数) 子 ...

  2. singleton 类模板限制类只能定义一个对象

    singleton 类模板限制类只能定义一个对象 singleton 类模板限制类只能定义一个对象 singleton 类模板限制类只能定义一个对象 ???

  3. C++实验二——函数重载、函数模板、简单类的定义和实现

    一.实验过程 函数重载编程练习 实验要求:编写重载函数add(),实现对int型,double型,complex型数据的加法.在main函数中定义不同类型的数据,调用测试. 代码实现: 先是简单的体验 ...

  4. c++学习笔记之函数重载和模板理解

    1.函数重载: C++ 不允许变量重名,但是允许多个函数取相同的名字,只要参数表不同即可,这叫作函数的重载(其英文是 overload).重载就是装载多种东西的意思,即同一个事物能完成不同功能. 所谓 ...

  5. c++之函数重载(函数匹配)

    Case void f(); void f(int); void f(int, int); void f(double, double = 3.14); 匹配原则: 1)其形参数量与本次调用提供的实参 ...

  6. JS高级之简单类的定义和继承

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. C++学习笔记之模板(1)——从函数重载到函数模板

    一.函数重载 因为函数重载比较容易理解,并且非常有助于我们理解函数模板的意义,所以这里我们先来用一个经典的例子展示为什么要使用函数重载,这比读文字定义有效的多. 现在我们编写一个交换两个int变量值得 ...

  8. C++ primer(八)--内联函数 引用变量 引用传递函数参数 函数重载/模板/模板具体化

    一.内联函数     常规函数和内联函数的区别在于C++编译器如何将他们组合到程序中.编译过程的最终产品是可执行程序--由一组机器语言指令组成.运行程序时,操作系统将这些指令载入到计算机内存中,因此每 ...

  9. C++函数重载和函数模板(04)

    函数重载 函数重载可以使一个函数名具有多种功能,即具有“多种形态”,这种特性称为多态性. C++的多态性又被直观地称为“一个名字,多个函数”.源代码只指明函数调用,而不说明具体调用哪个函数.编译器的这 ...

随机推荐

  1. iOS连续上传多张图片

    参考地址:http://www.cocoachina.com/ios/20180730/24366.html 需求是怎样的:for 循环里面.多个网络请求上传图片,每次上传一张,至于为什么每次只上传一 ...

  2. (转)Springboot 中filter 注入对象

    问题:我建立一个全局拦截器,当然,这是测试的时候建立的,我把它命名为LogFilter,它继承了Filter,web应用启动的顺序是:listener->filter->servlet,而 ...

  3. rsync+inotify安装配置 实时同步文件

    安装 #安装inotify 工具 [root@localhost ~]# yum install inotify-tools -y 常用命令 [root@localhost ~]# inotifywa ...

  4. http协议基础(九)响应首部字段

    响应首部字段: 服务器向客户端返回响应报文中所使用的字段,用于补充的附加信息.服务器信息.以及对客户端的附加要求等 1.Accept-Ranges 告知客户端服务器能否处理范围请求,以指定获取服务器的 ...

  5. requirejs初体验

    1.引入requirejs <script type="text/javascript" src="/js/common/require2.3.5.js" ...

  6. zw版【转发·台湾nvp系列Delphi例程】HALCON MaxImage2

    zw版[转发·台湾nvp系列Delphi例程]HALCON MaxImage2 procedure TForm1.Button1Click(Sender: TObject);var image0, i ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON SigmaImage1

    zw版[转发·台湾nvp系列Delphi例程]HALCON SigmaImage1 procedure TForm1.Button1Click(Sender: TObject);var img, im ...

  8. Object-C-NSFileHandle

    NSFileHandle 类中得到方法可以很方便的对文件数据进行读写.追加,以及偏移量的操作. NSFileHandle 基本步骤: 1.打开文件,获取一个NSFileHandle 对象 2.对打开N ...

  9. 谷歌浏览器 URL无法访问

    使用谷歌浏览器老是会崩溃,或者访问的时候发现“URL无法访问”等失败的问题,连淘宝都没法访问,这个让人很恼火, 最后在扩展应用那里搜到个URL的redirect,问题解决了,~~发现没有再出现类似问题 ...

  10. Impala SQL 语言元素(翻译)[转载]

    原 Impala SQL 语言元素(翻译) 本文来源于http://my.oschina.net/weiqingbin/blog/189413#OSC_h2_2 摘要 http://www.cloud ...