C + + class composition (Chapter 4)

Posted by NTGr on Sun, 09 Jan 2022 23:09:47 +0100

catalogue

Combination of classes

Concept of combination

Constructor design of class composition

Initialization order when constructing composite class objects

Class assembler example

Example 4-4: combination of Line class and Line class

Forward reference declaration

Forward reference declaration considerations

Combination of classes

Concept of combination

A member in a class is an object of another class.

More complex abstractions can be implemented on the basis of existing abstractions.

Constructor design of class composition

Principle: be responsible for initializing not only the basic type member data in this class, but also the object members.

Declaration form:

Class name:: class name (formal parameters required by object members, formal parameters of this class member)

: object 1 (parameter), object 2 (parameter)

        {

/ / other statements in the function body

        }

Initialization order when constructing composite class objects

First, initialize the members listed in the constructor initialization list (including basic type members and object members). The initialization order is the order in which the members are defined in the class body.

After processing the initialization list, execute the function body of the constructor.

Call order of member object constructor: according to the declaration order of object members, the declarant first constructs.

Initialize the member object that does not appear in the list, and call the default constructor (i.e. parameterless)

Class assembler example

Example 4-4: combination of Line class and Line class

//4_4.cpp

#include <iostream>

#include <cmath>

using namespace std;

class Point { //Point class definition

public:

	Point(int xx = 0, int yy = 0) {

		x = xx;

		y = yy;

	}

	Point(Point& p);

	int getX() { return x; }

	int getY() { return y; }

private:

	int x, y;

};


Point::Point(Point& p) { //Implementation of copy constructor

	x = p.x;

	y = p.y;

	cout << "Calling the copy constructor of Point" << endl;

}


//Combination of classes

class Line { //Definition of Line class

public: //External interface

	Line(Point xp1, Point xp2);

	Line(Line& l);

	double getLen() { return len; }

private: //Private data member

	Point p1, p2; //Objects p1,p2 of Point class

	double len;

};


//Constructor of composite class

Line::Line(Point xp1, Point xp2) : p1(xp1), p2(xp2) {

	cout << "Calling constructor of Line" << endl;

	double x = static_cast<double>(p1.getX() - p2.getX());

	double y = static_cast<double>(p1.getY() - p2.getY());

	len = sqrt(x * x + y * y);

}

Line::Line(Line& l) : p1(l.p1), p2(l.p2) {//Copy constructor for composite classes

	cout << "Calling the copy constructor of Line" << endl;

	len = l.len;

}


//Main function

int main() {

	Point myp1(1, 1), myp2(4, 5); //Create an object of the Point class

	Line line(myp1, myp2); //Create an object of the Line class

	Line line2(line); //Create a new object using the copy constructor

	cout << "The length of the line is: ";

	cout << line.getLen() << endl;

	cout << "The length of the line2 is: ";

	cout << line2.getLen() << endl;

	return 0;

}

Forward reference declaration

l} classes should be declared before use

l) if you need to reference a class before its declaration, you should make a forward reference declaration.

l) forward reference declaration only introduces an identifier for the program, but the specific declaration is elsewhere.

l) example:

class B;  //Forward reference declaration

class A {

public:

	void f(B b);

};

class B {

public:

	void g(A a);

};

Forward reference declaration considerations

l# using forward reference declaration can solve some problems, but it is not omnipotent.

l) before providing a complete class declaration, the object of this class cannot be declared, and the object of this class cannot be used in inline member functions.

l) when using forward reference declaration, only the declared symbols can be used, and no details of the class can be involved.

l , cases

class Fred; //Forward reference declaration

class Barney {

	Fred x; //Error: Declaration of class Fred is incomplete

};

class Fred {

	Barney y;

};

Topics: C++ Back-end