General form of composite class constructor definition in C++

In the C++ class composition section it is mentioned:

The general form of a composite class constructor definition is:

class name:: class name (formal parameter list): embedded object 1 (formal parameter list), embedded object 2 (formal parameter list), ...

{class initialization}

Among them, "embedded object 1 (formal parameter list), Lexi object 2 (formal parameter list), ..." is called the initialization list, and its function is to initialize the embedded object.

The same can be done for data members of primitive types.

According to the above format, I wrote a point class and a line segment class. Two points form a line segment and calculate its length.

#include<iostream>
#include<cmath>    
using namespace std;

class Point	    //点
{
private:
	int x, y;
public:
	Point(int xx = 0, int yy = 0):x(xx),y(yy){}     //constructor
	int getX() { return x; }
	int getY() { return y; }
};

class Line //Line segment
{
private:
	Point p1, p2; //A combination of classes
	int length; // length of line segment
public:
	Line(Point xp1, Point xp2):p1(xp1),p2(xp2)	 //Constructor { int a = p1.getX() - p2.getX();
	
		
		int b = p1.getY () - p2.getY ();
		length = sqrt(a*a + b * b);}
	
	void show()
	{
		cout << "The length of the line segment is:" << length << endl;
	}
};

intmain()
{
	Point p1(1, 1); //p1 point
	Point p2(4, 5);			//p2点

	Line line(p1, p2); //connect two points to form a line segment
	line.show();

	system("pause");
	return 0;
}

When I first saw this way of writing, I still couldn't remember the form. When I wrote the constructor of the composite class by myself one day, I didn't remember the form, and I wrote it as follows:

#include<iostream>
#include<cmath>
using namespace std;

class Point	//点
{
private:
	int x, y;
public:
	Point(int xx = 0, int yy = 0) //constructor
	{
		x = xx;
		y = yy;
	}
	int getX() { return x; }
	int getY() { return y; }
};

class Line //Line segment
{
private:
	Point p1, p2; //A combination of classes
	int length; // length of line segment
public:
	Line(Point xp1, Point xp2) //Constructor
	{
		p1 = xp1;
		p2 = xp2;

		int a = p1.getX() - p2.getX();
		int b = p1.getY () - p2.getY ();
		length = sqrt(a*a + b * b);
	}
	void show()
	{
		cout << "The length of the line segment is:" << length << endl;
	}
};

intmain()
{
	Point p1(1, 1); //p1 point
	Point p2(4, 5);			//p2点

	Line line(p1, p2); //connect two points to form a line segment
	line.show();

	system("pause");
	return 0;
}

The two writing methods are compared as follows:

The first the second
Point(int xx = 0, int yy = 0):x(xx),y(yy){} 
Point(int xx = 0, int yy = 0) //constructor
	{
		x = xx;
		y = yy;
	}
Line(Point xp1, Point xp2):p1(xp1),p2(xp2)	 //Constructor { int a = p1.getX() - p2.getX();
	
		
		int b = p1.getY () - p2.getY ();
		length = sqrt(a*a + b * b);}
	
Line(Point xp1, Point xp2) //Constructor
	{
		p1 = xp1;
		p2 = xp2;

		int a = p1.getX() - p2.getX();
		int b = p1.getY () - p2.getY ();
		length = sqrt(a*a + b * b);
	}

In comparison, the first method can reduce the number of lines of code, but I think the second method makes the code easier to understand┗|`O′|┛ 嗷~~.

(/≧▽≦)/, I finally finished writing such a simple question. After seeing the first form, you will not be blinded. O(∩_∩)O









Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853259&siteId=291194637