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. MYSQL中的BlackHole引擎

    MYSQL中的BlackHole引擎 http://blog.csdn.net/ylspirit/article/details/7234021 http://blog.chinaunix.net/u ...

  2. Spark中的Spark Shuffle详解

    Shuffle简介 Shuffle描述着数据从map task输出到reduce task输入的这段过程.shuffle是连接Map和Reduce之间的桥梁,Map的输出要用到Reduce中必须经过s ...

  3. 5分钟带你入门vuex(vue状态管理)

    如果你之前使用过vue.js,你一定知道在vue中各个组件之间传值的痛苦,在vue中我们可以使用vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,那么接下来我们就来学习 ...

  4. centos7最小安装初始化脚本

    #!/bin/bash #zhangsen #lovexlzs@qq.com if [[ "$(whoami)" != "root" ]]; then exit ...

  5. isKindOfClass isMemeberOfClass 的区分

    isKindOfClass If you use such constructs in your code, you might think it is alright to modify an ob ...

  6. 更高效的MergeSort--稍微优化

    0. 简介 本文简要介绍一下比传统MergeSort更高效的算法,在原来的算法Merge基础上,少发生一半拷贝.欢迎探讨,感谢阅读. 原文链接如下:http://loverszhaokai.com/p ...

  7. Web前端开发推荐阅读书籍、学习课程下载

    转自http://www.xuanfengge.com/fe-books.html 前言 学校里没有前端的课程,那如何学习JavaScript,又如何使自己成为一个合格的前端工程师呢? 除了在项目中学 ...

  8. 关于Redis-存Long取Integer类型转换错误的问题;String对象被转义的问题

    背景 最近遇到了两个Redis相关的问题,趁着清明假期,梳理整理. 1.存入Long类型对象,在代码中使用Long类型接收,结果报类型转换错误. 2.String对象的反序列化问题,直接在Redis服 ...

  9. NOSQL学习之二:MongoDB

    MongoDB是一个高性能,开源,无模式的文档型数据库,它在许多场景下可用于替代传统的关系型数据库或键/值存储方式,是当前NoSQL数据库中比较热门的一种. MongoDB使用C++开发.不支持SQL ...

  10. Java一元操作符++详解

    废话不多说,直接上代码. package com.coshaho.learn; /** * * OperatorLearn.java Create on 2016-11-13 下午8:38:15 * ...