《Beginning C++17》-学习笔记-Chapter 11-Defining Your Own Data Types

《Beginning C++17》-学习笔记-Chapter 01-Basic Ideas 书名:Beginning C++17 - From Novice to Professional 作者:Ivor Horton, Peter Van Weert 笔记: Blocks can be placed inside other blocks—this concept is called nesting. Blocks can be nested, one within anothe... 阅读详情

Every object of a given class incorporates the same combination of things, specifically, the set of data values as member
variables of the class that characterize an object and the set of operations as member functions of the class.
This packaging of data values and functions within an object is referred to as encapsulation.

The ability to make the data values for an object generally inaccessible is called data hiding or information hiding.

A class is a user - defined data type.
• The variables and functions defined within a class are members of the class.The variables are member variables, and the functions are member functions.
Member functions are also often referred to as methods; member variables are called either data members or fields.
• Variables of a class type store objects. Objects are sometimes called instances of the class.Defining an instance of a class is referred to as instantiation.


public and private precede a sequence of members that are or are not accessible outside the class. The specification of public or private applies to all members that follow until there is a different specification. You could omit the first private specification here and get the default status of private, but it’s better to make it explicit. Members in a private section of a class can be accessed only from functions that are members of the same class. Member variables or functions that need to be accessed by a function that is not a member of the class must be specified as public. A member function can reference any other member of the same class, regardless of the access specification, by just using its name.

#include "pch.h"
#include <iostream>
class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };
public:
	// Function to calculate the volume of a box
	double volume()
	{
		return length * width * height;
	}
};

int main()
{
	Box myBox; // A Box object with all dimensions 1

	std::cout << "Volume of myBox is " << myBox.volume() << std::endl; // Volume is 1.0
}
#include <iostream>
class Box
{
public:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// Function to calculate the volume of a box
	double volume()
	{
		return length * width * height;
	}
};

int main()
{
	Box myBox; // A Box object with all dimensions 1
	myBox.length = 1.5;
	myBox.width = 2.0;
	myBox.height = 4.0;
	std::cout << "Volume of myBox is " << myBox.volume() << std::endl; // Volume is 12.0

}

Structures and classes are nearly completely equivalent.You define a structure in the same way as a class, only using the struct keyword instead of the class keyword.The main difference between the two is that in contrast to members of a class, the members of a structure are public by default. 


#include <iostream>
/*A class constructor is a special kind of function in a class that differs in a few significant respects from
an ordinary member function. A constructor is called whenever a new instance of the class is defined. It
provides the opportunity to initialize the new object as it is created and to ensure that member variables
contain valid values. A class constructor always has the same name as the class.
A constructor does not return a value and therefore has no return type.
If you don’t define a constructor for a class, the compiler will supply a default default
constructor.
A default constructor is a constructor that can be called without arguments. If you do not define any
constructor for a class—so no default constructor or any other constructor—the compiler generates a default
constructor for you. That’s why it’s called a default default constructor; it is a default constructor that is
generated by default. A compiler-generated default constructor has no parameters, and its sole purpose is
to allow an object to be created.
as soon as you do define any constructor, even a nondefault one with parameters, the default default
constructor is no longer supplied. There are circumstances in which you need a constructor with no parameters
in addition to a constructor that you define that has parameters. In this case, you must ensure that there is
a definition for the no-arg constructor in the class.
*/
class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };
public:
	// The default constructor that was supplied by the compiler...
	Box()
	{
		// Empty body so it does nothing...
	}

public:
	// Function to calculate the volume of a box
	double volume()
	{
		return length * width * height;
	}
};

int main()
{
	Box myBox; // A Box object with all dimensions 1

	std::cout << "Volume of myBox is " << myBox.volume() << std::endl; // Volume is 1

}
// Defining a class constructor
#include <iostream>

// Class to represent a box
class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// Constructor
	Box(double lengthValue, double widthValue, double heightValue)
	{
		std::cout << "Box constructor called." << std::endl;
		length = lengthValue;
		width = widthValue;
		height = heightValue;
	}

	// Function to calculate the volume of a box
	double volume()
	{
		return length * width*height;
	}
};

int main()
{
	Box firstBox{ 80.0, 50.0, 40.0 };               // Create a box
	double firstBoxVolume{ firstBox.volume() };     // Calculate the box volume
	std::cout << "Volume of Box object is " << firstBoxVolume << std::endl;
	/*once you define a constructor, the compiler won’t supply a default constructor
anymore, at least not by default. That means the following statement will no longer compile.*/
// Box secondBox;                    // Causes a compiler error message
}
// Defining a class constructor
#include <iostream>

