函数重载:
#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语言的时候就没有学的很扎实,以后还要多练习