实验一 类和对象
四、实验结论
实验任务三
complex.hpp
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_Hpp 3 4 #include<iostream> 5 #include<cmath> 6 7 using namespace std; 8 9 class Complex{ 10 private: 11 double real; 12 double imag; 13 public: 14 Complex(); 15 Complex(double real0,double imag0=0); 16 Complex(const Complex &c); 17 18 double get_real() const; 19 double get_imag() const; 20 void show() const; 21 void add(const Complex &c); 22 23 friend Complex add(Complex &c1,const Complex &c2); 24 friend bool is_equal(Complex c1,Complex c2); 25 friend double abs(Complex &c1); 26 }; 27 Complex::Complex(){} 28 Complex::Complex(double real0,double imag0):real{real0},imag{imag0}{} 29 Complex::Complex(const Complex &c):real{c.real},imag{c.imag}{} 30 31 double Complex::get_real() const{return real;} 32 double Complex::get_imag() const{return imag;} 33 34 void Complex::show() const{ 35 if(imag>0)cout<<real<<"+"<<imag<<"i"; 36 else if(imag<0) cout<<real<<imag<<"i"; 37 else cout<<real; 38 } 39 void Complex::add(const Complex &c){ 40 imag+=c.imag; 41 real+=c.real; 42 43 } 44 45 Complex add(Complex &c1,const Complex &c2){ 46 Complex c3(0.0,0.0); 47 c3.real=c1.real+c2.real; 48 c3.imag=c1.imag+c2.imag; 49 return c3; 50 } 51 bool is_equal(Complex c1,Complex c2){ 52 if (c1.real==c2.real&&c1.imag==c2.imag) return true; 53 return false; 54 } 55 double abs(Complex &c){ 56 return sqrt(c.real*c.real+c.imag*c.imag); 57 } 58 #endif
task.cpp
1 int main() 2 { 3 using namespace std; 4 5 Complex c1(1, -2); 6 const Complex c2(1.5); 7 Complex c3(c1); 8 9 cout << "c1 = "; 10 c1.show(); 11 cout << endl; 12 13 cout << "c2 = "; 14 c2.show(); 15 cout << endl; 16 cout << "c2.imag = " << c2.get_imag() << endl; 17 18 cout << "c3 = "; 19 c3.show(); 20 cout << endl; 21 22 cout << "abs(c1) = "; 23 cout << abs(c1) << endl; 24 25 cout << boolalpha; 26 cout << "c1 == c3 : " << is_equal(c1, c3) << endl; 27 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 28 29 Complex c4; 30 c4 = add(c1, c2); 31 cout << "c4 = c1 + c2 = "; 32 c4.show(); 33 cout << endl; 34 35 c1.add(c2); 36 cout << "c1 += c2, " << "c1 = "; 37 c1.show(); 38 cout << endl; 39 }
运行测试结果
实验任务四
User.hpp
1 #ifndef USER_HPP 2 #define USER_HPP 3 4 #include<iostream> 5 #include<iomanip> 6 #include<string> 7 8 using namespace std; 9 10 class User{ 11 private: 12 string name; 13 string passwd; 14 string email; 15 static int n; 16 public: 17 User(string name0,string passwd0="111111",string email0=""); 18 void set_email(); 19 void change_passwd(); 20 void print_info(); 21 static void print_n(); 22 }; 23 24 int User::n=0; 25 26 User::User(string name0,string passwd0,string email0):name{name0},passwd{passwd0},email{email0}{n++;} 27 28 void User::set_email(){ 29 cout<<"Enter email address: "; 30 cin>>email; 31 cout<<"email is set successfully..."<<endl; 32 } 33 34 void User::change_passwd(){ 35 int count=1; 36 string oldpasswd; 37 cout<<"Enter old password: "; 38 cin>>oldpasswd; 39 while(oldpasswd!=passwd&&count<=2){ 40 cout<<"password input error.Please re-enter again: "; 41 cin>>oldpasswd; 42 ++count; 43 } 44 if(count==3&&oldpasswd!=passwd){ 45 cout<<"password input error.Please try after a while."<<endl; 46 return; 47 } 48 cout<<"Enter new passwd: "; 49 cin>>passwd; 50 cout<<"new passwd is set successfully..."<<endl; 51 } 52 53 void User::print_info(){ 54 cout<<setw(8)<<setiosflags(ios::left)<<"name: "<<name<<endl; 55 cout<<setw(8)<<setiosflags(ios::left)<<"passwd: "<<"******"<<endl; 56 cout<<setw(8)<<setiosflags(ios::left)<<"email: "<<email<<endl; 57 } 58 59 void User::print_n(){ 60 cout<<"there are "<<n<<" users."<<endl; 61 } 62 63 64 #endif
task.cpp
#include "User.hpp"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Helen", "921111", "aaa@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Leo");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}
运行测试结果
五、实验总结
1、注意输出的格式
2、注意const用法
3、对类和对象有了更深刻的认识