The inclusion of a class (known as has a) is a software reuse technique in program design. That is, when a new class is defined, the other class is "copied" through the compiler. When a class contains a member of a class type that has already been defined, the constructor with parameters initializes the data member, using the initialization syntax form.
Constructor (meta table): Object member 1 (meta table), ..., object member n (meta table);
1#include <iostream>2#include <cmath>3 using namespacestd;4#include <string>5 6 //calculates the distance between two points using a class containing7 classpoint{8 Public:9Point (DoubleXDoubley) {Ten This->x =x; One This->y =y; A } -Point (point&p) { -cout <<"\tcopy_constract"<<Endl; the } - DoubleGetX () { - returnx; - } + DoubleGetY () { - returny; + } A Private: at Doublex, y; - }; - classdistance{ - Public: - Distance (Point dp1,point DP2):p 1 (DP1), p2 (DP2) { - Doublex = Dp1.getx ()-dp2.getx (); in Doubley = dp1.gety ()-dp2.gety (); -dist = sqrt (x*x + y*y); to } + Doublegetdist () { - returnDist; the } * Private: $ Point p1,p2;Panax Notoginseng DoubleDist; - }; the + voidMain () { ADistance D (3.1,5.7), point (4.7,6.8)); thecout << d.getdist () <<Endl; +}
1#include <iostream>2#include <cmath>3 using namespacestd;4#include <string>5 6 //calculates the distance between two points using a class containing7 classpoint{8 Public:9Point (DoubleXDoubley) {Ten This->x =x; One This->y =y; A } -Point (point&p) { -cout <<"\tcopy_constract"<<Endl; the } - DoubleGetX () { - returnx; - } + DoubleGetY () { - returny; + } A Private: at Doublex, y; - }; - classdistance{ - Public: - Distance (Point dp1,point DP2):p 1 (DP1), p2 (DP2) { - Doublex = Dp1.getx ()-dp2.getx (); in Doubley = dp1.gety ()-dp2.gety (); -dist = sqrt (x*x + y*y); to } + Doublegetdist () { - returnDist; the } * Private: $ Point p1,p2;Panax Notoginseng DoubleDist; - }; the + voidMain () { ADistance D (3.1,5.7), point (4.7,6.8)); thecout << d.getdist () <<Endl; +}
Including C + +