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. 部署MyEclipse及Tomcat服务器

     修改MySQL文件工程密码 Tomcat6.12\webapps\SmsService\WEB-INF\applicationContext.xml目录下的数据库密码 <property na ...

  2. Mybatis的多对多映射

    一.Mybatis的多对多映射 本例讲述使用mybatis开发过程中常见的多对多映射查询案例.只抽取关键代码和mapper文件中的关键sql和配置,详细的工程搭建和Mybatis详细的流程代码可参见& ...

  3. Lintcode: k Sum II

    Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...

  4. 使用TreeView加载XML文件

    PS: 由于小弟初学编程,本文只写实现方式,代码写的不是很好请见谅! 1.需要读取的xml文档内容 2. 最终实现效果 3  貌似看起实现起来很复杂 但是想想还是挺简单 思路:  读取XML文档 →获 ...

  5. 20165207 Exp2 后门原理与实践

    20165207 Exp2 后门原理与实践 〇.实验准备 两个虚拟机,一个kali一个win7.kali的ip是192.168.43.72,win7的ip是192.168.43.116,在win7关掉 ...

  6. Linux服务器配置---安装vsftpd

    安装vsftpd 大多数Linux系统都使用vsftpd,因此这里我们也安装vsftpd 1.安装vsftpd [root@localhost phpMyAdmin]# yum install -y ...

  7. linux查看是否有某个运行的进程命令

    linux查看是否有某个运行的进程命令:例如,查询是否包含 “my_post” 关键字的进程 ps aux | grep my_post ps aux | grep  my_post | grep - ...

  8. Tomcat的work目录作用

    Tomcat的work目录作用 很多网友喜欢把tomcat的work目录里的东西叫做缓存,其实那不是很恰当,work目录只是tomcat的工作目录,也就是tomcat把jsp转换为class文件的工作 ...

  9. c/c++的typedef/using类型别名

    久而久之,发现c/c++的typedef给类型自定义别名的语法糖就保证设计的一致性而言,确实是个相当不错的特性,跟oracle pl/sql的rowtype或type一样,可惜java.mysql均不 ...

  10. maven parent工程.pom修改后未自动更新

    前两周,因为框架parent工程的pom文件做了一点变更,然后在测试服务器进行maven install的时候,死都找不到新的依赖,都把nexus翻了个遍,确定是最新的了,就是download不下来, ...