// Class to represent a box
class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// Constructor
	Box(double lengthValue, double widthValue, double heightValue)
	{
		std::cout << "Box constructor called." << std::endl;
		length = lengthValue;
		width = widthValue;
		height = heightValue;
	}

	//Box() {} // the first way of defining a default constructor
	Box() = default; //the second way of defining a default constructor; the preferred way.

	// Function to calculate the volume of a box
	double volume()
	{
		return length * width*height;
	}
};

int main()
{
	Box secondBox;               // Create a box
	double secondBoxVolume{ secondBox.volume() };     // Calculate the box volume
	std::cout << "Volume of Box object is " << secondBoxVolume << std::endl;

}
// Defining functions and constructors outside the class definition

#include <iostream>

class  Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// Constructors
	Box(double lengthValue, double widthValue, double heightValue);
	Box() = default;

	// Function to calculate the volume of a box
	double volume();
};


// Constructor definition
Box::Box(double lengthValue, double widthValue, double heightValue)
{
	std::cout << "Box constructor called." << std::endl;
	length = lengthValue;
	width = widthValue;
	height = heightValue;
}

// Function to calculate the  volume of  a box
double Box::volume()
{
	return length * width*height;
}

int main()
{
	Box firstBox{ 80.0, 50.0, 40.0 };             // Create a box
	double firstBoxVolume{ firstBox.volume() };    // Calculate the box  volume
	std::cout << "Volume of the first Box object is " << firstBoxVolume << std::endl;

	Box secondBox;                               // Uses compiler-generated default constructor
	double secondBoxVolume{ secondBox.volume() };  // Calculate the box volume
	std::cout << "Volume of the second Box object is " << secondBoxVolume << std::endl;
}
// Default Constructor Parameter Values

#include <iostream>

class  Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// A constructor for which all parameters have a default value counts as a default constructor.
	Box(double lv = 2.0, double wv = 2.0, double hv = 2.0);

	// Function to calculate the volume of a box
	double volume();
};


// Constructor definition
Box::Box(double lengthValue, double widthValue, double heightValue)
{
	std::cout << "Box constructor called." << std::endl;
	length = lengthValue;
	width = widthValue;
	height = heightValue;
}

// Function to calculate the  volume of  a box
double Box::volume()
{
	return length * width*height;
}

int main()
{
	Box firstBox;
	double firstBoxVolume{ firstBox.volume() };
	std::cout << "Volume of the first Box object is " << firstBoxVolume << std::endl;//output 8

	Box secondBox{ 80.0, 50.0, 40.0 };
	double secondBoxVolume{ secondBox.volume() };
	std::cout << "Volume of the first Box object is " << secondBoxVolume << std::endl;//output 160000

}
#include <iostream>

class  Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// A constructor for which all parameters have a default value counts as a default constructor.
	Box(double lv = 2.0, double wv = 2.0, double hv = 2.0);

	// Function to calculate the volume of a box
	double volume();
};


// Constructor definition using a member initializer list
Box::Box(double lv, double wv, double hv) : length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor called." << std::endl;
}

// Function to calculate the  volume of  a box
double Box::volume()
{
	return length * width*height;
}

int main()
{
	Box firstBox;
	double firstBoxVolume{ firstBox.volume() };
	std::cout << "Volume of the first Box object is " << firstBoxVolume << std::endl;//output 1

	Box secondBox{ 80.0, 50.0, 40.0 };
	double secondBoxVolume{ secondBox.volume() };
	std::cout << "Volume of the first Box object is " << secondBoxVolume << std::endl;//output 160000

}
// Using a delegating constructor
#include <iostream>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// Constructors
	Box(double lv, double wv, double hv);
	explicit Box(double side);                        // Constructor for a cube
	Box() = default;                                  // No-arg constructor

	double volume();                                  // Function to calculate the volume of a box
};

// Constructor definition
Box::Box(double lv, double wv, double hv)
	: length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor 1 called." << std::endl;
}

// Constructor for Box that is a cube
Box::Box(double side) : Box{ side, side, side }
{
	std::cout << "Box constructor 2 called." << std::endl;
}

// Function to calculate the volume of a box
double Box::volume()
{
	return length * width*height;
}



