实验二——函数重载,快速排序,类对象
函数重载:
#include<iostream>
using namespace std;
struct complex{
double real;
double imaginary;
};
int add(int,int);
double add(double,double);
complex add(complex,complex);
int main()
{
int a1=2,b1=3;
double a2=2.0,b2=3.0;
struct complex num1={2,3},num2={3,4};
cout<<add(a1,b1)<<endl;
cout<<add(a2,b2)<<endl;
complex n=add(num1,num2);
cout<< n.real<<"+"<<n.imaginary<<"i"<<endl;
return 0;
}
int add(int a,int b){return a+b;}
double add(double a,double b){return a+b;}
complex add(complex a,complex b){
struct complex m;
m.real=a.real+b.real;
m.imaginary=a.imaginary+b.imaginary;
return m;
}
快速排序:
#include<iostream>
using namespace std;
void quicksort(int a[], int low, int high)
{
if(low>=high){return;}
int num1=low;
int num2=high;
int sample=a[num1];
while(num1<num2)
{
while(num1<num2&&a[num2]>=sample)
--num2;
a[num1]=a[num2];
while(num1<num2&&a[num1]<=sample)
++num1;
a[num2]=a[num1];
a[num1]=sample;
quicksort(a,low,num1-1);
quicksort(a,num1+1,high);
}
}
int main()
{
int i;
int a[]={4,20,15,14,19,7,8};
cout<<"before:";
for(i=0;i<7;i++)
cout<<a[i]<<" ";
cout<<endl;
quicksort(a,0,6);
cout<<"after:";
for(i=0;i<7;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
简单类对象:
#include<iostream>
#include<string>
using namespace std;
class user
{
public:
void setinfo(string name1,string password1="111111",string email1=" ");
void changepassword();
void printinfo();
private:
string name;
string password;
string email;
};
void user::setinfo(string name1,string password1,string email1)
{
name=name1;
password=password1;
email=email1;
}
void user::changepassword()
{
int i=0;
string mima;
while(1)
{
cout<<"enter the old password:";
cin>>mima;
if(mima==password)
{
cout<<"enter the new password:";
cin>>password;
break;
}
if(mima!=password)
{
i++;
cout<<"the password is wrong,please resume load!"<<endl;
if(i==3)
{
cout<<"please try after a short while!";
break;
}
}
}
}
void user::printinfo()
{
cout<<"name:"<<name<<endl;
cout<<"password:"<<password<<endl;
cout<<"email:"<<email<<endl;
}
int main()
{
cout<<"please enter the first information"<<endl;
user user1;
user1.setinfo("Leonard");
user1.changepassword();
user1.printinfo();
cout<<"please enter the second information"<<endl;
user user2;
user2.setinfo("yfwg","173779","yfwg@bkymail.com");
user2.printinfo();
return 0;
}
实验总结:
1、对于快速排序的算法依旧很生疏,这里的代码借助了百度百科。
2、对于类对象的代码依旧没有形成充足的认识,这次写类对象的代码参考了很多,自己的能力实在不足。要加强这方面练习。
3、对于结构体这一块依旧很不熟练,在c语言的时候就没有学的很扎实,以后还要多练习
转载于:https://www.cnblogs.com/yfwg/p/10594280.html
实验二——函数重载,快速排序,类对象相关推荐
- 深圳大学计软《面向对象的程序设计》实验15 函数模板和类模板
A. 有界数组模板类(类模板) 题目描述 编写有界数组模板BoundArray(即检查对数组元素下标引用并在下标越界时终止程序的执行),能够存储各种类型的数据.要求实现对数组进行排序的方法sort,及 ...
- C++学习笔记:(二)函数重载 常量与引用
目录 3.函数重载 3.1 非成员函数重载 3.2 成员函数重载 3.3 函数的默认参数 3.4 内联函数 4.常量与引用 4.1 const的最初动机 4.2 const与指针 4.3 const与 ...
- C++基础知识 —— 内存分区模型、引用、函数重载、类和继承、this指针、友元、多态、文件操作
参考 黑马程序员 C++课程笔记,个人理解及整理 可以使用 在线编译c++代码 来简单验证.学习代码 目录 C++核心编程 1. 内存分区模型 1.1 程序运行前 1.2 程序运行后 1.3 ...
- const成员函数、const类对象、mutable数据成员
1. const成员函数 只是告诉编译器,表明不修改类对象. 但是并不能阻止程序员可能做到的所有修改动作,比如对指针的修改,编译器可能无法检测到 2. 类体外定义的const成员函数,在定义和声明处都 ...
- python--第六章 python函数 装饰器 类 对象
一.装饰器 1.什么是装饰器 ''' 装饰器''' # 创建几个函数 def add(a,b):'''求任意两个数的和'''print('计算开始:')r = a + breturn rprint(' ...
- python类的成员函数_Python为类对象动态添加成员函数
Python: 为对象动态添加函数 , 且函数定义 来自一个 str 在 Python 中 , 通常情况下 , 你只能为对象添加一个已经写好的方法 需求 : 传入一个 str 类型的变量 , 其值是一 ...
- python函数参数为类对象_将Cython类对象作为参数传递给C函数
问题已经解决了.这是因为类中的方法被定义为cdef,而不是{}.在 样品c #include "python.h" void c_func(PyObject *obj){ PyGI ...
- C++ 实验2:函数重载、函数模板、简单类的定义和实现
1.函数重载编程 编写重载函数add(),实现对int型,double型,Complex型数据的加法.在main()函数中定义不同类型 数据,调用测试. #include <iostream&g ...
- C++:运算符重载与类的赋值运算符重载函数
目录 章节知识架构 一.运算符重载 1. 运算符重载的基本概念 代码段1 2.关于运算符重载的重要语法细则 二.运算符重载在类中的使用 三.类的默认成员函数:=重载函数(赋值运算符重载) 1.自定义= ...
最新文章
- 【快乐水题】997. 找到小镇的法官
- 一、把握 Netty 整体架构脉络
- 【译】PGP Web of Trust: Core Concepts Behind Trusted Communication
- boost::signals2::deconstruct相关的测试程序
- 开机动画适配方案_2020 年 4 月前 App 启动画面、屏幕调整需按要求适配,否则存拒审风险!...
- 机器学习预测+akshare
- Java基础之深入认识hashCode和equals
- ae在哪里直接复制合成_AE模板里修改复制的合成如何不影响原先的合成?
- Linux安装docker及docker基本操作
- 打开浏览器不是主页_教你如何锁定电脑浏览器主页防止被篡改
- 智能优化算法应用:基于GWO优化的指数熵图像多阈值分割 - 附代码
- rabbits php实现文件下载!
- My eclipse和Eclipse平台 JSP可视化编程工具
- 【定位】纯激光导航定位丢失/漂移问题的优化方案及思考
- 彻底理解View事件体系!
- 汤唯:在街头卖艺的那些日子
- Vue——May(1)
- 中规中矩的CentOS7安装Python3.5
- 基于Vue2.x的前端架构,我们是这么做的
- 字符输出流,缓冲流和序列化
热门文章
- 变更控制管理流程图_制度是最好的老板,流程就是最好的管理!流程建立法则(附案例)...
- 每日一题(45)—— 字符数组找错
- AllocateAndInitializeSid function
- bootstrap 居中 表格中_使用Twitter Bootstrap在表格单元格中垂直居中
- python获取目录树_Python读取文件目录树——os.walk
- 福州大学c语言考试答案,C语言练习模拟考福州大学工程技术学院.doc
- LeetCode 1710. 卡车上的最大单元数(排序,模拟)
- LeetCode 833. 字符串中的查找与替换(排序,replace)
- 动态规划应用--双11购物凑单
- android qml 菜单,QML - ListView项目,用于显示菜单