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

实验二 类和对象(2)

时间:2022-10-18 22:44:46浏览次数:35  
标签:std cout 对象 imag Complex 实验 using include

实验任务4

h.pp

 1 #pragma once
 2 #include<iostream>
 3 #include<cmath>
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Complex {
 8 private:
 9     double real;
10     double imag;
11 
12 
13 public:
14     Complex(double  m = 0) :real{ m }, imag{ 0 } {}
15     Complex(double  m, double n) :real{ m }, imag{ n } {}
16     Complex(const Complex& obj) { real = obj.real; imag = obj.imag; }
17     double get_real() const { return real; }
18     double get_imag() const { return imag; }
19     void show() {
20         if (imag > 0 && imag != 1) cout << real << "+" << imag << "i";
21         else if (imag < 0 && imag != -1) cout << real << imag << "i";
22         else if (imag == 1) cout << real << "+" << "i";
23         else if (imag == -1) cout << real << "-" << "i";
24         else if (imag == 0)cout << real;
25     }
26     void show() const {
27         if (imag > 0 && imag != 1) cout << real << "+" << imag << "i";
28         else if (imag < 0 && imag != -1) cout << real << imag << "i";
29         else if (imag == 1) cout << real << "+" << "i";
30         else if (imag == -1) cout << real << "-" << "i";
31         else if (imag == 0)cout << real;
32     }
33     void add(const Complex& obj) { real += obj.real; imag += obj.imag; }
34     friend Complex add(const Complex& c1, const Complex& c2);
35     friend bool is_equal(const Complex &c1,const Complex &c2);
36     friend double abs(const Complex& c1);
37 };
38 Complex add(const Complex& c1, const Complex& c2) { 
39     Complex c3;
40     c3.real = c1.real + c2.real; 
41     c3.imag = c1.imag + c2.imag; 
42     return c3;
43 }
44 bool is_equal(const Complex& c1, const Complex& c2){ 
45     bool a1 = false;
46     if (c1.real == c2.real)
47         if (c1.imag == c2.imag)
48             a1 = true;
49         return a1;
50 }
51 double abs(const Complex& c1) {
52     double s = 0.0;
53     s = sqrt(c1.real * c1.real + c1.imag * c1.imag);
54     return s;
55 }

.cpp

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

实验任务5

.hpp

#pragma once
#include<iostream>
//#include<iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::string;

class User {
private:
    string name;
    string passwd;
    string email;
public:
    static int n;
    static void print_n() { cout <<"there are " <<n<<" users"; }
    User(string a, string  b = "111111", string c ={}) :name{a}, passwd{b}, email{c} { ++n; }
    void set_email() { 
        cout << "Enter email address:";
        cin >> email;
        cout << "email is set successfully..."<<endl;
    }
    void change_passwd() {
        string c;
        cout << "Enter old password:";
        cin >> c;
        for(auto i=1;i<=3;i++)
        {
            if (c != passwd&&i!=3)
            {
                cout << "password input error.Please re-enter again:";
                    cin >> c;
            }
            if (c != passwd && i == 3)
                cout << "password input error.Please try after a while."<<endl;
            if (c == passwd)
            {
                cout << "Enter new passwd:";
                cin >> passwd;
                cout << "new passwd is settd successfully..." << endl;
                break;
            }
        }
    }
        void print_info() {

        cout << "name:   "<< name << endl;
        cout << "passwd: ";
        for (auto i = 1; i <= passwd.size(); i++)
            cout << "*";
            cout << endl;
        cout << "email:  " <<  email << endl;
    }
};
int User::n = 0;

.cpp

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

 

标签:std,cout,对象,imag,Complex,实验,using,include
From: https://www.cnblogs.com/aomijia/p/16804489.html

相关文章

  • 实验二
      #include<iostream>#include<complex>intmain(){usingnamespacestd;complex<double>c1{3,4},c2{4.5};constcomplex<double>c3{c2};......
  • 实验一
    task1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;}task1_1#include<stdio.h>intma......
  • 实验1
    1.#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;}2.#include<stdio.h>#include<math.h>intmain......
  • 实验一
    task1——1#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");printf("O\n");printf("<H>......
  • JavaScript学习--Array数组对象
    定义1.var变量名=newArray(元素列表);如vararr=newArray(1,2,3);2.常用:var变量名=[元素列表];如vararr=[1,2,3];访问arr[索引]=值;如arr[0]=1;ps:数组长度类型均可变 len......
  • 实验1
    #include<stdio.h>#include<math.h>intmain(){doublex,ans;scanf_s("%lf",&x);ans=pow(x,365);printf("%.2f的365次方:%.2f\n",x,ans);......
  • 实验1 C语言开发环境使用和数据类型、运算符、表达式
    Task1.c#include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;}Task1_1.c#include<stdio.h>intmai......
  • Python实验报告
                                                         ......
  • Python类对象的创建和使用
    通过前面章节的学习,我们已经学会如何定义一个类,但要想使用它,必须创建该类的对象。创建类对象的过程,又称为类的实例化。Python类的实例化对已定义好的类进行实例化,其......
  • 域对象共享数据
    1、使用ServletAPI向request域对象共享数据@RequestMapping("/testServletAPI")publicStringtestServletAPI(HttpServletRequestrequest){request.setAttribute("te......