int main()
{
	Box box1{ 2.0, 3.0, 4.0 };           // An arbitrary box
	/*Creating the second object calls constructor 1 followed by constructor 2.*/
	Box box2{ 5.0 };                     // A box that is a cube

	std::cout << "box1 volume = " << box1.volume() << std::endl;
	std::cout << "box2 volume = " << box2.volume() << std::endl;

	/* compiler supplied a default copy constructor, which is a constructor that creates an object by copying an existing object. The
default copy constructor copies the values of the member variables of the object that is the argument to the
new object.*/
	Box box3{ box2 };
	std::cout << "box3 volume = " << box3.volume() << std::endl; // Volume = 125
}
#include <iostream>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	// Constructors
	Box(double lv, double wv, double hv);
	explicit Box(double side);                        // Constructor for a cube
	Box() = default;                                  // No-arg constructor
	Box(const Box& box);//the Copy Constructor
	double volume();                                  // Function to calculate the volume of a box
};

// Constructor definition
Box::Box(double lv, double wv, double hv)
	: length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor 1 called." << std::endl;
}

// Constructor for Box that is a cube
Box::Box(double side) : Box{ side, side, side }
{
	std::cout << "Box constructor 2 called." << std::endl;
}
//copy constructor
Box::Box(const Box& box) : length{ box.length }, width{ box.width }, height{ box.height }
{
	std::cout << "Box constructor 3 called." << std::endl;
}

// Function to calculate the volume of a box
double Box::volume()
{
	return length * width*height;
}



int main()
{
	Box box1{ 2.0, 3.0, 4.0 };           // An arbitrary box
	/*Creating the second object calls constructor 1 followed by constructor 2.*/
	Box box2{ 5.0 };                     // A box that is a cube

	std::cout << "box1 volume = " << box1.volume() << std::endl;
	std::cout << "box2 volume = " << box2.volume() << std::endl;

	Box box3{ box2 };
	std::cout << "box3 volume = " << box3.volume() << std::endl; // Volume = 125
}
#include <iostream>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	Box(double lv, double wv, double hv);

	Box() = default;

	double volume();

	double getLength() { return length; }
	double getWidth() { return width; }
	double getHeight() { return height; }


	Box*  setLength(double lv) {
		if (lv > 0) length = lv; 
		return this;
	}
	Box*  setWidth(double wv) {
		if (wv > 0) width = wv; 
		return this;
	}
	Box*  setHeight(double hv) {
		if (hv > 0) height = hv; return this;
	}

};

// Constructor definition
Box::Box(double lv, double wv, double hv)
	: length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor 1 called." << std::endl;
}

double Box::volume()
{
	return this->length * this->width*this->height;
}

int main()
{
	Box myBox{ 3.0, 4.0, 5.0 };
	std::cout << "myBox dimensions are " << myBox.getLength()
		<< " by " << myBox.getWidth()
		<< " by " << myBox.getHeight() << std::endl;

	myBox.setLength(-20.0)->setWidth(40.0)->setHeight(10.0); // Set all dimensions of myBox
	/*Because the mutator functions return the this pointer, you can use the value returned by one function
to call the next. */
	std::cout << "myBox dimensions are now " << myBox.getLength() // 3 (unchanged)
		<< " by " << myBox.getWidth() // by 40
		<< " by " << myBox.getHeight() << std::endl; // by 10
}

#include <iostream>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	Box(double lv, double wv, double hv);

	Box() = default;

	double volume();

	double getLength() { return length; }
	double getWidth() { return width; }
	double getHeight() { return height; }


	Box&  setLength(double lv) {
		if (lv > 0) length = lv;
		/*returning references to *this*/
		return *this;
	}
	Box&  setWidth(double wv) {
		if (wv > 0) width = wv;
		return *this;
	}
	Box&  setHeight(double hv) {
		if (hv > 0) height = hv;
		return *this;
	}

};


Box::Box(double lv, double wv, double hv)
	: length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor 1 called." << std::endl;
}

double Box::volume()
{
	return length * width * height;
}

int main()
{
	Box myBox{ 3.0, 4.0, 5.0 };
	std::cout << "myBox dimensions are " << myBox.getLength()
		<< " by " << myBox.getWidth()
		<< " by " << myBox.getHeight() << std::endl;
	/*The following pattern is called method chaining. */
	myBox.setLength(-20.0).setWidth(40.0).setHeight(10.0); // Set all dimensions of myBox
	std::cout << "myBox dimensions are now " << myBox.getLength() // 3 (unchanged)
		<< " by " << myBox.getWidth() // by 40
		<< " by " << myBox.getHeight() << std::endl; // by 10
}
#include <iostream>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };

public:
	Box(double lv, double wv, double hv);

	Box() = default;

	double getLength() const { return length; }
	double getWidth() const { return width; }
	double getHeight() const { return height; }

};


Box::Box(double lv, double wv, double hv)
	: length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor 1 called." << std::endl;
}


