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(){ cout<<"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; }View Code
#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
在生命周期结束时,运行析构函数