实验二 类与对象(2)

任务4:

  Complex.hpp

 1 #pragma once
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 class Complex{
 5     public:
 6         Complex (double r=0,double i=0):real{r},imag{i}{} ;
 7         Complex (const Complex &p):real{p.real},imag{p.imag}{} ;
 8         double get_real() const{return real;}
 9         double get_imag() const{return imag;}
10         void show() const;
11         void add(const Complex &p) ;
12         friend Complex add(const Complex &p1,const Complex &p2) ;
13         friend bool is_equal(const Complex &p1,const Complex &p2) ;
14         friend double abs(Complex &p) ;
15         private:
16             double real,imag;
17 };
18 void Complex::show()const {
19     if (imag > 0)
20         cout << real << "+" << imag << "i";
21     if (imag < 0)
22         cout << real << imag << "i"  ;
23     if (imag == 0)
24         cout << real ;
25 }
26 void Complex::add(const Complex &p){
27     real=real+p.real;
28     imag=imag+p.imag;
29 }
30 Complex add(const Complex& p1, const  Complex& p2) {
31     Complex p;
32     p.real =p1.real + p2.real;
33     p.imag = p1.imag +p2.imag;
34     return p;
35 }
36 bool is_equal(const Complex &p1,const Complex &p2){
37     if(p1.real==p2.real&&p1.imag==p2.imag){
38         return true;
39     }
40     else return false;
41 }
42 double abs(Complex &p){
43      return sqrt(p.real*p.real+p.imag*p.imag);
44 }

  main.cpp

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(4, -5);
 9     const Complex c2(2.7);
10     Complex c3(c1);
11 
12     cout << "c1 = ";
13     c1.show();
14     cout << endl;
15 
16     cout << "c2 = ";
17     c2.show();
18     cout << endl;
19     cout << "c2.imag = " << c2.get_imag() << endl;
20 
21     cout << "c3 = ";
22     c3.show();
23     cout << endl;
24 
25     cout << "abs(c1) = ";
26     cout << abs(c1) << endl;
27 
28     cout << boolalpha;
29     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
30     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
31 
32     Complex c4;
33     c4 = add(c1, c2);
34     cout << "c4 = c1 + c2 = ";
35     c4.show();
36     cout << endl;
37 
38     c1.add(c2);
39     cout << "c1 += c2, " << "c1 = ";
40     c1.show();
41     cout << endl;
42 }
43 
44 int main() {
45     test();
46 }

 

任务5:

  

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 class User{
 6     public:
 7     User(string n0,string p0="111111",string e0 ="");
 8      void set_email();
 9      void change_passwd();
10      void print_info() const;
11      static void print_n();
12      
13      private:
14      string name,passwd,email;
15      static int n;
16 };
17 int User::n = 0;
18 User::User(string n0,string p0,string e0):name{n0},passwd{p0},email{e0}{
19     n++;
20 }
21 
22 void User::set_email(){
23     cout << "Enter emal address: ";
24     cin >> email;
25     cout << "email is set successfully..." << endl;
26 }
27 void User::change_passwd(){
28     string old_password;
29     int count =1;
30     cout << "Enter old password: ";
31     while(count<=3){
32         cin >> old_password;
33         if(old_password==passwd){
34         cout << "Enter new passwd: ";
35         cin >> passwd;
36         cout << "new passwd is set successfully..." << endl;
37         break;
38         }
39         else{
40             if(count!=3){
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             count++;
47         }
48     }
49     
50 }
51 void User::print_info() const{
52     cout << "name: " << name << endl;
53     cout << "passwd: ";
54     for (int i = 0; i < passwd.length(); i++)
55          cout << "*";
56          cout << endl;
57          cout << "eamil: " << email << endl;
58 }
59 void User::print_n(){
60     cout << "there are " << n << " users.";
61 }

  task5.cpp

 1 void test() {
 2     using std::cout;
 3     using std::endl;
 4 
 5     cout << "testing 1......\n";
 6     User user1("Jonny", "92197", "xyz@hotmail.com");
 7     user1.print_info();
 8 
 9     cout << endl
10          << "testing 2......\n\n";
11          
12     User user2("Leonard");
13     user2.change_passwd();
14     user2.set_email();
15     user2.print_info();
16 
17     cout << endl;
18     User::print_n();
19 }
20 
21 int main() {
22     test();
23 }

 

posted on 2022-10-16 23:08  WREKER  阅读(20)  评论(0编辑  收藏  举报

导航