int main()
{
	const Box myBox{ 3.0, 4.0, 5.0 };
	std::cout << "myBox dimensions are " << myBox.getLength()
		<< " by " << myBox.getWidth()
		<< " by " << myBox.getHeight() << std::endl;
	/*For const objects you can only call const member functions. */
}
#include <iostream>

int main()
{
	double length{ 10 };
	double& length1 = length;
	double length2 = length;
	length1 = 20;
	std::cout << length << std::endl;//output 20
	length2 = 30;
	std::cout << length << std::endl;//output 20
}
// Defining mutable member variables that can be modified within const member functions
#include <iostream>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };
	mutable unsigned count{};
public:
	// Constructors
	Box() = default;
	Box(double length, double width, double height);

	double volume() const;      // Function to calculate the volume of a box
	void printVolume() const;   // Function to print out the volume of a box

	// Functions to provide access to the values of member variables
	double getLength() const { return length; }
	double getWidth() const { return width; }
	double getHeight() const { return height; }

	// Functions to set member variable values
	void setLength(double lv) { if (lv > 0) length = lv; }
	void setWidth(double wv) { if (wv > 0) width = wv; }
	void setHeight(double hv) { if (hv > 0) height = hv; }
};

// Constructor definition
Box::Box(double lv, double wv, double hv)
	: length{ lv }, width{ wv }, height{ hv }
{
}

// Function to calculate the volume of a box
double Box::volume() const
{
	return length * width * height;
}

// Function to print the volume of a Box to std::cout
void Box::printVolume() const
{
	// Count how many times printVolume() is called using a mutable member in a const function
	std::cout << "The volume of this box is " << volume() << std::endl;
	std::cout << "printVolume() has been called " << ++count << " time(s)" << std::endl;
}


int main()
{
	Box myBox{ 3.0, 4.0, 5.0 };
	myBox.printVolume();

	myBox.setHeight(55.5);
	myBox.printVolume();

	// Even for constant Boxes mutable member variables can be modified:
	const Box constBox{ 1.0, 2.0, 3.0 };
	constBox.printVolume();
	constBox.printVolume();
}

// Using a friend function of a class
#include <iostream>
#include <memory>

class Box
{
private:
	double length;
	double width;
	double height;

public:
	// Constructors
	Box(double lv = 1.0, double wv = 1.0, double hv = 1.0);

	double volume() const;                        // Function to calculate the volume of a box

	friend double surfaceArea(const Box& aBox);   // Friend function for the surface area
};

// Constructor definition
Box::Box(double lv, double wv, double hv) : length{ lv }, width{ wv }, height{ hv }
{
	std::cout << "Box constructor called." << std::endl;
}

// Function to calculate the volume of a box
double Box::volume() const
{
	return length * width*height;
}

int main()
{
	Box box1{ 2.2, 1.1, 0.5 };            // An arbitrary box
	Box box2;                           // A default box
	/*create a smart pointer to a Box object allocated in the free store*/
	auto box3 = std::make_unique<Box>(15.0, 20.0, 8.0); // Dynamically allocated Box

	std::cout << "Volume of box1 = " << box1.volume() << std::endl;
	std::cout << "Surface area of box1 = " << surfaceArea(box1) << std::endl;

	std::cout << "Volume of box2 = " << box2.volume() << std::endl;
	std::cout << "Surface area of box2 = " << surfaceArea(box2) << std::endl;

	std::cout << "Volume of box3 = " << box3->volume() << std::endl;
	std::cout << "Surface area of box3 = " << surfaceArea(*box3) << std::endl;
}

// friend function to calculate the surface area of a Box object
double surfaceArea(const Box& aBox)
{
	return 2.0 * (aBox.length * aBox.width + aBox.length * aBox.height + aBox.height * aBox.width);
}

#include <iostream>

class  Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };
	/*Static member variables of a class are associated with the class as a whole, not with any particular object of
the class. When you declare a member variable of a class as static, the static member variable is defined
only once and will exist even if no class objects have been created. Each static member variable is accessible
in any object of the class and is shared among however many objects there are. An object gets its own
independent copies of the ordinary member variables, but only one instance of each static member variable
exists, regardless of how many class objects have been defined.*/
	static inline size_t objectCount{}; // Count of objects in existence

public:
	/* Constructors */
	Box(double lv, double wv, double hv);
	Box(double side);       // Constructor for a cube
	Box();                  // Default constructor
	Box(const Box& box);    // Copy constructor

	double volume() const;   // Function to calculate the volume of a box
	size_t getObjectCount() const { return objectCount; }
};

Box::Box(double lv, double wv, double hv)       // Constructor definition
	: length{ lv }, width{ wv }, height{ hv }
{
	++objectCount;
	std::cout << "Box constructor 1 called." << std::endl;
}

