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

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;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值