首页 > 其他分享 >实验二 类和对象(二)

实验二 类和对象(二)

时间:2022-10-18 17:55:45浏览次数:43  
标签:real const cout 对象 imag 实验 obj include

 1 #include <iostream>
 2 #include <complex>
 3 int main()
 4 {
 5     using namespace std;
 6     complex<double> c1{3, 4}, c2{4.5};
 7 const complex<double> c3{c2};
 8 cout << "c1 = " << c1 << endl;
 9 cout << "c2 = " << c2 << endl;
10 cout << "c3 = " << c3 << endl;
11 cout << "c3.real = " << c3.real() << endl;
12 cout << "c3.imag = " << c3.imag() << endl;
13 cout << "c1 + c2 = " << c1 + c2 << endl;
14 cout << "c1 - c2 = " << c1 - c2 << endl;
15 cout << "abs(c1) = " << abs(c1) << endl;
16 cout << boolalpha;
17 cout << "c1 == c2 : " << (c1 == c2) << endl;
18 cout << "c3 == c2 : " << (c3 == c2) << endl;
19 complex<double> c4 = 2;
20 cout << "c4 = " << c4 << endl;
21 c4 += c1;
22 cout << "c4 = " << c4 << endl;
23 }

试验任务一截图

 

 

 1 #include <iostream>
 2 #include <array>
 3 template<typename T>
 4 void output1(const T &obj) {
 5 for(auto i: obj)
 6 std::cout << i << ", ";
 7 std::cout << "\b\b \n";
 8 }
 9 template<typename T>
10 void output2(const T &obj) {
11 for(auto p = obj.cbegin(); p != obj.cend(); ++p)
12 std::cout << *p << ", ";
13 std::cout << "\b\b \n";
14 }
15 int main() {
16 using namespace std;
17 array<int, 5> x1;
18 cout << "x1.size() = " << x1.size() << endl;
19 x1.fill(42); 
20 x1.at(0) = 999; 
21 x1.at(4) = -999; 
22 x1[1] = 777; 
23 cout << "x1: ";
24 output1(x1);
25 cout << "x1: ";
26 output2(x1);
27 array<double, 10> x2{1.5, 2.2}; 
28 cout << "x2.size() = " << x2.size() << endl;
29 cout << "x2: ";
30 output1(x2);
31 array<int, 5> x3{x1};
32 cout << boolalpha << (x1 == x3) << endl;
33 x3.fill(22);
34 cout << "x3: ";
35 output1(x3);
36 swap(x1, x3); 
37 cout << "x1: ";
38 output1(x1);
39 cout << "x3: ";
40 output1(x3);
41 }

实验任务二截图

 

 实验任务四

#include<iostream>
#include<math.h>

using namespace std;

class Complex{

public:
        
    Complex (double x = 0, double y = 0) : real{x}, imag{y} {}
    Complex (const Complex &obj) : real{obj.real} , imag{obj.imag} {}
    
    double get_real() const { return real; }
    double get_imag() const { return imag; }
    
    void show() const {
        cout << real;
        if(imag > 0)
            cout << " + " << imag << "i";
        else if(imag < 0)
            cout << " - " << -imag << "i";
    }
    
    void add(const Complex &obj){
        real += obj.get_real();
        imag += obj.get_imag();
    }

    friend Complex add(const Complex &a, const Complex &b){
        return Complex(a.real + b.real, a.imag + b.imag);
    }

    friend bool is_equal(const Complex &a, const Complex &b){
        return fabs(a.real - b.real) <= 1e-5 && fabs(a.imag - b.imag) <= 1e-5;
    }

    
    friend double abs(const Complex &obj){
        return sqrt(obj.real * obj.real + obj.imag * obj.imag);
    }

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

截图

 

 更换数组后的结果

 

 实验任务五

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 
 5 class User{
 6 
 7 public:
 8     
 9     User (string Name, string Passwd = "111111", string Email = "") 
10         : name{Name} , passwd{Passwd}, email{Email} {n ++; }
11         
12     void set_email(){
13         cout << ("Enter email address: ");
14         cin >> email;
15         puts("email is set successfully...");
16     }
17     
18     void change_passwd(){
19         
20         string Passwd;
21         
22         cout << ("Enter old password: ");
23         cin >> Passwd;
24         
25         for(int wrong = 1; wrong <= 2 && Passwd != passwd; wrong ++){
26             cout <<("password input error. Please re-enter again: ");
27             cin >> Passwd;
28         }
29         
30         if(Passwd == passwd){
31             cout << "Enter new passwd: ";
32             cin >> passwd;
33             puts("new passwd is set successfully...");
34         }
35         else
36             puts("password input error. Please try after a while.");
37     
38     }
39     
40     void print_info(){
41         cout << "name:   " << name << "\n";
42         cout <<"passwd: ";
43         
44         for(int i = 0; passwd[i]; i ++)
45             cout << "*";
46         cout << "\n";
47         cout << "email:  " << email << "\n";
48     }
49     
50     static void print_n(){
51         cout << "there are " << n << " users.\n";
52     }
53     
54 private:
55     string name, passwd, email;
56     static int n;
57 }; 
58 
59 int User::n = 0;
 1 #include<iostream>
 2 #include"User.hpp"
 3 
 4 void test(){
 5     
 6     
 7     using std::cout;
 8     using std::endl;
 9     
10     cout << "testing 1......\n";
11     
12     User user1("Jonny", "92197", "[email protected]");
13     user1.print_info();
14     
15     cout << endl << "testing 2......\n\n";
16     
17     User user2("Leonard");
18     user2.change_passwd();
19     user2.set_email();
20     user2.print_info();
21     
22     cout << endl;
23     User::print_n();
24     
25     
26 }
27 
28 int main(){
29     test();

 


 

 


 在更换一组信息过后

 

 



标签:real,const,cout,对象,imag,实验,obj,include
From: https://www.cnblogs.com/yy-L886/p/16803495.html

相关文章