实验1 类与对象
#include <iostream> #include<cmath> using namespace std; class complex{ public: complex(double real=0, double imag=0):REAL{real},IMAG{imag}{} complex(const complex &obj):REAL{obj.REAL},IMAG{obj.IMAG}{} ~complex()=default; double get_real()const; double get_imag()const; double show()const; complex add(const complex &c); friend complex add(const complex &c1,const complex &c2); friend bool is_equal(const complex &c1,const complex &c2); friend double abs(complex &c); private: double REAL,IMAG; }; double complex::get_real()const { return REAL; } double complex::get_imag()const { return IMAG; } double complex::show()const { if(IMAG>0) { cout<<REAL<<" + "<<IMAG<<"i"; } else{ if(IMAG==0){ cout<<REAL; } else { cout<<REAL<<" - "<<IMAG*(-1)<<"i"; } } } complex complex::add(const complex &c) { REAL+=c.REAL; IMAG+=c.IMAG; return c; } complex add(const complex &c1,const complex &c2) { complex c; c.REAL=c1.REAL+c2.REAL; c.IMAG=c1.IMAG+c2.IMAG; return c; } bool is_equal(const complex &c1,const complex &c2) { if(c1.REAL==c2.REAL&&c1.IMAG==c2.IMAG) { return true; } else { return false; } } double abs(complex &c) { return sqrt(c.REAL*c.REAL+c.IMAG*c.IMAG); }
1 #include"Complex.hpp" 2 #include <iostream> 3 #include<cmath> 4 using namespace std; 5 6 7 int main() 8 { 9 complex c1(1,2); 10 const complex c2(3.5); 11 complex c3(c1); 12 13 cout << "c1 = "; 14 c1.show(); 15 cout << endl; 16 17 cout << "c2 = "; 18 c2.show(); 19 cout << endl; 20 cout << "c2.imag = " << c2.get_imag() << endl; 21 22 cout << "c3 = "; 23 c3.show(); 24 cout << endl; 25 26 cout << "abs(c1) = "; 27 cout << abs(c1) << endl; 28 29 cout << boolalpha; 30 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 31 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 32 33 complex c4; 34 c4 = add(c1, c2); 35 cout << "c4 = c1 + c2 = "; 36 c4.show(); 37 cout << endl; 38 39 c1.add(c2); 40 cout << "c1 += c2, " << "c1 = "; 41 c1.show(); 42 cout << endl; 43 }
#include<iostream> #include<string> #include<conio.h> #include<cstring> using namespace std; class user{ public: user(string NAME,string PASSWD="111111",string EMAIL=" "):name{NAME},passwd{PASSWD},email{EMAIL}{count++;} user(const user &obj):name{obj.name},passwd{obj.passwd},email{obj.email}{count++;} ~user(){count--;} void set_email(); //一开始没有注意到是void类型,导致了程序错误 void print_Info(); void changepasswd (); static void print_n(){ cout<<"there are "<<count<<" users."; } private: static int count; string name; string passwd; string email; }; int user::count=0; //初始化极为重要 void user::changepasswd(){ string oldpasswd; int chance=0; cout<< "Enter old password: "; while(oldpasswd!=passwd){ cin>>oldpasswd; if(oldpasswd==passwd) { chance=2; break; } chance++; if(chance==3) break; else{ cout <<"password input error. Please re-enter again:"; } } if(chance==3) { cout << "password input error. Please try after a while"<< endl; } else{ cout << "Enter new password: "; cin>>passwd; cout<<"new passwd is set sucessfully..."<<endl; } } void user::set_email(){ cout<<"Enter email address:"; cin>>email; cout<<"email is set sucessfully..."<<endl; } void user::print_Info(){ cout << "name: " << name<<endl; cout << "passwd: " << "******"<<endl ; cout << "email: " << email<<endl; }
task4测试数据
1 #include "User.hpp" 2 #include <iostream> 3 int main() 4 { 5 using namespace std; 6 7 cout << "testing 1......" << endl; 8 user user1("Jonny", "92197", "xyz@hotmail.com"); 9 user1.print_Info(); 10 11 cout << endl 12 << "testing 2......" << endl 13 << endl; 14 user user2("Leonard"); 15 user2.changepasswd(); 16 user2.set_email(); 17 user2.print_Info(); 18 user::print_n(); 19 }