Box::Box(double side) : Box{ side, side, side }  // Constructor for a cube
{
	std::cout << "Box constructor 2 called." << std::endl;
}

Box::Box()                                      // Default constructor
{
	++objectCount;
	std::cout << "Default Box constructor called." << std::endl;
}

Box::Box(const Box& box)                        // Copy constructor
	: length{ box.length }, width{ box.width }, height{ box.height }
{
	++objectCount;
	std::cout << "Box copy constructor called." << std::endl;
}

// Function to calculate the volume of a box
double Box::volume() const
{
	return length * width * height;
}

int main()
{
	const Box box1{ 2.0, 3.0, 4.0 };           // An arbitrary box
	Box box2{ 5.0 };                     // A box that is a cube
	std::cout << "box1 volume = " << box1.volume() << std::endl;
	std::cout << "box2 volume = " << box2.volume() << std::endl;
	Box box3{ box2 };
	std::cout << "box3 volume = " << box3.volume() << std::endl;   // Volume = 125

	std::cout << std::endl;
	/*The initial values for the first three array elements are existing objects, so the compiler calls the copy
constructor to duplicate box1, box2, and box3. The fourth element is initialized with an object that
is created in the braced initializer for the array by the constructor 2, which calls constructor 1 in its
initialization list. The last two array elements have no initial values specified, so the compiler calls the
default constructor to create them.*/
	Box boxes[6]{ box1, box2, box3, Box {2.0} };

	std::cout << sizeof boxes << std::endl;//ouput 144
	std::cout << sizeof Box << std::endl;//output 24

	/*only one copy of the static member objectCount exists, and all the
constructors are updating it. The getObjectCount() function is called for the box1 object, but you could use
any object including any of the array elements to get the same result. */
	std::cout << "\nThere are now " << box3.getObjectCount() << " Box objects." << std::endl;//output 9
}

// Defining and accessing static constants
#include <iostream>
#include <string>
#include <string_view>

class CylindricalBox
{
public:
	static inline const float maxRadius{ 35.0f };
	static inline const float maxHeight{ 60.0f };
	static inline const std::string defaultMaterial{ "paperboard" };

	CylindricalBox(float radius, float height, std::string_view material = defaultMaterial);

	double volume() const;

private:
	// The value of PI used by CylindricalBox's volume() function
	static inline const float PI{ 3.141592f };

	double radius;
	double height;
	std::string material;
};

CylindricalBox::CylindricalBox(float r, float h, std::string_view mat)
	: radius(r)
	, height(h)
	, material(mat)
{
}

double CylindricalBox::volume() const
{
	return PI * radius * radius * height;
}

int main()
{
	const CylindricalBox bigBox{ 1.23f, CylindricalBox::maxHeight, CylindricalBox::defaultMaterial };
	std::cout << "The volume of bigBox is " << bigBox.volume() << std::endl;
}
// Implementing a destructor
#include <iostream>
#include <memory>

class Box
{
private:
	double length{ 1.0 };
	double width{ 1.0 };
	double height{ 1.0 };
	static inline size_t objectCount{};              // Count of objects in existence

public:
	// Constructors
	Box(double lv, double wv, double hv);
	Box(double side);       // Constructor for a cube
	Box();                  // Default constructor
	Box(const Box& box);    // Copy constructor

	double volume() const;                            // Function to calculate the volume of a box

	static size_t getObjectCount() { return objectCount; }

	~Box();                                           // Destructor
};

// Constructor definition
Box::Box(double lv, double wv, double hv) : length{ lv }, width{ wv }, height{ hv }
{
	++objectCount;
	std::cout << "Box constructor 1 called." << std::endl;
}

Box::Box(double side) : Box{ side, side, side }  // Constructor for a cube
{
	std::cout << "Box constructor 2 called." << std::endl;
}

Box::Box()                                      // Default constructor
{
	++objectCount;
	std::cout << "Default Box constructor called." << std::endl;
}

Box::Box(const Box& box)                        // Copy constructor
	: length{ box.length }, width{ box.width }, height{ box.height }
{
	++objectCount;
	std::cout << "Box copy constructor called." << std::endl;
}

Box::~Box()                                     // Destructor
{
	std::cout << "Box destructor called." << std::endl;
	--objectCount;
}

// Function to calculate the volume of a box
double Box::volume() const
{
	return length * width*height;
}


