C++的构造函数属性初始化_静态成员_this指针代码实例讲解
#includeusing namespace std;
//构造函数的属性初始化列表 /*class Teacher{ private: char* name; public: Teacher(char* name){ this->name = name; } char* getName(){ return this->name; } }; class Student{ private: char* name; //对象属性该如何赋值 Teacher t1; Teacher t2; public: Student(char* name, char* name_t1, char* name_t2) :t1(name_t1), t2(name_t2){ this->name = name; cout << "studnet构造函数" << endl; } void my_print(){ cout << this->name << "," << t1.getName() << "," << t2.getName() << endl; } }; void main(){ Student t("tom", "bo", "c"); t.my_print(); system("pause"); }*/
//C++ 通过new(delete)动态内存分配 //Cmalloc(free) /*class Teacher{ public: char* name; int age; public: Teacher(char* name, int age){ this->name = name; this->age = age; cout << "有参构造函数" << endl; } ~Teacher(){ cout << "析构函数" << endl; } }; void main(){ //c++ (new delete)动态内存分配 //对象 //分配内存,调用了构造函数 Teacher* t1 = new Teacher("tom", 20); //释放内存,调用了析构函数 delete t1; //数组 int* i2 = new int[10]; delete[] i2; //c 动态内存分配 //对象 Teacher* t3 = (Teacher*)malloc(sizeof(Teacher)); free(t3); //数组 int* t4 = (int*)malloc(sizeof(int)* 10); t4[2] = 6; free(t4); system("pause"); }*/
/*静态属性 方法 class Student{ public: char* name; //静态属性 static int total; public: Student(char* name){ this->name = name; cout << "构造函数" << endl; } ~Student(){ cout << "析构函数" << endl; } //非静态方法可以调用静态属性 void count(){ total++; cout << total << endl; } //静态方法中只能调用静态属性 static void count2(){ total++; cout << total << endl; } }; //给静态属性赋值 int Student::total = 6; void main(){ //通过 类名::静态属性名 方法调用类中的静态属性 //cout << Student::total << endl; //通过 类名::静态方法() 调用静态方法 Student::count2(); //Student t1("jack"); //t1.count(); system("pause"); }*/
//类的大小 /*class A{ public: int i; int j; int k; static int m; }; class B{ public: int i; int j; int k; void myprintf(){ cout << "打印" << endl; } }; void main(){ cout << sizeof(A) << endl;//12 cout << sizeof(B) << endl;//12 //可以看到静态属性和非静态属性不在同一内存 system("pause"); }*/
/* //常函数,当前对象不能被修改,防止数据成员被非法访问 class Student{ public: char* name; public: Student(char* name){ this->name = name; cout << "构造函数" << endl; } //常函数 //既不能改变指针的值,又不能改变指针指向的内容 //const Teacher* const this void print() const{ //下面两句会报错 //this->name = "tom"; //this = (Student*)0x0011; printf("%#x", this); cout << this->name << "," << endl; } void print2(){ printf("%#x", this); cout << this->name << "," << endl; } }; void main(){ Student t1("tom"); const Student t2("jack"); //常量对象只能调用常量方法 t2.print(); //下面这句会报错 //t2.print2(); //非常量对象既可以调用常量方法,也可以调用非常量方法 t1.print(); t1.print2(); system("pause"); }*/
/* class A{ //友元函数 friend void change(A* a, int b); private: int i; public: A(int i){ this->i = i; } void print(){ cout << i << endl; } }; //友元函数的实现,在友元函数中可以访问私有的属性 void change(A* a, int b){ a->i = b; } void main(){ A a(5); a.print(); change(&a, 10); a.print(); system("pause"); }*/
//友元类 /* class A{ //声明友元类 友元类可以访问A类中的所有属性 friend class B; private: int i; public: A(int i){ this->i = i; } void print(){ cout << i << endl; } }; class B{ private: A a; public: void change(){ a.i = 20; } }; */ //运算符重载 /* class Point{ public: int x; int y; public: Point(int x = 0, int y = 0){ this->x = x; this -> y = y; } void print(){ cout << x << "," << y << endl; } };
//运算符重载如果放在类外面,则需要两个参数 //这里使用的是引用,不是指针,要比指针方便 Point operator+(Point &p1, Point &p2){ Point tmp(p1.x + p2.x, p1.y + p2.y); return tmp; } Point operator-(Point &p1, Point &p2){ Point tmp(p1.x - p2.x, p1.y - p2.y); return tmp; } void main(){ Point p1(3, 4); Point p2(1, 2); Point p3=p1 - p2; p3.print(); system("pause"); }*/
//成员函数,运算符重载 //如果要在函数内部进行重载,则参数只能有一个 /*class Point{ public: int x; int y; public: Point(int x = 0, int y = 0){ this->x = x; this->y = y; } void print(){ cout << x << "," << y << endl; } Point operator+( Point &p){ Point tmp(this->x + p.x, this->y + p.y); return tmp; } Point operator-(Point &p){ Point tmp(this->x - p.x, this->y - p.y); return tmp; } }; void main(){ Point p1(3, 4); Point p2(1, 2); Point p3 = p1 - p2; p3.print(); system("pause"); }*/
//当属性私有时,通过友元函数完成运算符重载 class Point{ //属性私有 friend Point operator+(Point &p1, Point &p2); friend Point operator-(Point &p1, Point &p2); private: int x; int y; public: Point(int x = 0, int y = 0){ this->x = x; this->y = y; } void print(){ cout << x << "," << y << endl; } }; //运算符重载是放在类外面的 //这里使用的是引用,不是指针,要比指针方便 Point operator+(Point &p1, Point &p2){ Point tmp(p1.x + p2.x, p1.y + p2.y); return tmp; } Point operator-(Point &p1, Point &p2){ Point tmp(p1.x - p2.x, p1.y - p2.y); return tmp; } void main(){ Point p1(3, 4); Point p2(1, 2); Point p3 = p1 - p2; p3.print(); system("pause"); }