close
In C++, classes are very much like structs, except that classes provide much more power and flexibility.
In C++, when we declare a variable of a class, we call it instantiating the class. The variable itself is called an instance of the class. A variable of a class type is also called an object.
In addition to holding data, classes can also contain functions! Here is our Date class with a function to set the date:
class
Date
{
public
:
int
m_nMonth;
int
m_nDay;
int
m_nYear;
void
SetDate(
int
nMonth,
int
nDay,
int
nYear)
{
m_nMonth = nMonth;
m_nDay = nDay;
m_nYear = nYear;
}
};
The function is called inline function because it is decined in the class.
We can also define the body of function outside the class as follow :
class
Date
{
public
:
int
m_nMonth;
int
m_nDay;
int
m_nYear;
void
SetDate(
int
nMonth,
int
nDay,
int
nYear);
};
void
Date::SetDate(
int
nMonth,
int
nDay,
int
nYear)
{
m_nMonth = nMonth;
m_nDay = nDay;
m_nYear = nYear;
}
ref : http://www.learncpp.com/cpp-tutorial/82-classes-and-class-members/
全站熱搜