int main()
{
	std::cout << "There are now " << Box::getObjectCount() << " Box objects." << std::endl;

	const Box box1{ 2.0, 3.0, 4.0 };                                // An arbitrary box
	Box box2{ 5.0 };                                                // A box that is a cube

	std::cout << "There are now " << Box::getObjectCount() << " Box objects." << std::endl;

	for (double d{}; d < 3.0; ++d)
	{
		Box box{ d, d + 1.0, d + 2.0 };
		std::cout << "Box volume is " << box.volume() << std::endl;
	}

	std::cout << "There are now " << Box::getObjectCount() << " Box objects." << std::endl;

	auto pBox = std::make_unique<Box>(1.5, 2.5, 3.5);
	std::cout << "Box volume is " << pBox->volume() << std::endl;
	std::cout << "There are now " << pBox->getObjectCount() << " Box objects." << std::endl;
}

Members of a class can be specified as public, in which case they are freely accessible from any function in a program.Alternatively, they can be specified as private, in which case they may be accessed only by member functions, friend functions of the class, or members of nested classes.

#include <cstdlib>                     // For random number generation
#include <ctime>                       // For the std::time() function
#include <iostream>
// Function to generate a random integer 1 to 100

inline double random()
{
	return 1.0 + rand() / (RAND_MAX / 100 + 1);
}



int main()

{
	std::srand((unsigned)std::time(0));  // Initialize the random number generator

	int count{};
	std::cout << "Do you want to generate a number from 1 to 100? Answer Y to continue" << std::endl;
	char answer{};

	std::cin >> answer;

	while (answer == 'Y')
	{
		std::cout << random() << std::endl;
		std::cout << "Do you want to generate a number from 1 to 100? Answer Y to continue" << std::endl;
		std::cin >> answer;
	}
}

 

《Beginning C++17》-学习笔记-Chapter 07-Working with Strings #include &lt;iostream&gt; #include &lt;string&gt;//This header of the C++ Standard Library defines the std::string type /*Type string is a compound type, which is a type that’s a composite of severa... 阅读详情

相关推荐

Beginning Visual C++ 2008

Ivor Horton’s Beginning Visual C++®2008

C++ 现代编程(一)

C++是最广泛使用的编程语言之一。它快速,灵活,高效,用于解决许多编程问题。这个学习路径的目标是使您熟悉并熟悉 C++。通过学习语言结构,函数和类,您将熟悉 C++编程的构造,这将帮助您识别代码中的执行流程。您将探索并了解 C++标准库的重要性,以及为编写更好,更快的程序而进行的内存分配。这个学习路径还涉及理解高级 C++编程所面临的挑战。您将学习高级主题,如多线程,网络,并发性,性能,元编程,lambda 表达式,正则表达式,测试等,以食谱的形式。通过这个学习路径的结束,您将成为 C++的专家。

龙哥盟 1491

Ivor Horton’s Beginning Visual C++ 2010 英文版 PDF 超清晰版

Product Description The leading author of programming tutorials for beginners introduces you to Visual C++ 2010 Ivor Horton is the preeminent author of introductory programming language tutorials; previous editions of his Beginning Visual C++ have sold nearly 100,000 copies. This book is a comprehensive introduction to both the Standard C++ language and to Visual C++ 2010; no previous programming experience is required. All aspects of the 2010 release are covered, including changes to the language and the C++ standard.. Microsoft Visual C++ is one of the most popular C++ development environments and compilers, used by hundreds of thousands of developers Ivor Horton's approach to programming tutorials has achieved a huge following; this book gives beginning programmers a comprehensive introduction to both Standard C++ and Visual C++ 2010 Covers all the language changes in Visual C++ 2010, library additions, new MFC features, changes in the Visual Studio development environment, and more Also includes a brief introduction to programming for multicore processors in native C++ and C++/CLR processors Nearly 100,000 copies of this book have been sold in previous editions Beginners seeking a complete education in Visual C++ will find everything they need in Ivor Horton's Beginning Visual C++ 2010. Note: CD-ROM/DVD and other supplementary materials are not included as part of eBook file. From the Back Cover Build real-world applications as you dive into C++ development By following author Ivor Horton's accessible tutorial approach and detailed examples you can quickly become an effective C++ programmer. Thoroughly updated for the 2010 release, this book introduces you to the latest development environment and teaches you how to build real-world applications using Visual C++. With this book by your side, you are well on your way to writing applications in both versions of C++ and becoming a successful C++ programmer. Ivor Horton's Beginning Visual C++ 2010: Teac

Beginning Visual C++ 2005 之一

