实验一 类与对象

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

 

 

 1 #ifndef USER_HPP
 2 #define USER_HPP
 3 
 4 #include <iostream>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 class User{
10 public:
11     User(string n_name);
12     User(string n_name,string p_passwd,string e_email);
13     void set_email();
14     void change_passwd();
15     void print_info();
16     static void print_n();
17 private:
18     string name,passwd,email;
19     static int n;
20 };
21 
22 int User::n=0;
23 
24 User::User(string n_name):name{n_name},passwd{"111111"},email{""}
25 {
26     n++;
27 }
28 User::User(string n_name,string p_passwd,string e_email)
29 {
30     name=n_name;
31     passwd=p_passwd;
32     email=e_email;
33     n++;
34 }
35 
36 void User::set_email()
37 {
38     string e_email;
39     cout<<"Enter email address:";
40     cin>>e_email;
41     email=e_email;
42     cout<<"Your email is set successfully..."<<endl;
43 }
44 
45 void User::change_passwd()
46 {
47     string old_passwd,new_passwd;
48     int i=1;
49     cout<<"Enter old password:";
50     cin>>old_passwd;
51     while(i<=3)
52     {
53         if(old_passwd!=passwd)   //判断输入是否正确
54         {
55             cout<<"password input error.Please re-enter again: ";
56             cin>>old_passwd;
57             i++;
58             if(i==4)
59                 break;
60         }
61         else            //形成新密码并跳出循环
62             break;
63     }
64 
65     if(i<=3)
66     {
67         cout<<"Enter new password:";
68         cin>>new_passwd;
69         cout<<"new password is set successfully..."<<endl;
70     }
71     else
72         cout<<"password input error.Please try after a while."<<endl;
73 }
74 
75 void User::print_info()
76 {
77     cout<<"name:\t"<<name<<endl;
78     cout<<"passwd:\t"<<"******"<<endl;
79     cout<<"email\t"<<email<<endl;
80 }
81 
82 void User::print_n()
83 {
84     cout<<"there are "<<n<<" users"<<endl;
85 }
86 #endif // USER_HPP

 

 

 

总结:

一.类的定义

类的定义通常有两种方式:

将类的声明和定义都放在类体中
●将类的声明放在.h头文件中,定义放在.hpp文件中

二.类的成员

●public成员在类外可以直接访问
●protected和private成员在类外不能够直接访问
●作用域从该访问限定符出现的位置开始直到下一个访问限定符出现截止
●class的默认访问权限是private,而struct默认访问权限是public

三.类的成员的访问

●使用对象包括访问对象的数据成员和调用成员函数。

●类中的成员函数可以使用自身不同性质的数据成员和调用成员函数。

●对象成员的访问形式与访问结构形式相同,运算符‘.’和‘->’用于访问对象成员。

四.构造函数

●用于创建对象的特殊成员函数。当创建对象时,系统自动调用构造函数。

●作用:为对象分配空间;对数据成员赋初值;请求其他资源。

●函数名与类名相同;可以有任意类型的参数,但不能有返回类型。

●原型:类名::类名(参数表);

                   类名::~类名();

 ●构造函数和析构函数不应该被定义在私有部分。

五.析构函数

●类名::类名(const类名&引用名,…)

posted @ 2021-10-24 23:38  斍奭藇簨  阅读(48)  评论(3编辑  收藏  举报