1. 程式人生 > 其它 >C++學習記錄(二)引用,動態開闢空間,類,get與set方法,建構函式

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 }