阅读记录而已。 为什么要读2005呢?第一,本人非常熟悉unix环境编程,项目需要转到了windows。第二,本人貌似要使用vs2005,所以就看vc2005吧。 下为记录: 1、VC支持native C++和C++/CLI,后者是运行在CLR下面。 2、进行windows C++编程的话,可以完全只用native c++直接操作API,或者采用MFC来操作API,也可以用Form

chn89的专栏 698

基于统一环境建模与可行性约束一致化的三维路径规划A星RRT星与粒子群优化对比研究(Matlab代码实现)

内容概要:本文系统研究了基于统一环境建模与可行性约束一致化的三维路径规划问题,对A*算法、RRT*算法与粒子群优化(PSO)算法在三维空间中的路径规划性能进行了深入对比分析。通过构建标准化的仿真环境与统一的可行性约束条件,从路径长度、规划耗时、避障能力及路径平滑性等多个维度对三种算法进行定量评估,全面揭示其各自的优势与局限性。研究旨在为不同应用场景下的路径规划算法选型提供科学依据与技术指导,并配套提供了完整的Matlab代码实现,便于读者复现结果与进一步研究。; 适合人群:具备一定算法基础和Matlab编程能力,从事机器人、自动驾驶、无人机或智能系统等相关领域研究的研发人员及研究生。; 使用场景及目标:① 掌握A*、RRT*、PSO等主流路径规划算法的核心思想与实现细节;② 学习如何在统一标准下对不同算法进行公平、有效的性能对比与评估;③ 获取可直接用于科研或项目开发的三维路径规划Matlab代码框架。; 阅读建议:建议读者在阅读过程中结合提供的Matlab代码进行实践操作,通过修改环境参数和算法配置来观察不同算法的表现差异,从而加深对算法原理与适用场景的理解。

用VINS-Fusion跑通EuRoC数据集:Ubuntu22.04+ROS2环境下的保姆级评测指南

本文详细介绍了在Ubuntu22.04和ROS2 Humble环境下使用VINS-Fusion处理EuRoC数据集的完整流程。从环境配置、数据集转换到参数调优和性能评估,提供了一套全面的多传感器融合分析方案,帮助开发者在无人机精准定位场景中实现高效部署与优化。

weixin_29212867的博客 524

Ivor Horton's Beginning Visual C++ 2010

Ivor Horton's Beginning Visual C++ 2010 转载于:https://www.cnblogs.com/gavinhughhu/archive/2010/05/12/1733142.html

weixin_30311605的博客 276

ai数字人系统部署

这段代码实现了基本的语音交互闭环,用户通过语音输入问题,数字人识别语音后进行自然语言理解并生成回复,再将回复转换为语音输出。但实际应用中的 AI 数字人系统更为复杂,如形象渲染需专业图形引擎(如 Unity、Unreal Engine),自然语言处理也会涉及更复杂的模型微调及多轮对话管理等。一个完整的 AI 数字人系统涵盖形象生成、自然语言处理、动作控制等多个复杂功能,下面为你提供一个 Python 示例,整合多个库来实现数字人基础功能,包括语音交互、简单的自然语言理解以及通过虚拟形象展示对话。

weixin_ggwwsscc的博客 378

Ivor Horton‘s Beginning Visual C++ 2013 - Visual C++ 2013 入门经典

Ivor Horton‘s Beginning Visual C++ 2013 - Visual C++ 2013 入门经典

程永强 1037

SAP软件外币评估系统配置与操作

文章摘要:本文详细介绍了SAP系统中外币评估的流程、策略及系统操作。外币评估是期末关账的重要环节,主要针对外币未清项目及资产/负债进行汇率重估,生成评估差异过账以反映真实财务状况。文中阐述了三种评估策略(不评估、增量评估、期末评估冲回)及适用场景,并提供了SAP系统外币评估的具体操作步骤(FAGL_FCV/FAGL_FC_VAL事务码)。同时,讲解了货币资金科目和未清项管理科目的评估差异处理方式,以及相关的系统配置(OB59、OBA1等)。文章还区分了已实现和未实现汇兑损益的会计处理,为企业外币业务管理提供

weixin_40672823的博客 1978

NOAA气象数据下载攻略

各方搜寻气象数据下载途径,发现网上最多的就是从NOAA下载,但是中途有尝试过很多次都没有下载到,每次都是显示不可用,今天终于下载成功了,特此记录。 首先我是从这个网址进入的:National Centers for Environmental Information (NCEI) formerly known as National Climatic Data Center (NCDC) | NCEI offers access to the most significant archives of oc

lianqian66的博客 3万+

《C++ Without Fear》学习笔记

书名:C++ Without Fear A Beginner’s Guide That Makes You Feel Smart 作者:Brian Overland 阅读时间:2018/10/6-2018/10/20 书摘+书评: An important rule of good programming style is that variables should usually...

