实验一 类与对象

TASK 3:Complex.hpp

 1 #include<math.h>
 2 #include<iostream>
 3 using namespace std;
 4 class Complex
 5 {
 6   private: double real,imag;
 7   public:
 8       Complex(double r=0,double i=0):real(r),imag(i){}
 9       
10         Complex(const Complex &c){
11             real=c.real;
12             imag=c.imag;
13         }//构造复制函数 
14         double get_real() const;//返回实部 
15         double get_imag() const;//返回虚部 
16         void show() const;//输出结果
17         
18          void add(const Complex &a){
19              real+=a.real;
20              imag+=a.imag;
21          }
22          
23          //友元函数
24          friend bool is_equal(const Complex &c1,const Complex &c2);//判断是否相同 
25          friend Complex add(const Complex &c1,const Complex &c2);//两个复数相加
26          friend double abs(const Complex &c1);//取模        
27 };
28 
29 double Complex::get_real() const{
30 return real;
31 }
32 double Complex::get_imag() const{
33 return imag;
34 }
35 void Complex::show() const{
36     if(real!=0){
37         if(imag>0)
38             cout<<real<<"+"<<imag<<"i";
39             else if(imag==0) 
40             cout<<real<<endl;
41             else
42             cout<<real<<imag<<"i";
43     }
44     else if(imag!=0){
45         if(imag>0)
46             cout<<real<<"+"<<imag<<"i";
47             else if(imag==0) 
48             cout<<real<<endl;
49             else
50             cout<<real<<imag<<"i";
51     }
52     }
53 
54 bool is_equal(const Complex &c1,const Complex &c2){
55     if(c1.real==c2.real&&c1.imag==c2.imag)
56     return true;
57     else 
58     return false;
59 }
60 
61 Complex add(const Complex &c1,const Complex &c2){
62     Complex c3;
63     c3.real=c1.real+c2.real;
64     c3.imag=c1.imag+c2.imag;
65     return c3;
66 }
67 
68 double abs(const Complex &c1){
69     return sqrt(c1.real*c1.real+c1.imag*c1.imag);
70 }

task.cpp

#include "Complex.hpp"
#include <iostream>

int main()
{
    using namespace std;

    Complex c1(3,2);
    const Complex c2(5.4);
    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;
    c4 = add(c1, c2);
    cout << "c4 = c1 + c2 = ";
    c4.show();
    cout << endl;

    c1.add(c2);
    cout << "c1 += c2, " << "c1 = ";
    c1.show();
    cout << endl;
    getchar();
}

原数据运行结果和更换数据测试结果如下

 

 Task 4 User.hpp

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 class User{
 6 private:
 7     string name,password,email;
 8     static int n;
 9 public:
10     User(string na,string p="111111",string e=""):name(na),password(p),email(e){
11         n++;}
12     User(){n++;};
13         
14     void set_email();//s设置邮箱 
15     void change_password();//修改密码 
16     void print_info();//输出结果 
17     static void print_n();
18 };
19 int User::n=0;//类外成员初始化 
20 
21 void User::set_email(){
22     cout<<"Enter email address: ";
23     cin>>email;
24     cout<<"Email is set successfully...";
25 }//设置邮箱 
26 
27 void User::change_password(){
28     cout<<"Enter old password: ";
29     string t0,t1;
30     int num=0;
31     for(int i=1;i<=3;i++){   //不超过三次 
32         cin>>t0;
33         if(t0==password)
34         {
35             cout<<"Enter new password: ";
36             cin>>t1;
37             cout<<"new password is set successfully..."<<endl;
38             return;
39         }
40         else if(i<=2&&t0!=password){
41             cout<<"password input error. Please re-enter again: ";
42         }
43         else 
44             cout<<"Password input error.Please try after a while."<<endl;
45     }
46 }//重设密码 
47 
48 void User::print_info(){
49     cout<<"name: "<<name<<endl;
50     cout<<"password: ******"<<endl;
51     cout<<"email: "<<email<<endl;
52 }
53 void User::print_n(){
54     cout<<"There are "<<n<<" users."<<endl;
55 }

task4.cpp

 1 #include "User.hpp"
 2 #include <iostream>
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     cout << "testing 1......" << endl;
 9     User user1("sss", "020320", "sssz@hotmail.com");
10     user1.print_info();
11 
12     cout << endl
13          << "testing 2......" << endl
14          << endl;
15     User user2("xiaoming");
16     user2.change_password();
17     user2.set_email();
18     user2.print_info();
19 
20     User::print_n();
21 }

 

 

posted @ 2021-10-24 12:32  zoracoding  阅读(29)  评论(3)    收藏  举报