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

实验二 类和对象(2)

时间:2022-10-14 23:12:41浏览次数:54  
标签:const cout 对象 double void Complex 实验 User

任务四代码:

  Complex.hpp文件源码:

 1 #pragma once
 2 
 3 //Complex类的定义
 4 #include<iostream>
 5 #include<cmath>
 6 
 7 using namespace std;
 8 
 9 class Complex
10 {
11 public:
12     Complex(double real0 = 0, double imag0 = 0);
13     Complex(const Complex& c);//复制构造函数
14     ~Complex() = default;//析构函数
15 
16     double get_real() const ;//返回复数实部
17     double get_imag() const ;//返回复数虚部
18     void show() const ;//用于输出复数
19     void add(Complex const& c);//将一个复数加到自身
20 
21     friend Complex add(Complex const& c1, Complex const& c2);//两个复数相加
22     friend bool is_equal(Complex const& c1, Complex const& c2);//判断两个复数是否相等
23     friend double abs(Complex const& c);//取模
24 
25 private:
26     double real;
27     double imag;
28 };
29 
30 //带参数的构造函数
31 Complex::Complex(double real0, double imag0) :real(real0), imag(imag0){}
32 
33 //复制构造函数
34 Complex::Complex(const Complex& c) : real{ c.real }, imag{ c.imag }{}
35 
36 //返回复数实部
37 double Complex::get_real() const
38 {
39     return real;
40 }
41 
42 //返回复数虚部
43 double Complex::get_imag() const
44 {
45     return imag;
46 }
47 
48 //输出复数
49 void Complex::show() const
50 {
51     if (real != 0 && imag != 0)
52         cout << real << imag << " i " ;
53     else if (real == 0 && imag != 0)
54         cout << imag << " i " ;
55     else if (real != 0 && imag == 0)
56         cout << real ;
57     else
58         cout << "0" ;
59 }
60 
61 //将一个复数加到自身
62 void Complex::add(Complex const& c)
63 {
64     real += c.real;
65     imag += c.imag;
66 }
67 
68 //两个复数相加
69 Complex add(Complex const& c1, Complex const& c2)
70 {
71     Complex c3;
72     c3.real = c1.real + c2.real;
73     c3.imag = c1.imag + c2.imag;
74     return c3;
75 }
76 
77 //两个复数是否相等
78 bool is_equal(Complex const& c1, Complex const& c2)
79 {
80     if (c1.real == c2.real && c1.imag == c2.imag)
81         return true;
82     else
83         return false;
84 }
85 
86 //取模
87 double abs(Complex const& c)
88 {
89     return sqrt(c.real * c.real + c.imag * c.imag);
90 }

  task4.cpp源码:

 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 // 类测试
 5 void test() {
 6     using namespace std;
 7 
 8     Complex c1(5, -2);
 9     const Complex c2(2.5);
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 
41     c1.show();
42     cout << endl;
43 }
44 int main() {
45     test();
46 }

任务四运行截图:

 

 

任务五代码:

  User.hpp文件源码:

 1 #pragma once
 2 
 3 //User类的定义
 4 #include<iostream>
 5 #include<string>
 6 
 7 using namespace std;
 8 class User
 9 {
10 public:
11     User(string name0, string passwd0 = "111111", string email0 = "");//构造函数
12     ~User() = default;//析构函数
13 
14     void set_email();//设置邮箱
15     void change_passwd();//修改密码
16     void print_info() const;//打印用户信息
17     static void print_n();//打印用户总数
18 
19 private:
20     string name, passwd, email;
21     static int n;
22 };
23 
24 int User::n = 0;
25 
26 //构造函数
27 User::User(string name0,string passwd0,string email0)
28 {
29     name = name0;
30     passwd = passwd0;
31     email = email0;
32     n++;
33 }
34 
35 //设置邮箱
36 void User::set_email()
37 {
38     cout << "Enter email address: ";
39     cin >> email;
40     cout << "email is set successfully..." << endl;
41 }
42 
43 //修改密码
44 //修改密码前,要求先输入旧的密码,验证无误后,才允许修改;如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。
45 void User::change_passwd()
46 {
47     string old_passwd;
48     int count = 0;
49     cout << "Enter old password: ";
50     while (count != 3)
51     {
52         cin >> old_passwd;
53         if (old_passwd==passwd)
54         {
55             cout << "Enter new passwd: ";
56             cin >> passwd;
57             cout << "new passwd is set successfully..." << endl;
58             break;
59         }
60         else
61         {
62             count++;
63             if (count != 3)
64                 cout << "password input error.Please re-enter again: ";
65             else
66                 cout << "password input error.Please try after a while." << endl;
67         }
68     }
69     
70 }
71 
72 //打印用户信息
73 //打印用户名、密码、联系邮箱。其中,密码以与真实密码相同位数的*显示,比如密码为12345,则显示 *****。
74 void User::print_info() const
75 {
76     cout << "name: " << name << endl
77          << "passwd: ";
78     for (int i = 0; i < passwd.length(); i++)
79         cout << "*";
80     cout << endl
81          << "email: " << email << endl;
82 }
83 
84 //打印用户总数
85 void User::print_n()
86 {
87     cout << "there are " << n << " users.";
88 }

  task5.cpp源码:

 1 #include "User.hpp"
 2 #include<iostream>
 3 
 4 //调试User类 
 5 void test()
 6 {
 7     using std::cout;
 8     using std::endl;
 9 
10     cout << "testing 1......\n";
11     User user1("Brown", "980525", "[email protected]");
12     user1.print_info();
13 
14     cout << endl
15          << "testing 2......\n\n";
16 
17     User user2("Yellow");
18     user2.change_passwd();
19     user2.set_email();
20     user2.print_info();
21 
22     cout << endl;
23     User::print_n();
24 }
25 
26 int main()
27 {
28     test();
29 }

任务五运行截图:

 

 

 

 

       

标签:const,cout,对象,double,void,Complex,实验,User
From: https://www.cnblogs.com/jane-d/p/16792822.html

相关文章