你知道什麼最可怕麼?就是明明是因為失望才離開,他卻以為是你不夠喜歡。。。
---- 網易雲熱評
一、類定義
#include <iostream> using namespace std; //實作一個表示學生的類 //struct Student{ class Student{ public: //行為:成員函數 void eat(const string& food){ cout << "我在吃" << food << endl; } void sleep(int hour){ cout << "我睡了" << hour << "小時" << endl; } void learn(const string& course){ cout << "我在學" << course << endl; } void who(void){ cout << "我叫" << m_name << ",今年" << m_age << "歲,學号是" << m_no << endl; } public: /* 私有成員不能在類的外部直接通路,但是可以 * 提供公有的成員函數來間接通路,在函數中可 * 以對非法資料加以限定,控制業務邏輯的合理 * 性----封裝思想*/ void setName(const string& name){ if(name == "二"){ cout << "你才二" << endl; } else{ m_name = name; } } void setAge(int age){ if(age < 0){ cout << "無效年齡" << endl; } else{ m_age = age; } } void setNo(int no){ if(no < 10000){ cout << "無效學号" << endl; } else{ m_no = no; } } private: //屬性:成員變量 string m_name; int m_age; int m_no; }; int main(void) { //原來:定義結構體類型變量 //現在:建立對象 或 執行個體化對象 Student s; /*s.m_name = "張飛"; s.m_name = "二"; s.m_age = 28; s.m_no = 10086;*/ s.setName("張翼德"); s.setName("二"); s.setAge(29); s.setAge(-2); s.setNo(10011); s.setNo(290); s.who(); s.eat("拉州拉面"); s.sleep(8); s.learn("C++程式設計"); return 0; }
二、構造函數,主要負責對象的初始化(初始化成員變量)
#include <iostream> using namespace std; class Student{ public: Student(const string &name,int age,int no){ cout << "構造函數" << endl; m_name = name; m_age = age; m_no = no; } void who(void){ cout << "我叫" << m_name << ",今年" << m_age << "歲,學号是" << m_no << endl; } private: string m_name; int m_age; int m_no; }; int main(void) { //建立對象/執行個體化對象/構造對象 //(...):指定構造函數需要的實參 Student s("張飛",28,10011); s.who(); //構造函數不能顯式調用 //s.Student("張三",29,10012); return 0; }
#include <iostream> using namespace std; class A{ public: A(void){ cout << "A的無參構造函數" << endl; m_i = 0; } A(int i){ cout << "A的有參構造函數" << endl; m_i = i; } int m_i; }; class B{ public: int m_j;//基本類型成員變量 A m_a;//成員子對象 }; int main(void) { B b; cout << "基本類型:" << b.m_j << endl;//? cout << "類類型:" << b.m_a.m_i << endl;//0 return 0; }
#include <iostream> using namespace std; class A{ public: A(int data = 0){ cout << "A(int=0)" << endl; m_data = data; } //拷貝構造函數 /*A(const A& that){ cout << "A(const A&)" << endl; m_data = that.m_data; }*/ int m_data; }; int main(void) { const A a1(100); //A a2(a1);//拷貝構造 A a2 = a1;//和上面寫法等價 cout << a1.m_data << endl;//100 cout << a2.m_data << endl;//100 return 0; }
#include <iostream> using namespace std; class Student{ public: //先定義成員變量,再賦初值 /* Student(const string&name,int age,int no){ cout << "構造函數" << endl; m_name = name; m_age = age; m_no = no; }*/ //定義成員變量同時初始化 Student(const string&name,int age,int no) :m_name(name),m_age(age),m_no(no){} void who(void){ cout << "我叫" << m_name << ",今年" << m_age << "歲,學号是" << m_no << endl; } private: string m_name; int m_age; int m_no; }; int main(void) { //建立對象/執行個體化對象/構造對象 //(...):指定構造函數需要的實參 Student s("張飛",28,10011); s.who(); //構造函數不能顯式調用 //s.Student("張三",29,10012); return 0; }
三、對象
#include <iostream> using namespace std; class Student{ public: Student(const string&name,int age,int no){ cout << "構造函數" << endl; m_name = name; m_age = age; m_no = no; } void who(void){ cout << "我叫" << m_name << ",今年" << m_age << "歲,學号是" << m_no << endl; } private: string m_name; int m_age; int m_no; }; int main(void) { //建立對象/執行個體化對象/構造對象 //(...):指定構造函數需要的實參 //Student s("張飛",28,10011); Student s = Student("張飛",28,10011); s.who(); //在棧區建立多個對象 Student sarr[3] = { Student("貂蟬",20,10012), Student("小喬",22,10013), Student("孫尚香",25,10014)}; sarr[0].who(); sarr[1].who(); sarr[2].who(); //在堆區建立/銷毀單個對象 Student* ps = new Student("扈三娘",30,10015); ps->who();//(*ps).who() delete ps; ps = NULL; //在堆區建立/銷毀多個對象 Student* parr = new Student[3]{ Student("孫二娘",35,10016), Student("白骨精",40,10017), Student("潘金蓮",36,10018)}; parr[0].who();//(*(parr+0)).who() parr[1].who();//(*(parr+1)).who() parr[2].who(); delete[] parr; parr = NULL; return 0; }
歡迎關注公衆号:順便編點程