C++实验一类与对象

 第三题

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

 

 1 #include"c.hpp"
 2 #include <iostream>
 3 #include<iomanip>
 4 int main()
 5 {
 6     using namespace std;
 7 
 8     my_complex c1(3, 4);
 9     const my_complex c2(5.5);
10     my_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     my_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     return 0;
43 }

 

 

 

 

 

 

第四题

 1 #ifndef USER_HPP
 2 #define USER_HPP
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 class User {
 7 public:
 8     User(string name, string passwd = "111111", string email = "") :name(name), passwd(passwd), email(email) { n++; }
 9     void set_email();
10     void change_passwd();
11     void print_info()const;
12     static void print_n();
13     bool jianceqi(const string tmp)const;
14     ~User()=default;
15 private:
16     string  name;
17     string passwd;
18     string email;
19     static int  n;
20 };
21 
22 int User::n = 0;
23 void User::set_email() {
24     cout << "\nEnter email adress: ";
25     cin >> email;
26     if (!jianceqi(email)) {
27         return;
28     }
29     cout << "email is set successfully...";
30 }
31 void User::change_passwd() {
32     cout << "\nEnter the old pasword: ";
33     string tmp;
34     int i = 0;
35     cin >> tmp;
36     for ( i; i < 2; i++)
37     {
38         if (tmp.compare(passwd) != 0) {
39             cout << "password input error.please re-enter again: ";
40             cin >> tmp;
41         }
42         else break;
43     }
44     if (i < 2) { 
45         cout << "enter new password: ";
46             cin >> passwd;
47         cout << "new passwd is set successfully..."; 
48     }
49     else cout << "password input error. please try after a while.";
50 }
51 void User::print_info()const {
52     cout << "\nname: " << name << "\npasswd: ******" << "\nemail: " << email<<endl;
53 }
54 void User::print_n() {
55     cout << "There are "<<n<<"users";
56 }
57 bool User::jianceqi(const string tmp)const {
58     int found = 0,found2;
59     if ((found = tmp.find('@')) != -1|| (( found2 = tmp.find(".com"))!=-1)) {
60         if (found == -1) {
61             cout << "wrong input.\n";
62             return false;
63         }
64         return true;
65     }
66     return false;
67 }
68 #endif

 

 1 #include "User.hpp"
 2 #include <iostream>
 3 #include<string>
 4 int main()
 5 {
 6     using namespace std;
 7    
 8     cout << "testing 1......" ;
 9     User user1("Jonny", "114514", "xianbei@hotmail.com");
10     user1.print_info();
11 
12     cout << endl
13         << "testing 2......" << endl
14         ;
15     User user2("Belly");
16     user2.change_passwd();
17     user2.set_email();
18     user2.print_info();
19 
20     User::print_n();
21 }

 

 

 

 

 

实验总结:
1.定义类并实现类函数时要注意不要重定义

2.static 类型的静态变量要在类内是引用性声明必须要在文件作用域的某个地方使用类名限定进行定义型声明。(常量表达式类型修饰的静态常量可在类内初始化,此时在类外只可声明,不可二次初始化)

3静态成员函数可以访问对象的非静态成员,但必须通过对象名,不推荐

4注意友元的性质(单向,不可传递性等)

5string下的find函数可以找到要找的字符下标位置,否则返回-1find(const string &p,'字符')    还有更多重载函数。。。

posted on 2021-10-21 19:29  wlj23  阅读(62)  评论(3)    收藏  举报