前言:
当前兄弟们对“类里面的构造函数”大体比较看重,你们都想要学习一些“类里面的构造函数”的相关知识。那么小编也在网上搜集了一些关于“类里面的构造函数””的相关资讯,希望看官们能喜欢,姐妹们快快来学习一下吧!使用C++语言判断两个圆是否相交
#define _CRT_SECURE_NO_WANRINGS#include<iostream>#include<cmath>using namespace std;//点类class Point{public: void setXY(int x, int y) { m_x = x; m_y = y; } //计算两点距离 double pointDistance(Point& another) { int d_x = m_x - another.m_x; int d_y = m_y - another.m_y; double dis = sqrt(d_x * d_x + d_y * d_y); return dis; }private: int m_x; int m_y;};class Circle{public: void setR(int r) { m_r = r; } void setXY(int x, int y) { P0.setXY(x, y); } //判断量圆是否相交 bool isIntersection(Circle& another) { //两圆半径和 int rr = m_r + another.m_r; //两圆心距离 double dis = P0.pointDistance(another.P0); if (dis<=rr) { //相交 return true; } else { return false; } }private: int m_r; Point P0;};int main(){ Circle c1, c2; int x, y, r; cout << "请输入第一个圆的半径" << endl; cin >> r; c1.setR(r); cout << "请输入第一个圆的x" << endl; cin >> x; cout << "请输入第一个圆的y" << endl; cin >> y; c1.setXY(x, y); cout << "请输入第二个圆的半径" << endl; cin >> r; c2.setR(r); cout << "请输入第二个圆的x" << endl; cin >> x; cout << "请输入第二个圆的y" << endl; cin >> y; c2.setXY(x, y); if (c1.isIntersection(c2)==true) { cout << "相交" << endl; } else { cout << "不相交" << endl; } return 0;}
构造函数
C++中的类可以定义与类名相同的特殊成员函数,这种与类名相同的成员函数叫做构造函数。
class 类名 { 类名(形式参数) { 构造体 } }
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<cmath>using namespace std;class Test{public:#if 1 void init(int x, int y) { m_x = x; m_y = y; name = (char*)malloc(100); strcpy(name, "AA"); }#endif //test类的构造函数 //在对象被创建时,用于初始化对象的函数 Test()//无参数的构造函数 { m_x = 0; m_y = 0; } Test(int x, int y) { m_x = x; m_y = y; } Test(int x) { m_x = x; m_y = 0; } void printT() { cout << "x=" << m_x << "y=" << m_y << endl; } ~Test()//析构函数与构造函数都没有 返回值 析构函数没有形参 { cout << "~Test()...." << endl; if (name!=NULL) { free(name); name = NULL; } cout << "free succ" << endl; }private: int m_x; int m_y; char* name;};void test01(){ Test t1(10, 20); t1.printT(); //在对象释放之前,要自动调用析构函数}int main(){#if 0 Test t1(10, 20); t1.printT(); Test t2(100); t2.printT(); Test t3;//调用无参数的构造函数 t3.printT();#endif test01(); return 0;}
析构函数
C++中的类可以定义一个特殊的成员函数清理对象,这个特殊的成员函数叫做析构函数。
class 类名{ ~类名() { 析构体 }}
规则:
1 对象销毁时,自动调用,完成销毁的善后工作。
2 无返值 与类名同,无参,不可以重载与默认参数
默认的无参构造函数和析构函数
#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<cmath>using namespace std;class Test{public: //默认的无参构造函数#if 0 Test() { }#endif //显示提供一个有参数的构造函数,这时默认的构造函数就不复存在了 Test(int x, int y) { m_x = x; m_y = y; } Test() { m_x = 0; m_y = 0; } void printT() { cout << "x=" << m_x << "y" << m_y << endl; } //默认的析构函数#if 0 ~Test() { }#endif //析构函数不能重载 ~Test() { cout << "~Test()..." << endl; }private: int m_x; int m_y;};int main(){ Test t1;//调用Test无参构造函数 当显示的构造函数存在时,默认的无参将销毁 t1.printT(); return 0;}拷贝构造函数
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class Test{public: Test() { m_x = 0; m_y = 0; } Test(int x, int y) { m_x = x; m_y = y; } void printT() { cout << "x=" << m_x << "y=" << m_y << endl; }#if 1 //显示的拷贝构造函数 Test(const Test&another) { cout << "Test(const Test&)..." << endl; m_x = another.m_x; m_y = another.m_y; }#endif#if 0 //有一个默认的拷贝构造函数 Test(const Test& another) { m_x = another.m_x; m_y = another.m_y; }#endif //=赋值操作符 void operator=(const Test& another) { m_x = another.m_x; m_y = another.m_y; }private: int m_x; int m_y;};int main(){ Test t1(100,200); Test t2(t1); t2.printT(); //构造函数是在对象初始化时调用的 Test t3 ;//初始化t3时调用t3的构造函数 t3 = t1;//这里调用的不是t3的拷贝构造函数 而是赋值造作符 return 0;}
默认的拷贝构造函数
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class A{public:#if 0 A() { m_a = 0; m_b = 0; }#endif#if 0 A(const A&another)//显示的拷贝构造 { m_a = another.m_a; m_b = another.m_b; } ~A() { }#endifprivate: int m_a; int m_b;};/*1.默认的无参构造函数 当没有任何显示的构造函数时 (显示的无参、显示有参、显示拷贝构造) 默认无参构造函数就会出现2.默认的拷贝构造: 当没有显示的拷贝构造函数 默认的拷贝构造就会出现3.默认的析构函数 当没有显示的构造函数时 默认的析构函数就会出现*/int main(){ A a;//提供显示拷贝构造 默认无参构造就销毁 A a1(a); return 0;}拷贝构造函数的应用场景
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class Test{public: Test() { cout << "test()..." << endl; m_x = 0; m_y = 0; } Test(int x, int y) { cout << "Test(int x,int y)..." << endl; m_x = x; m_y = y; } Test(const Test& another) { cout << "Test(const Test&)..." << endl; m_x = another.m_x; m_y = another.m_y; } void operator=(const Test& another) { cout << "operator=(const Test& another)" << endl; m_x = another.m_x; m_y = another.m_y; } void printT() { cout << "x=" << m_x << "y=" << m_y << endl; } ~Test() { cout << "~Test()..." << endl; }private: int m_x; int m_y;};//析构函数的调用顺序:与构造相反,谁先构造,谁后析构。void test01(){ Test t1(10, 20); Test t2(t1);//Test t2=t1; 等价}void test02(){ Test t1(10, 20); Test t2; t2 = t1;//=操作符}void func(Test t)//Test t=t1;//Test t的拷贝构造函数{ cout << "func begin" << endl; t.printT(); cout << "func end" << endl;}void test03(){ cout << "test3 begin..." << endl; Test t1(10, 20); func(t1); cout << "test3 end..." << endl;}Test func2(){ cout << "func2 begin.." << endl; Test temp(10, 20); temp.printT(); cout << "func2 end.." << endl; return temp;}//匿名对象=temp 匿名对象.拷贝构造(temp)void test04(){ cout << "test04 begin.." << endl; func2();//返回一个匿名对象 /* 当一个函数返回一个匿名对象时,函数外部没有任何变量接收的情况下 这个匿名对象将被编译器立即回收,不会再被使用 */ cout << "test04 end.." << endl;}void test05(){ cout << "test05 begin.." << endl; Test t1 = func2();//这里不会触发t1的匿名拷贝 //而是将匿名对象转正 起名叫t1 cout << "test05 end.." << endl;}void test06(){ cout << "test06 begin..." << endl; Test t1; t1 = func2(); t1.printT(); cout << "test06 end..." << endl;}int main(){ //test01(); //test02(); //test03(); //test04(); //test05(); test06(); return 0;}深拷贝与浅拷贝
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class Teacher{public: Teacher(int id, const char* name) { cout << "Teacher(int id,char*name)..." << endl; m_id = id;//赋值id //赋值姓名 int len = strlen(name); m_name = (char*)malloc(len + 1); strcpy(m_name, name); } void printT() { cout << "m_id=" << m_id << ",m_name=" << m_name << endl; } //显示提供一个拷贝构造函数,来完成深拷贝动作 Teacher(const Teacher& another) { m_id = another.m_id; int len = strlen(another.m_name); m_name = (char*)malloc(len + 1); strcpy(m_name, another.m_name); } ~Teacher() { cout << "~Teacher..." << endl; if (m_name!=NULL) { free(m_name); m_name = NULL; } }private: int m_id; char *m_name;};void test(){ Teacher t1(1, "AA"); t1.printT(); Teacher t2(t1);//t2的拷贝构造}int main(){ test(); return 0;}构造函数的初始化列表
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class A{public: A(int a) { cout << "A(int)..." << a << endl; m_a = a; } ~A() { cout << "~A()" << endl; } void printA() { cout << "m_a=" << m_a << endl; }private: int m_a;};//构造函数的初始化列表class B{public: B(A& a1, A& a2, int b):m_a1(a1),m_a2(a2) { cout << "B(A&,A&,int)..." << endl; m_b = b; } //构造对象成员的顺序与初始化列表的顺序无关 //而是跟成员对象的定义顺序有关 B(int a1, int a2, int b) :m_a1(a1), m_a2(a2) { cout << "B(int,int,int)..." << endl; m_b = b; } void printB() { cout << "b=" << m_b << endl; m_a1.printA(); m_a2.printA(); } ~B() { cout << "~B()..." << endl; }private: int m_b; A m_a1; A m_a2;}; void test01(){ A a1(10), a2(100); B b(a1, a2, 1000); b.printB();}void test02(){ B b(10, 20, 30); b.printB();}class ABC{public: ABC(int a, int b, int c,int m):m_m(m) { cout << "ABC(int,int,int)" << endl; m_a = a; m_b = b; m_c = c; } ~ABC() { cout << "~ABC()" << endl; }private: int m_a; int m_b; int m_c; const int m_m;//常量};class ABCD{public: ABCD(int a, int b, int c, int d,int m):m_abc(a,b,c,m) { m_d = d; } ABCD(ABC& abc, int d):m_abc(abc) { m_d = d; }private: int m_d; ABC m_abc;};int main(){ //test01(); test02(); B b(10, 20, 30); b.printB(); ABC abc(10, 20, 30,40); ABCD(1, 2, 3, 4, 5); ABCD abcd1(abc, 40); return 0;}new和delete
#define _CRT_SECURE_NO_WARNINGS#include<iostream>using namespace std;class Test{public: Test() { cout << "Test()" << endl; m_a = 0; m_b = 0; } Test(int a,int b) { cout << "Test(int,int)" << endl; m_a = a; m_b = b; } void printT() { cout << "printT()" << m_a <<","<< m_b << endl; } ~Test() { cout << "~Test()" << endl; }private: int m_a; int m_b;};//C语言中void test01(){ //开辟空间 int* p = (int*)malloc(sizeof(int)); *p = 10; if (p!=NULL) { free(p); //delete p; p = NULL; } //创建数组 int* array_p = (int*)malloc(sizeof(int) * 10); for (int i = 0; i < 10; i++) { array_p[i] = i + 1; } for (int i = 0; i < 10; i++) { printf("%d ", array_p[i]); } printf("\n"); if (array_p!=NULL) { free(array_p); array_p = NULL; } cout << "===========================" << endl; Test* tp = (Test*)malloc(sizeof(Test)); tp->printT(); if (tp!=NULL) { free(tp); tp = NULL; }}//C++中//new在堆上初始化一个对象时,会触发对象的构造函数 ,二malloc不会//free不能出触发一个析构函数 deletevoid test02(){ int* p = new int; *p = 10; if (p!=NULL) { //free(p); delete p; p = NULL; } int a(10);//等价于 int a = 10; //创建数组 int* array_p = new int[10]; for (int i = 0; i < 10; i++) { array_p[i] = i + 1; } for (int i = 0; i < 10; i++) { cout << array_p[i] << " "; } cout << endl; if (array_p!=NULL) { delete[]array_p; } cout << "===========================" << endl; //Test* tp = new Test(10, 20);//触发有参构造 Test* tp2 = new Test;//触发无参构造 tp2->printT(); if (tp2!=NULL) { delete tp2; tp2 = NULL; }}int main(){ test01(); cout << "---------------------" << endl; test02(); return 0;}
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #类里面的构造函数 #创建一个类为该类定义三个构造函数 #有参构造函数 #带参构造函数和无参构造函数