标签:iostream ring 函数重载 vat 一个用户 struct 声明 pre 定义
1.函数重载编程练习
编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型数据,调用测试。
#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 a,b,c; cin>>a>>b; c=add(a,b); cout<<"sum:"<<c<<endl; double x,y,z; cin>>x>>y; z=add(x,y); cout<<"sum:"<<z<<endl; Complex j,k,l; cin>>j.real>>j.imaginary>>k.real>>k.imaginary; l=add(j,k); cout<<"sum:"<<l.real<<"+"<<l.imaginary<<"i"<<endl; return 0; } int add(int m,int n) { return m+n; } double add(double m,double n) { return m+n; } Complex add(Complex a,Complex b) { Complex c; c.real=a.real+b.real; c.imaginary=a.imaginary+b.imaginary; return c; }
#ifndef Sort #define Sort template <class T> void quick(T s[], int low, int high) { int x, y, z = 0; T f, ex; x = low; y = high; f = s[(low + high) / 2]; if (x < y) { while (x< y) { while (f < s[y]) y--; while (f > s[x]) x++; if (x >= y) z = y; else { ex = s[x];s[x] = s[y];s[y] = ex; } } quick(s, low, z); quick(s, z+ 1, high); } } #endif
#include <iostream> #include <iomanip> #include "quick sort.h" using namespace std; int main() { int i; int x[5] = { 57,49,61,16,39}; double y[5] = { 2.5,1.1,9.8,4.7,2.6}; quick(x, 0, 4); quick(y, 0, 4); for (i = 0; i < 5; i++) cout << setw(5) << x[i]; cout << endl; for (i = 0; i < 5; i++) cout << setw(5) << y[i]; system("pause"); return 0; }
#include <iostream> #include <string> using namespace std;// User类的声明 class User { public: User(string name1, string passwd1, string email1); User() { name = ""; passwd = "111111"; email = ""; } void setInfo(string name1="", string passwd1="111111", string email1=""); void changePasswd(); void printInfo(); private: string name; string passwd; string email; }; void User::setInfo(string name1, string passwd1, string email1 ) { name = name1; passwd = passwd1; email = email1; } void User::changePasswd() { string old, n; int i; cout << "Please enter the oldpassword: ";cin >> old; if(old!=passwd){ for (i = 0; i < 2; i++) { cout << "Please enter the newpassword again: "; cin >> old; if (old == passwd) break;} cout << "Please try again afer a while." << endl; } if (old== passwd) {cin >> old;cout << "Please enter the password again:";cin>>n; if(old==n) passwd = old; else cout << "Your entered is wrong!"<<endl; } } void User::printInfo(){ cout << "Name:" << name << endl; cout << "Passwd:" << "******" << endl; cout << "Email:" << email << endl; } 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","568945","xyz@hotmail.com"); user2.printInfo(); return 0; }
标签:iostream ring 函数重载 vat 一个用户 struct 声明 pre 定义
原文地址:https://www.cnblogs.com/jackyayue/p/10589584.html