1、函数重载编程练习
#include
using namespace std;
struct Complex {
double real;
double imaginary;
};
int add(int, int);
double add(double,double);
Complex add(Complex, Complex);
int main() {
Complex x={2.1,5.2};
Complex y={3.1,5.1};
Complex c=add(x,y);
cout<<"2+5的和是"<
}
int add(int x,int y){
return x+y;
}
double add(double x,double y){
return x+y;
}
Complex add(Complex x,Complex y){
Complex sum;
sum.imaginary=x.imaginary+y.imaginary;
sum.real=x.real+y.real;
return sum;
}
截图:
2、函数模板编程练习
#include
using namespace std;
template
T partition(T arr[], int low, int high){
T key;
key = arr[low];
while(low
high--;
if(low
while( low
if(low
}
arr[low] = key;
return low;
}
template
void quick_sort(T arr[], int start, int end){
int pos;
if (start
quick_sort(arr,start,pos-1);
quick_sort(arr,pos+1,end);
}
}
int main(){
int i;
int arr[6]={32,12,7, 78, 23,45};
double arrs[9]={32.1,12.8,12.3,11.9,7.5,78.1,23.4,45.5,1.1};
printf("排序前\n");
for(i=0;i<6;i++)
cout<
quick_sort(arr,0,5);
printf("\n排序后\n"); printf("\n\n排序前\n"); 截图: 3、类的定义、实现和使用编程练习 #include 截图:
for(i=0; i<6; i++)
cout<
for(i=0;i<9;i++)
cout<
quick_sort(arrs,0,8);
printf("\n排序后\n");
for(i=0; i<9; i++)
cout<
}
#include
using namespace std;
class User {
public:
User(){};
void setInfo(string names,string passwds="111111",string emails="");
void changePasswd();
void printInfo();
~User(){}
private:
string name;
string passwd;
string email;
};
void User::setInfo(string names,string passwds,string emails){
name=names;
passwd=passwds;
email=emails;
}
void User::changePasswd(){
cout<<"Enter the old passwd:";
string pass;
int count=0;
cin>>pass;
while(pass!=passwd){
cout<<"passwd input error,Please re-Enter again:";
cin>>pass;
count++;
if(count==2){
cout<<"Please try after a while"<
}
}
cout<<"Please sign in new passwd:";
cin>>pass;
passwd=pass;
}
void User::printInfo(){
cout<<"name:"<
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("Ben","12306","[email protected]");
user2.printInfo();
return 0;
}