程式設計與演算法(三)C++面向物件程式設計 第二週 相關筆記
阿新 • • 發佈:2021-10-07
1、
成員函式可寫在類的外面
#include<bits/stdc++.h> using namespace std; class student{ public: int grade; string name; void init_(string _name,int _grade){ name = _name; grade = _grade; } void get_name(){ cout<<name<<endl; }void get_grade(){ cout<<grade<<endl; } void together(); }; void student::together(){ cout<<name<<" "<<grade<<endl; } int main(){ student stu; stu.init_("Bruce",99); stu.get_grade(); stu.together();return 0; }
2、
類的成員
有pubilc,private,protected 沒有次序,預設private
3、
成員函式同樣可以過載
4、建構函式
(1)必須public 與類同名 類生成就進行
View Code(2)過載
#include<bits/stdc++.h> using namespace std; class test{ public: test(int x){ cout<<"first called"<<endl; } test(){ coutView Code<<"second called"<<endl; } }; int main(){ test t1; cout<<endl; test t2[2] = {1,2}; cout<<endl; test t3[2] = {1}; cout<<endl; test * t4= new test[3]; delete []t4; return 0; }
#include<bits/stdc++.h> using namespace std; class test{ public: test(int x,int y){ cout<<"first called"<<endl; } test(int x){ cout<<"second called"<<endl; } test(){ cout<<"third called"<<endl; } }; int main(){ test t1; cout<<endl; test t2[2] = {1,test(1,2)}; // ! cout<<endl; test t3[2] = {1}; cout<<endl; test * t4= new test[3]; cout<<endl; test * t5[3] = { new test(),new test(1),new test(1,2) }; delete []t4; return 0; }View Code
5、
複製建構函式的三種使用方法
(1)初始化時
#include<bits/stdc++.h> using namespace std; class student{ public: int grade; string name; student (){ ; } student (student &a); void get_name(){ cout<<name<<endl; } void get_grade(){ cout<<grade<<endl; } void together(); }; void student::together(){ cout<<name<<" "<<grade<<endl; } student::student(student &a){ // 建構函式 name = a.name; grade = a.grade; cout<<"Copy constructor run!"<<endl; } int main(){ student stu1; stu1.name = "Bruce"; stu1.grade = 99; student stu2(stu1); return 0; }View Code
(2)作為形參時
#include<bits/stdc++.h> using namespace std; class student{ public: int grade; string name; student (){ ; } student (student &a); void get_name(){ cout<<name<<endl; } void get_grade(){ cout<<grade<<endl; } void together(); }; void student::together(){ cout<<name<<" "<<grade<<endl; } student::student(student &a){ // 建構函式 name = a.name; grade = a.grade; cout<<"!!!"<<endl; } void func(student b){ return ; } int main(){ student stu1; stu1.name = "Bruce"; stu1.grade = 99; student stu2(stu1); func(stu2); return 0; }View Code
(3)作為函式返回值
pass;
賦值時不用到建構函式 如 a = b
呼叫建構函式耗時間,可以用引用,怕更改原來被引用的還可以const &A
6、
型別轉換建構函式
一個引數
#include<bits/stdc++.h> using namespace std; class Complex{ double real,imag; public: // 型別轉換建構函式 Complex(double i){ real = i; imag = 0; } void output(){ cout<<real<<" + "<<imag<<" i"<<endl; } }; int main(){ Complex c = 1; c.output(); return 0; }View Code
7、
解構函式 == 消亡函式
#include<bits/stdc++.h> using namespace std; class test{ char *p; public: test(){ p = new char [10]; } ~test(){ cout<<"died!"<<endl; delete [] p; } }; int main(){ test t[2]; cout<<"End Main"<<endl; return 0; } End Main died! died!View Code
#include<bits/stdc++.h> using namespace std; class test{ char *p; public: test(){ p = new char [10]; } ~test(){ cout<<"died!"<<endl; delete [] p; } }; int main(){ test * p = new test [2]; cout<<"~~~"<<endl; delete []p; cout<<"~~~"<<endl; return 0; } ~~~ died! died! ~~~View Code
在生命週期結束時,執行解構函式