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++的多态性又被直观地称为“一个名字,多个函数”.源代码只指明函数调用,而不说明具体调用哪个函数.编译器的这 ...
随机推荐
- MySQL纯透明的分库分表技术还没有
MySQL纯透明的分库分表技术还没有 种树人./oneproxy --proxy-address=:3307 --admin-username=admin --admin-password=D033 ...
- iOS 新浪微博-1.0框架搭建
项目搭建 1.新建一个微博的项目,去掉屏幕旋转 2.设置屏幕方向-->只有竖向 3.使用代码构建UI,不使用storyboard 4.配置图标AppIcon和LaunchImage 将微博资料的 ...
- Ajax棵
ajax 1.什么是ajax?(异步请求,局部刷新) ajax是一个改善用户体验的技术,实质上是利用浏览器端ajax对象()向服务器发送异步(ajax对象在向服务器发送请求的时候,用户可以继续其他操作 ...
- Lintcode: Kth Prime Number (Original Name: Ugly Number)
Ugly number is a number that only have factors 3, 5 and 7. Design an algorithm to find the kth numbe ...
- discuz完善用户资料任务不能完成的解决方法
再来吐槽下discuz. 虽然很很很不想用discuz,但是,,,,..便利性以及各种原因,还得使用. 问题:discuz-运营-任务-完善资料,,,,,,变更部分后无法完成任务. 解决方式如下 原因 ...
- Learning to Rank算法介绍:RankSVM 和 IR SVM
之前的博客:http://www.cnblogs.com/bentuwuying/p/6681943.html中简单介绍了Learning to Rank的基本原理,也讲到了Learning to R ...
- uva11383 转化为 二分图匹配
给定一个n*n矩阵,每个格子里都有一个正整数w(i,j).你的任务是给每行确定一个整数row(i),没列也确定一个正整数col(i),使得对于任意格子(i,j),w(i,j) <= row(i) ...
- FastJson(阿里巴巴)基础
一.所需jar包: fastjson-x.x.xx.jar(本例使用fastjson-1.1.36.jar). 二.解析转化: 1.json字符串 < ------ > js trin ...
- linux常用命令:ln 命令
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- Code Blocks+gtest环境配置
本文仅介绍Code::Blocks+gtest环境配置,gtest具体使用方法请参考: 玩转Google开源C++单元测试框架Google Test系列(gtest)(总) http://www.cn ...