C++学习记录(二)引用,动态开辟空间,类,get与set方法,构造函数

这是第二天学习的代码。

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 // 引用
  6 int add(int& a, int& b)
  7 {
  8     return a+b;
  9 }
 10 void swap(int& a, int& b)
 11 {
 12     int c = a;
 13     a = b;
 14     b = c;
 15 }
 16 
 17 // 动态开辟空间
 18 void test1()                        // C中动态开辟空间
 19 {
 20     int* pa = (int*)malloc(sizeof(int));
 21     cout << *pa << endl;            // 随机值
 22 
 23     int* pb = (int*)calloc(1, sizeof(int));
 24     cout << *pb << endl;            // 0
 25 
 26     free(pa);
 27     free(pb);
 28     pa = NULL;
 29     pb = NULL;
 30 }
 31 void test2()                         // C++中动态开辟空间
 32 {
 33     int* pa = new int;
 34     cout << *pa << endl;            // 对应malloc
 35 
 36     int* pb = new int(100);
 37     cout << *pb << endl;
 38 
 39     int* pc = new int[4];
 40     pc[0] = 200;
 41     pc[1] = 300;
 42     cout << *pc << endl;
 43     cout << *(pc+1) << endl;
 44 
 45     delete pa;
 46     delete pb;
 47     delete []pc;
 48 }
 49 
 50 // class对象,struct数据
 51 class Person
 52 {
 53 private:
 54     string name;
 55     int age;
 56     char sex;
 57 
 58 public:
 59     Person() = default;
 60     Person(string _name, int _age, char _sex)
 61     {
 62         name = _name;
 63         age = _age;
 64         sex = _sex;
 65     }
 66 
 67     void work() { cout << " I'm working !" << endl; }
 68     void eating() { cout << " I'm eating !" << endl;}
 69     void setName(const string& _name) { name = _name; }
 70     string gatName() { return name; }
 71     void setAge(int _age) { age = _age; }
 72     void showInfo()
 73     {
 74         cout << "Person Infomation: " << name << ", Age: " << age << ", Sex: " << sex << "." << endl;
 75     }
 76 
 77 };
 78 
 79 //
 80 enum Sex
 81 {
 82     female,
 83     male
 84 };
 85 
 86 //#pragma pack(1)             // 取消字节对齐,即按1对齐
 87 class Student               // 第一个字母大写
 88 {
 89 private:
 90     string name;            // 32个字节
 91     int age;                // 4个字节
 92     Sex sex;                // 4个字节
 93     char ff;
 94 
 95 public:
 96     //Student() {};           // 默认构造函数,无参空构造
 97 
 98     // 默认有参构造,不能与默认构造函数重复出现,编译器提供优化后的
 99 
100     Student(string name = "", int age = 0)
101     {
102         this->name = name;
103         this->age = age;
104         cout << "I'm " << this->name << ", age " << this->age << endl;
105     }
106 
107     ~Student() {};
108 
109     void writeCode() { cout << "I'm writing code !" << endl; }
110     void eat_launch() { cout << "I'm eating launch !" << endl;}
111 
112     void setName(const string& name) { this->name = name; }
113     string getName() { return this->name; }
114 
115     void setAge(int age) { this->age = age; }
116     int getAge() { return this->age; }
117 
118     void setSex(Sex sex) { this->sex = sex; }
119     Sex getSex() { return this->sex; }
120 
121 };
122 //#pragma pack()
123 
124 // 设计一个平面类,属性:长,宽,行为:求面积
125 class Plane
126 {
127 private:
128     int length;
129     int width;
130 
131 public:
132     int area() const { return this->length * this->width; } // 主要常函数的写法
133     void setValue(int length = 1, int width = 1)            // 函数重载时不能设置默认值
134     {
135         this->length = length;
136         this->width = width;
137     }
138 };
139 
140 bool compaired(const Plane& p1, const Plane& p2)            // 常对象,只能访问不能修改,只能调用常函数,
141 {
142     return p1.area() > p2.area() ? true: false;
143 }
144 
145 //
146 
147 int main()
148 {
149     // -1- 引用(主要用在函数传参)
150     // 从编译器角度,就是指针;从语法角度,是const修饰的指针,调用时不可修改。
151     /*
152     int a = 100;
153     int* p = &a;                    // 指针p,变量a的地址
154     cout << *p << endl;
155     *p = 200;
156     cout << *p << endl;
157     cout << a << endl;
158 
159     //int* const b = &a;
160     int& b = a;                     // 引用必须初始化
161     int data1 = 300;
162     int data2 = 400;
163     swap(data1,data2);
164     cout << "data1 = " << data1 << endl;
165     cout << "data2 = " << data2 << endl;
166     */
167 
168     // -2- 动态开辟空间
169     // C++中用new开辟,delete释放
170     /*
171     test1();
172     test2();
173     */
174 
175     // 类与对象抽象与封装
176     /*
177     Person p1;
178     p1.work();
179 
180     Person* p2 = new Person;
181 
182     Person p3("zhangsan",18,'M');
183     p3.showInfo();
184     p3.setAge(20);
185     p3.setName("Lisi");
186     p3.showInfo();
187     */
188 
189     // -3- 类对象与get,set方法
190     /*
191     Student stu1;
192     cout << sizeof (stu1) << endl;                  // 48,取消字节对齐后变为41
193     //cout << sizeof (Student) << endl;               // 48,取消字节对齐后变为41
194     // 1 求出类中所占内存最大的,以这个内存空间大小为基准,进行内存字节对齐。
195     // 2 结构体与数值是连续存储的,也要求每个元素中最大空间的那一个,为基准。
196 
197     Student zhangsan;
198     zhangsan.setName("zhangsan");
199     zhangsan.setAge(18);
200     zhangsan.setSex(Sex::male);
201     cout << zhangsan.getName() << "\t" << zhangsan.getAge() << "\t" <<endl;
202     switch ( zhangsan.getSex() ){
203         case Sex::male:
204             cout << "Boy." << endl;
205             break;
206         case Sex::female:
207             cout << "girl." << endl;
208             break;
209     }
210     */
211 
212     // -4- 类设计与常对象
213     /*
214     Plane p1;
215     p1.setValue(2,3);
216     cout << p1.area() << endl;
217 
218     Plane p2;
219     p2.setValue(3,4);
220     cout << p2.area() << endl;
221 
222     if (compaired(p1,p2))
223     {
224         cout << " P1 is the larger one. " << endl;
225     }
226     else
227     {
228         cout << " P2 is the larger one. " << endl;
229     }
230     */
231 
232     // -5- 构造函数
233     // 构造函数:初始化类中的属性;
234     // 当类对象在内存中被定义时,会自动调用对应的构造函数;
235     // 默认构造函数会根据调用方式不同而不同,在栈区定义无参对象(不带括号),会调用无参构造函数,
236     //                                在堆区定义无参对象(指针),会调用有参且为默认值的构造函数。
237     //Student stu3;                       // 隐式构造,会调用无参构造,不能加括号
238     //Student* stu4 = new Student;        // 这是在堆区的隐式调用,在调用无参构造时,可以加括号也可不加,前提手写无参构造函数
239 
240     //Student stu5("zhangsan",18);          // 显示调用
241     Student* stu6 = new Student();          // 在堆区的显示调用
242 
243 
244     cout << "Hello World!" << endl;
245     return 0;
246 }

 

posted @ 2022-05-05 21:59  Z_He  阅读(107)  评论(0编辑  收藏  举报