实验一 类与对象

发布时间:2022-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了实验一 类与对象脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

task3:

 1 #ifndef COMPLEX_HPP
 2 #define COMPLEX_HPP 
 3 #include<iostream>
 4 #include<cmath>
 5 using namespace std;
 6 class Complex
 7 {
 8 private:
 9     double real,image;
10 public:
11     Complex( double x,double y):real(x),image(y){};
12     Complex(){}
13     Complex(double x):real(x),image(0){}
14     Complex(Complex &c):real(c.real),image(c.image){}
15     
16     double get_real()const{return real;}
17     double get_image()const{return image;}
18     void show()const;
19     void add(Complex c){real+=c.real;image+=c.image;}
20     
21     ~Complex()=default;
22     
23     friend Complex add(const Complex &c1,const Complex &c2);
24     friend bool is_equal(const Complex &c1,const Complex &c2);
25     friend double abs(const Complex &c1);
26 };
27 void Complex:: show()const
28 {
29     if(image>0)
30     cout<<real<<"+"<<image<<"i"<<endl;
31     else if(image<0)
32     cout<<real<<image<<"i"<<endl;
33     else
34     cout<<real<<endl;
35 }
36 Complex add(const Complex &c1,const Complex &c2)
37 {
38     Complex c3;
39     c3.real=c1.real+c2.real;
40     c3.image=c1.image+c2.image;
41     return c3;
42 }
43 bool is_equal(const Complex &c1,const Complex &c2)
44 {
45     if(c1.real==c2.real&&c1.image==c2.image)
46     return true;
47     else 
48     return false;
49 }
50 double abs(const Complex &c1)
51 {
52     return sqrt(c1.real*c1.real+c1.image*c1.image);
53 }
54 #endif
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 int main()
 4 {
 5     using namespace std;
 6     Complex c1(5, -12);
 7     const Complex c2(2.5);
 8     Complex c3(c1);
 9     cout << "c1 = ";
10     c1.show();
11     cout << endl;
12     cout << "c2 = ";
13     c2.show();
14     cout << endl;
15     cout << "c2.imag = " << c2.get_image() << endl;
16     cout << "c3 = ";
17     c3.show();
18     cout << endl;
19     cout << "abs(c1) = ";
20     cout << abs(c1) << endl;
21     cout << boolalpha;
22     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
23     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
24     Complex c4;
25     c4 = add(c1, c2);
26     cout << "c4 = c1 + c2 = ";
27     c4.show();
28     cout << endl;
29     c1.add(c2);
30     cout << "c1 += c2, " << "c1 = ";
31     c1.show();
32     cout << endl;
33 }

注意const类型的使用 

实验一 类与对象

 

 

task4:

  1 #ifndef USER.HPP
  2 #define USER.HPP
  3 #include<iostream>
  4 #include<string>
  5 using namespace std; 
  6 int BF(string S,string T);
  7 class User
  8 {
  9 private:
 10     string name;
 11     string password;
 12     string email;
 13     static int count;
 14 public:
 15     User(string n1):name(n1),password("111111"),email(""){++count;}
 16     User(string n1,string p1,string e1):name(n1),password(p1),email(e1){++count;}
 17     void set_email(string e1);
 18     void set_email();   
 19     void change_passwd();
 20     void print_info()const;
 21     static void print_n();
 22     ~User(){--count;};
 23 };
 24 int User::count=0;
 25 void User::set_email()
 26 {
 27     string e1;
 28     cout<<"Enter email address: ";
 29     cin>>e1;
 30     int i,flag=1;
 31     while(flag)
 32     {
 33         for(i=0;e1[i]&&flag;i++)
 34         {
 35             if(e1[i]=='@')
 36             flag=0;
 37         }
 38         if(flag||!BF(e1,".com"))
 39         {
 40             cout<<"Invalid email address!Please enter again:";
 41             cin>>e1;
 42         }
 43     }
 44     email=e1;
 45     cout<<"Email is set successfully..."<<endl;
 46 }
 47 void User::change_passwd()
 48 {
 49     using namespace std;
 50     cout<<"Enter old password:";
 51     int i=1;
 52     while(i<4)
 53     {
 54         string p;
 55         cin>>p;
 56         if(p!=password)
 57         cout<<"Password input error.Please re-enter again:";
 58         else
 59         {
 60             cout<<"Enter new password: ";
 61             int flag=1;
 62             while(flag)
 63             {
 64                 int count=0;
 65                 cin>>p;
 66                 if(p.length()>=6)
 67                 {
 68                     for(int i=0;i<p.length();i++)
 69                     if(isdigit(p[i]))
 70                     count++;
 71                     if(count==p.length())
 72                     cout<<"Your password is too simple.Please re-enter again:";
 73                     else
 74                     flag=0;
 75                 }
 76                 else
 77                 cout<<"Your password is too short.Please re-enter again:";
 78             }
 79             password=p;
 80             cout<<"New password is set successfully..."<<endl;
 81             break;
 82         }
 83         i++;
 84     }
 85     if(i==4)
 86     cout<<"Please try after a while."<<endl;
 87 }
 88 void User:: print_info()const
 89 {
 90     using namespace std;
 91     cout<<"name:"<<name<<endl<<"password: ******"<<endl<<"email: "<<email<<endl;
 92 }
 93 void User::print_n()
 94 {
 95     if(count==0)
 96     cout<<"There is "<<count<<" user."<<endl;
 97     else
 98     cout<<"There are "<<count<<" users."<<endl;
 99 }
100 int BF(string S,string T)
101 {
102     int i=0,j=0;
103     while (i<S.length()&&j<T.length())
104     {
105         if(S[i]==T[j])
106         {
107             i++;
108             j++;
109         }
110         else
111         {
112             i=i-j+1;
113             j=0;
114         }
115     }
116     if(j==T.length())
117     return 1;
118     else
119     return 0;
120 }
121 #endif
 1 #include"User.hpp"
 2 #include<iostream>
 3 int main()
 4 {
 5     using namespace std;
 6     cout << "testing 1......" << endl;
 7     User user1("Tonny", "321321", "xxs@hotmail.com");
 8     user1.print_info();
 9     cout << endl<< "testing 2......" << endl;
10     User user2("Conner");
11     user2.change_passwd();
12     user2.set_email();
13     user2.print_info();
14     User::print_n();
15 }

注意string类型的使用,使用前要添加using namespace std;语句在宏定义后面。

实验一 类与对象

 

实验一 类与对象

 

脚本宝典总结

以上是脚本宝典为你收集整理的实验一 类与对象全部内容,希望文章能够帮你解决实验一 类与对象所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: