Reference: I Horton, Ivor Horton's Beginning Visual C++ 2013 (Wiley 2014).
7.1 The Struct in C++
∎ Struct
// Struct definition
struct Rectangle
{
int left;
int top;
int right;
int bottom;
};
7.2 Types, Classes, Objects, and Instances
∎ Class
// Definition of a class
class CBox
{
public:
double m_length; // Data members
double m_width;
double m_height;
};
// Main.cpp
CBox box1; // Declare object box1 of type CBox
CBox box2;
if (box1 > box2) // ">" is an operator acting on the objects
box1.fill(); // "fill()" is a member function acting on object 1
else
box2.fill();
7.3 Understanding Classes
∎ Accessing the data members of a class
// Main.cpp
CBox box1;
CBox box2;
box1.m_length = box1.m_width = box1.m_height = 2.0;
box2 = box1; // Member-wise copying from box1 to box2
∎ Adding a member function to CBox
// Member function
double volume()
{
return m_length * m_width * m_height;
}
7.4 Class Constructors
∎ Adding a Constructor to the CBox class
// Constructor definition
CBox(double lv, double wv, double hv)
{
cout << "Constructor called." << endl;
m_length = lv; // Set values of data members
m_width = wv;
m_height = hv;
}
∎ Default constructor
// Default constructor definition
CBox()
{
cout << "Default constructor called." << endl;
}
∎ Constructor initialization list
// Constructor definition
CBox(double lv = 1.0, double wv = 1.0, double hv = 1.0) :
m_length {lv}, m_width{wv}, m_height{hv}
{
cout << "Constructor called." << endl;
}
cf. CCandyBox(double lv, double wv, double hv, const char* str = "Candy") :
: CBox {lv, wv, hv} // In the inherited class CCandyBox
∎ Making a constructor explicit
// Make a constructor more specific, instead of generalCBox box;
box = 99.0;
CBox(double side) : m_Length {side}, m_Width {side}, m_Height {side} {} // Will compile
explicit CBox(double side) : m_Length {side}, m_Width {side}, m_Height {side} {} // Will not, unless CBox box {4.0} is used
7.5 Private Members of a Class
∎ Private data members
// CBox.h
class CBox
{
private:
double m_length;
double m_width;
double m_height;
};
// Main.cpp
box1.m_length = 4.0; // Error message: cannot access private data member declared in class CBox
∎ Friend function of a class
// Friend function prototype (in CBox.h)
friend double boxSurface(const CBox& aBox);
// A function that is not a member of the class, but with special privilege
double boxSurface(const CBox& aBox)
{
return 2.0 * (aBox.m_length * aBox.m_width + aBox.m_width * aBox.m_height
+ aBox.m_height * aBox.m_length);
}
∎ Copying information between instances
// Initialize box2 with box1 (done by the default copy constructor)
CBox box2{box1};
// .h
CBox (const CBox& initB);
// .cpp
CBox::CBox(const CBox& initB):
m_length{initB.m_length}, m_width{initB.m_width}, m_height{initB.m_height}
7.6 The Pointer This
∎ Explicit use of this
// main.cpp
if (cigar.compare(match))
cout << "cigar is larger than match" << endl;
else
cout << "cigar is not larger than match" << endl;
// .cpp
bool compare(CBox& aBox)
{
return this->volume() > aBox.volume();
}
7.7 Const Objects
∎ Constant member functions of a class
// Non-const object
CBox cigar{8.0, 5.0, 1.0};
bool CBox::compare(CBox& aBox)
// const object
const CBox cigar{8.0, 5.0, 1.0};
bool CBox::compare(const CBox& aBox) const
∎ Member function definitions outside the class
7.8 Array of Objects
7.9 Static Members of a Class
∎ Counting instances of a class type (ex7_12)
static int objectCount; // Count of objects in existence
int CBox:: objectCount{}; // Initialize static data member of CBox class; no keyword "static"
7.10 Pointers and References to Objects
7.11 Summary
沒有留言:
張貼留言