CodingIsFun 832

AI学习记录 - transformer的位置编码的理解

先看三角函数为什么可以,我们看下三角函数的图像先提一个点:位置信息肯定是唯一的,或者是在很长的文字当中,起码重复度不会太高,x轴代表位置,y轴代表叠加上去的值1、假设只使用一个三角函数sin,我们将第2和第14两个位置输入到sin,他们的值很像,在神经网络的训练中,很容易把第2个位置和第14个位置当成是一个位置,不满足我们上面说到的要求位置唯一性。sin2sin14。

weixin_43954090的博客 1425

《Beginning C++17》-学习笔记-Chapter 04-Making Decisions

There are only two possible bool values, true and false. True and false are keywords and are literals of type bool. They are sometimes called Boolean literals. #include &lt;iostream&gt; int main() ...

CodingIsFun 492

化工设备应力分析软件:STAAD.Pro二次开发_(24).非线性分析技术

在STAAD.Pro 中,可以通过命令。

kkchenjj的博客 667

深入详细分析Skyworks 78113 PAMiD射频方案图

是一种高集成度的射频前端模块,它结合了功率放大器(PA)、双工器(Duplexer)、滤波器和开关等多个组件,主要用于4G LTE移动通信系统中的射频前端链路。是一款典型的PAMiD模块,通过将多个频段的发射(TX)和接收(RX)路径集成在一个模块中,大大减少了传统射频设计中的分立器件,同时提高了信号链路的效率和可靠性。模块化设计的优势PCB空间优化:高集成度设计大幅减小了射频电路的尺寸,适用于紧凑型移动设备。减少设计复杂度:模块化设计减少了器件间的匹配与调试工作,加速产品开发周期。性能稳定性。

weixin_43199439的博客 1490

《Beginning C++17》-学习笔记-Chapter 09-Functions Templates

// Using a function template #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; template&lt;typename T&gt; T larger(T a, T b); // Function template prototype // a speciali...

CodingIsFun 458

高通平台蓝牙学习-- 基于WCN6750 Bluetooth 5.2的讲解

摘要:蓝牙5.2解决方案WCN6750具备多项先进特性:完全兼容5.2标准及ANT+协议,支持2Mbps高速BLE和长距离传输;采用射频集成方案,与WLAN共享天线并支持5G并发;内置功率放大器,支持Class1/2传输;提供双音频流和Slimbus接口优化。其HCI UART接口支持3.2Mbps速率,通过BlueZ协议栈实现蓝牙5.2功能,包括2Mbps模式和长距离传输,符合核心规范要求。(149字)

专业开发者的博客,一个从事Android WiFi和蓝牙的工程师 3万+

《Beginning C++17》-学习笔记-Chapter 08-Defining Functions

/*A function with a return type other than void must return a value of the type specified in the function header. The only exception to this rule is the main() function, where reaching the closing bra...

CodingIsFun 457

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

《Beginning C++17》-学习笔记-Chapter 05-Arrays and Loops

The size of an array must always be specified using a constant integer expression. Any integer expression that the compiler can evaluate at compile time may be used, though mostly this will be either ...

CodingIsFun 431

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

《Beginning C++17》-学习笔记-Chapter 10-Program Files and Preprocessing Directives

#include "..\Power.h" /*including the file called Power.h located in the folder that includes the foler that includes this file.*/ Each source file, along with the contents of all the header files ...

CodingIsFun 425

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

《Beginning C++17》-学习笔记-Chapter 06-Pointers and References

#include &lt;iostream&gt; int main() { long* pnumber1{};//the statement initializes pnumber1 with the pointer equivalent of zero, which is a special address that doesn't point to anything. long* p...

CodingIsFun 396

《Beginning C++17》-学习笔记-Chapter 02-Introducing Fundamental Types of Data

#include <iostream> int main() { //int apple_count{ 15.5 }; /*此行会导致编译错误。错误类型是narrowing conversion。*/ int apple_count{ 15 }; /*第一种赋值形式;The braces enclosing the initial value are called a bra...

CodingIsFun 370

《Beginning C++17》-学习笔记-Chapter 03-Working with Fundamental Data Types

The XOR operator is used less frequently than the &amp; and | operators. Important applications arise, however, in for instance cryptography, random number generation, and computer graphics. XOR is al...

CodingIsFun 317
上一篇: 《Beginning C++17》-学习笔记-Chapter 10-Program Files and Preprocessing Directives
下一篇: 软件行业从业人员言论收集01
CodingIsFun
博客等级 码龄8年 37粉丝 240原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址