201983290531 唐乾凱 面向物件程式設計實驗1
阿新 • • 發佈:2021-10-27
#ifndef EMPLOYEE_HPP #define EMPLOYEE_HPP #include<iostream> #include<math.h> using namespace std; class Complex { public: ~Complex(); Complex(float a1) { a=a1; b=0; } Complex(float a1,float b1) { a=a1; b=b1; } Complex(const Complex &c) { a=c.a; b=c.b; } float get_real()const { return a; } float get_imag()const { return b; } void show()const { if(b>=0) printf("%.2f+%.2fi\n",a,b); else printf("%.2f-%.2fi\n",a,b*(-1)); } void add(Complex c2) { a+=c2.a; b+=c2.b; } friend Complex add(Complex c1,Complex c2) { Complex c(0,0); c.a=c1.a+c2.a; c.b=c1.b+c2.b; return c; } friend bool is_equal(Complex c1,Complex c2) { if(c1.a==c2.a&&c1.b==c2.b) return 1; else return 0; } friend float abs(Complex &c) { return sqrt(c.a*c.a+c.b*c.b); } private: float a; float b; }; Complex::~Complex() { } #endif
Complex.h標頭檔案
#include "Complex.h" #include<iostream> int main() { using namespace std; Complex c1(3,4); const Complex c2(4.5); Complex c3(c1); cout<<"c1="; c1.show(); cout<<endl; cout<<"c2="; c2.show(); cout<<endl; cout<<"c2.imag="<<c2.get_imag()<<endl; cout<<"c3="; c3.show(); cout<<endl; cout<<"abs(c1)="; cout<<abs(c1)<<endl; cout<<boolalpha; cout<<"c1==c3:"<<is_equal(c1,c3)<<endl; cout<<"c1==c2:"<<is_equal(c1,c2)<<endl; Complex c4(0,0); c4=add(c1,c2); cout<<"c4=c1+c2="; c4.show(); cout<<endl; c1.add(c2); cout<<"c1+=c2,"<<"c1="; c1.show(); cout<<endl; }
task3.cpp
執行結果為:
標頭檔案USER,h
#ifndef USER_HPP #define USER_HEPP #include<iostream> #include<iomanip> #include<string> using namespace std; class User { private: string name; string password; string email; static int count;//設定靜態成員 public: ~User(); User(string n1); User(string n1,string p1,string e1); void set_email(); void change_password(); void print_info(); void print_n(); static void display_count(); }; int User::count=0;//靜態成員初始化 User::~User() { ++count; } User::User(string n1) { name=n1; password="111111"; email="\0"; ++count; } User::User(string n1,string p1,string e1) { name=n1; password=p1; email=e1; ++count; } void User::set_email() { string newemail; bool flag=0; cout<<"Please set your e-mail:"; cin>>newemail; do { int i=0; flag=0; for(i=0;i<newemail.size();i++) { if(newemail[i]=='@') flag=1; } if(flag) { email=newemail; break; } else { cout<<"E-mail adress is illegal"<<endl; } cout<<"Please set your e-mail:"; cin>>newemail; } while(!flag); } void User::change_password() { string oldpassword; int num=0; bool flag=0; do { cout<<"Enter old password:"; cin>>oldpassword; if(oldpassword.size()!=6) { cout<<"password length error"<<endl; flag=0; } num++; } while(oldpassword!=password&&count!=3&&!flag);//密碼不滿3次繼續進行 if(count==3) { cout<<"Set new password error\n"; exit(0); } else { string newpassword; cout<<"Please set new password:"; cin>>newpassword; while(newpassword.size()!=6) { cout<<"password length error\n"; cout<<"Please set new password:"; cin>>newpassword; } password=newpassword; } } void User::print_info() { cout<<"NAME:"<<name<<endl; cout<<"PASSWORD:******"<<endl; cout<<"E-MAIL:"<<email<<endl; } void User::print_n() { cout<<"There are "<<count<<" users"<<endl; } #endif
task4.cpp
#include "USER.h" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_password(); user2.set_email(); user2.print_info(); user2.print_n(); }
執行測試截圖為: