其他分享
首页 > 其他分享> > 实验1 类与对象

实验1 类与对象

作者:互联网

T1:

Complex.hpp

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 class Complex {
 5 public:
 6     Complex() = default;
 7     Complex(double r, double i) :real(r), imag(i) {}
 8     Complex(double r) :real(r),imag(0) {}
 9     Complex(const Complex& c) :real(c.real), imag(c.imag) {}
10     double get_real() const { return real; }
11     double get_imag() const { return imag; }
12     void show() const;
13     void add(const Complex c);
14     friend Complex add(const Complex c1, const Complex c2);
15     friend bool is_equal(const Complex c1, const Complex c2);
16     friend double abs(const Complex c);
17 private:
18     double real=0, imag=0;
19 
20 };
21 void Complex::show() const{
22     if (imag > 0)
23         cout << real << '+' << imag << 'i' << endl;
24     else if (imag == 0)
25         cout << real << endl;
26     else
27         cout << real << imag <<'i'<< endl;
28 }
29 void Complex::add(const Complex c) {
30     real += c.real;
31     imag += c.imag;
32 }
33 Complex add(Complex c1, const Complex c2) {
34     c1.add(c2);
35     return c1;
36 
37 }
38 bool is_equal(const Complex c1, const Complex c2) {
39     if (c1.real == c2.real && c1.imag == c2.imag)
40         return true;
41     else
42         return false;
43 }
44 double abs(const Complex c) {
45     double sqr;
46     sqr = sqrt(c.real * c.real + c.imag * c.imag);
47     return sqr;
48 }

Complextest.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(6.3);
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 }

 T2:

User.hpp

 1 #include<iostream>
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 class User {
 7 public:
 8     User(string namel,string passwdl="111111",string emaill=""):name(namel),passwd(passwdl),email(emaill){}
 9     void set_email();
10     void change_passwd();
11     void print_info();
12     static void print_n();
13 private:
14     string name, passwd, email;
15     static int n;
16 };
17 void User::set_email() {
18     cout << "Enter email address:";
19     cin >> email;
20     cout << endl << "Email has been set successfully!";
21 }
22 void User::change_passwd() {
23     string oldpw;
24     int i=0;
25     cout << "Please enter the old password:";
26 
27     while (i < 3) {
28         cin >> oldpw;
29         cout << endl;
30         if (passwd == oldpw)
31             break;
32         else {
33             cout << "Password input error.";
34             i++;
35         }
36         if (i < 3)
37             cout << "Please enter again :";
38         else
39             cout << "Please retry later." << endl;
40     }
41     if (i < 3) {
42         cout << "Enter new password:";
43         cin >> passwd;
44         cout << '\n' << "New password has been set successfully!" << endl;
45     }
46     else
47         return;
48 }
49 void User::print_info() {
50     cout << endl << "name:" << name << endl;
51     cout << "passwd:******" << endl;
52     cout << "email:" << email << endl;
53     n++;
54 }
55 int User::n = 0;
56 void User::print_n() {
57     cout << "There are " << n << " users." << endl;
58 }

User.cpp

#include "User.hpp"
#include <iostream>

int main()
{
    using namespace std;

    cout << "testing 1......" << endl;
    User user1("Jonny", "92197", "xyz@hotmail.com");
    user1.print_info();

    cout << endl
        << "testing 2......" << endl
        << endl;
    User user2("Leonard");
    user2.change_passwd();
    user2.set_email();
    user2.print_info();

    User::print_n();
}

 Password error

 Password correct

 

Conclusion

1.本次实验将头文件以 .hpp 形式单独存放,作为自定义头文件,使代码更加简洁易于修改。

2.本实验需要注意的点在于:const对象只能调用const成员函数,static函数应尽量只调用static变量避免冗余。

本实验作为本学期面向对象的程序设计课程的第一个实验,难度较易,易于上手,知识点包含全面,可以很好的巩固第四第五章基础知识,作为入门实验是十分适宜的。

 

标签:const,cout,对象,void,Complex,实验,include,imag
来源: https://www.cnblogs.com/finimpyq-blog/p/15470013.html