C++学习记录(五)this指针,常对象常函数,静态对象静态函数,友元对象友元函数,运算符重载

这是第五天的记录。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class Student
 6 {
 7 private:
 8     string name;
 9     int age;
10     static int amount;
11 public:
12     Student() {}
13     ~Student() {}
14 
15     Student(string name, int age)        // this指针的用法一
16     {
17         this->name = name;
18         this->age = age;
19     }
20     Student& setName(string name)        // this指针的第二种用法,返回对象
21     {
22         this->name = name;
23         return *this;
24     }
25 
26     void setAge(int age, Student* student)
27     {
28         student->age = age;                // 函数隐藏有this指针(即student)
29     }
30     void getAge()
31     {
32         cout << this->age << endl;
33     }
34 
35     void showInfo()
36     {
37         cout << "Student name is: " << this->name << ", age is: " << this->age << endl;
38     }
39     void showThis()
40     {
41         cout << "This pointer address is: " << this << endl;
42     }
43 
44     static int getAmount()
45     {
46         return amount;                      // 静态函数只能访问静态成员
47     }
48 };
49 
50 int Student::amount = 100;
51 
52 int main()
53 {
54     Student s1("Zhangsan", 18);
55     Student s2 = s1.setName("Lisi");
56     s2.showInfo();
57 
58     cout << "-------------" << endl;
59     cout << sizeof (Student) << endl;       // this指针不占用类大小和类对象的大小
60 
61     s2.showThis();
62     cout << "-------------" << endl;        // this保存本对象地址
63     cout << &s2 << endl;
64 
65     // 类中静态函数没有this指针(无法访问类中属性),加载静态区时就已经存在,是先于对象产生的;
66     // 普通函数默认隐藏有this指针
67     s2.setAge(20,&s2);
68     s2.getAge();
69 
70     // 无需类对象存在,就可调用类中静态函数
71     // static修饰的函数,仅表示修饰作用域,仅限本源码使用
72     // 静态函数可以定义重名函数,且不能被其他文件调用
73     cout << "-------------" << endl;
74     cout << "Amount student: " << Student::getAmount() << endl;
75 
76     //cout << "Hello World!" << endl;
77     return 0;
78 }
  1 #include <iostream>
  2 
  3 using namespace std;
  4 class Desk
  5 {
  6 
  7 public:
  8     Desk() { cout << "Desk construction " << endl; }
  9     ~Desk() { cout << "Desk deconstruction " << endl; }
 10 };
 11 
 12 class Chair
 13 {
 14 public:
 15     Chair() { cout << "Chair construction " << endl; }
 16     ~Chair() { cout << "Chair deconstruction " << endl; }
 17 };
 18 
 19 class Room
 20 {
 21 private:
 22     //Desk _desk[3];                 // 对象数组
 23     //Chair _chair[3];
 24 
 25     Desk _desk1;
 26     Chair _chair1;
 27     //const string name;
 28     //const Computer _computer;
 29 
 30     string name;
 31     int size;
 32     static int amount;
 33 public:
 34     //Room():name("office213")
 35     Room()
 36     {
 37         cout << "Room construction " << endl;
 38     }
 39     Room(string name, int size)
 40     {
 41         this->name = name;
 42         this->size = size;
 43     }
 44     ~Room() { cout << "Room deconstruction " << endl; }
 45 
 46     string getName() const                  // 常函数
 47     {
 48         return this->name;
 49     }
 50     const Room& setName(string name)        // 常对象
 51     {
 52         this->name = name;
 53         return *this;
 54     }
 55 
 56     void showInfo()
 57     {
 58         cout << "Room information, name: " << this->name << ", size: " << this->size << endl;
 59     }
 60     static int getAmount()                  // 静态成员或函数,属于进程的,不可以访问非静态成员
 61     {                                       // 它是所以对象共享
 62         return amount;
 63     }
 64 private:
 65     void state()
 66     {
 67         cout << "It has been used. Size is "<< this->size << endl;
 68     }
 69     // 友元函数可以不受访问权限的约束,可以直接访问类中的所有属性
 70     friend void checkState(Room& _room);
 71     // 友元类
 72     friend class Space;
 73 };
 74 
 75 int Room::amount = 4;
 76 
 77 void checkState(Room& _room)
 78 {
 79     _room.state();
 80 }
 81 
 82 class Space
 83 {
 84 private:
 85     string name;
 86     int size;
 87 public:
 88     Space();
 89     Space(string name, int size)
 90     {
 91         this->name = name;
 92         this->size = size;
 93     }
 94     void checkPosition(Room& _room, string ok)
 95     {
 96         cout << _room.name << " is belong to " << this->name << ok << endl;
 97     }
 98 };
 99 
100 int main()
101 {
102     // 当本类生成时,先构造类中的对象(按顺序),在构造本对象,先析构本对象,再析构类中的对象(逆顺序)
103     Room r1;
104 
105     // 常对象,const修饰的对象(只读),用于保护数据不被修改
106     cout << "Room name is " << r1.getName() << endl;
107 
108     // 常对象只能调用常函数,常函数可以读取类中成员,不能修改
109     cout << r1.setName("classroom001").getName() << endl;;
110     //cout << "Room name is " << r1.getName() << endl;
111 
112     // 静态成员与静态函数
113     Room r2("classroom002", 80);
114     Room r3("classroom003", 90);
115     cout << "Amount of classrooom002 is: " << r2.getAmount() << endl;
116     cout << "Amount of classrooom003 is: " << r3.getAmount() << endl;
117 
118     // 友元,友元函数,友元类
119     // 友元函数:类中任意位置申明,即可申明某个全局函数
120     // 友元不能传递
121     checkState(r3);
122     Space s1("Space", 200);
123     s1.checkPosition(r3,"No");
124 
125     //cout << "Hello World!" << endl;
126     return 0;
127 }
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class A
 6 {
 7 private:
 8     int number;
 9 public:
10     A() = default;
11     A(int number) { this->number = number; }
12     void setNumber(int num)
13     {
14         this->number = num;
15     }
16     int getNumber()
17     {
18         return this->number;
19     }
20     A& operator=(A& a)                  // 编译器提供的
21     {
22         this->number = a.number;
23         cout << "This is the of operator of equal of the class A. " << endl;
24         return *this;
25     }
26     A& operator+(A& a)
27     {
28         this->number += a.number;
29         return *this;
30     }
31     A& operator-(A& a)
32     {
33         this->number -= a.number;
34         return *this;
35     }
36     A& operator*(A& a)
37     {
38         this->number *= a.number;
39         return *this;
40     }
41     A& operator/(A& a)
42     {
43         //cout << "first " << this->getNumber() << " Scend " << a.getNumber() << endl;
44         //cout << this->number / a.number << endl;;
45         this->number /= a.number;
46         return *this;
47     }
48 };
49 int main()
50 {
51     A a1;
52     a1.setNumber(1);
53     A a2;
54     a2.setNumber(2);
55 
56     a1 = a2;
57     cout << a1.getNumber() << endl;
58 
59     A a3(4);
60     A a4(2);
61     //cout << (a3 + a4).getNumber() << endl;
62     //cout << (a3 - a4).getNumber() << endl;
63     //cout << (a3 * a4).getNumber() << endl;
64     cout << (a3 / a4).getNumber() << endl;
65 
66     //cout << "Hello World!" << endl;
67     return 0;
68 }

 

posted @ 2022-05-08 17:03  Z_He  阅读(29)  评论(0编辑  收藏  举报