实验1 类与对象

实验任务3

1、程序源码

  • Complex.hpp
 1 #ifndef COMPLEX_HPP
 2 #define COMPLEX_HPP
 3 
 4 #include<iostream>
 5 #include<cmath>
 6 using namespace std;
 7 
 8 class Complex{
 9 public:
10     Complex();
11     Complex(double real1, double imag1 = 0.0);
12     Complex(const Complex &c);
13     double get_real() const;
14     double get_imag() const;
15     void show() const;
16     void add(const Complex &c);
17 
18     friend Complex add(const Complex &c1, const Complex &c2);
19     friend bool is_equal(const Complex &c1, const Complex &c2);
20     friend double abs(const Complex &c);
21 
22 private:
23     double real;
24     double imag;
25 };
26 
27 Complex::Complex() {
28 
29 }
30 
31 Complex::Complex(double real1, double imag1): real{real1}, imag{imag1} {
32 
33 }
34 
35 Complex::Complex(const Complex &c): real{c.real}, imag{c.imag} {
36 
37 }
38 
39 double Complex::get_real() const {
40     return real;
41 }
42 
43 double Complex::get_imag() const {
44     return imag;
45 }
46 
47 void Complex::show() const {
48     if(imag > 0){
49         cout << real << " + " << imag << "i";
50     }else if(imag < 0){
51         cout << real << " - " << abs(imag) << "i";
52     }else{
53         cout << real;
54     }
55 }
56 
57 void Complex::add(const Complex &c) {
58     this->real += c.real;
59     this->imag += c.imag;
60 }
61 
62 Complex add(const Complex &c1, const Complex &c2) {
63     Complex c3;
64     c3.real = c1.get_real() + c2.get_real();
65     c3.imag = c1.get_imag() + c2.get_imag();
66     return c3;
67 }
68 
69 bool is_equal(const Complex &c1, const Complex &c2) {
70     if (c1.get_real() == c2.get_real() && c1.get_imag() == c2.get_imag()){
71         return true;
72     }
73     return false;
74 }
75 
76 double abs(const Complex &c) {
77     return sqrt(c.get_real() * c.get_real() + c.get_imag() * c.get_imag());
78 }
79 
80 #endif

 

  • task3.cpp
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     Complex c1(6, -8);
 9     const Complex c2(3.6);
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     system("pause");
44 }

 

2、运行测试结果截图

 实验任务4

1、程序源码

  • User.hpp

#ifndef USER_HPP
#define USER_HPP

#include<iostream>
#include<string>
using namespace std;

class User{
public:
    User(string name1,string passwd1="111111",string email1="");
    void set_email();
    void change_passwd();
    void print_info() const;
    static void print_n();

private:
    string name;
    string passwd;
    string email;
    static int count;
};

int User::count = 0;

User::User(string name1, string passwd1, string email1): name{name1}, passwd{passwd1}, email{email1} {
    count++;
}

void User::set_email() {
    string newemail;
    string s = "@";
    int n = 1;
    cout << "Enter email address: ";
    cin >> newemail;

    while(newemail.find(s) == string::npos && n < 3){
        cout << "email input error. Please re-enter again: ";
        cin >> newemail;
        n++;
    }

    if (newemail.find(s) != string::npos){
        email = newemail;
        cout << "email is set successfully..." << endl;
    }else{
        cout << "email input error. Please try after a while." << endl;
    }
}

void User::change_passwd() {
    string oldpasswd;
    int n = 1;
    cout << "Enter old password: ";
    cin >> oldpasswd;

    while (oldpasswd != passwd && n < 3){
        cout << "password input error. Please re-enter again: ";
        cin >> oldpasswd;
        n++;
    }

    if (oldpasswd != passwd)
        cout << "password input error. Please try after a while." << endl;
    else{
        cout << "Enter new passwd: ";
        cin >> passwd;
        cout << "new passwd is set successfully..." << endl;
    }
   
}

void User::print_info() const {
    cout << "name:\t" << name << endl;
    cout << "passwd:\t" << "******" << endl;
    cout << "email:\t" << email << endl;
}

void User::print_n() {
    cout << "there are " << count << " users." << endl;
}

#endif
 

 

  • 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("Hellen", "12345", "hellen@hotmail.com");
10     user1.print_info();
11 
12     cout << endl
13          << "testing 2......" << endl
14          << endl;
15     User user2("Jennie");
16     user2.change_passwd();
17     user2.set_email();
18     user2.print_info();
19 
20     User::print_n();
21     
22     system("pause");
23 }

 

2、运行测试结果截图

 

 

 

实验总结

函数既有定义又有声明时,不能在定义和声明中同时给定参数的默认值。

 

 

 

posted @ 2021-10-22 09:35  ╮君颜~ઇଓ  阅读(41)  评论(0编辑  收藏  举报