Format:
- class class_name {
-
access_specifier_1:
-
member1;
-
access_specifier_2:
-
member2;
-
...
-
} object_names;
Access specifier:
- private members of a class are accessible only from within other members of the same class or from their friends.
- protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
- Finally, public members are accessible from anywhere where the object is visible.
By
default, all members of a class declared with the class keyword have
private access for all its members.
Example:
- #include <iostream>
-
using namespace std;
-
-
class CRectangle {
-
int x, y;
-
public:
-
void set_values (int, int);
-
int area() {return (x*y);}
-
};
-
-
void
-
CRectangle::set_values (int a, int b)
-
{
-
x = a;
-
y = b;
-
}
-
-
int
-
main(void)
-
{
-
CRectangle rect;
-
rect.set_values(3,4);
-
cout << "area:" << rect.area() << endl;
-
-
return (0);
-
}
The only difference between defining a class member function completely
within its class or to include only the prototype and later its
definition, is that in the first case the function will automatically be
considered an
inline member function by the compiler, while in the
second it will be a normal (not-inline) class member function, which in
fact supposes no difference in behavior.
Constructor:
- #include <iostream>
-
using namespace std;
-
-
class CRectange {
-
int width, height;
-
public:
-
CRectange (int, int);
-
int area() {return (width*height);}
-
};
-
-
CRectange::CRectange(int a, int b)
-
{
-
width = a;
-
height = b;
-
}
-
-
int
-
main(void)
-
{
-
CRectange recta (3,4);
-
CRectange rectb (5,6);
-
cout << "recta area: " << recta.area() << endl;
-
cout << "rectb area: " << rectb.area() << endl;
-
-
return (0);
-
}
Objects generally need to initialize variables or assign dynamic memory
during their process of creation to become operative and to avoid
returning unexpected values during their execution.
a class can include a special function called constructor,
which is automatically called whenever a
new object of this class is
created. This constructor function must have
the same name as the class,
and cannot have any return type; not even void.
Constructors cannot be called explicitly as if they were regular member
functions. They are only executed when a new object of that class is
created.
Destructor:
- #include <iostream>
-
using namespace std;
-
-
class CRectangle {
-
int *width, *height;
-
public:
-
CRectangle (int, int);
-
~CRectangle ();
-
int area () {return (*width * *height);}
-
};
-
-
CRectangle::CRectangle(int a, int b)
-
{
-
width = new int;
-
height = new int;
-
*width = a;
-
*height = b;
-
}
-
-
CRectangle::~CRectangle()
-
{
-
delete width;
-
delete height;
-
}
-
-
int
-
main(void)
-
{
-
CRectangle recta (3,4), rectb (5,6);
-
cout << "recta area: " << recta.area() << endl;
-
cout << "rectb area: " << rectb.area() << endl;
-
-
return (0);
-
}
The
destructor fulfills the opposite functionality. It is
automatically called when an object is destroyed, either because its
scope of existence has finished or because it is
an object dynamically assigned and it is released using the operator
delete.
The destructor must have the same name as the class, but preceded
with a tilde sign (~) and it must also return no value.Overload Constructor:
- #include <iostream>
-
using namespace std;
-
-
class CRectangle {
-
int width, height;
-
public:
-
CRectangle ();
-
CRectangle (int, int);
-
int area () {return (width * height);}
-
};
-
-
CRectangle::CRectangle()
-
{
-
width = 5;
-
height = 5;
-
}
-
-
CRectangle::CRectangle(int a, int b)
-
{
-
width = a;
-
height = b;
-
}
-
-
int
-
main(void)
-
{
-
CRectangle rect (3,4);
-
CRectangle rectb;
-
cout << "rect area: " << rect.area() << endl;
-
cout << "rectb area: " << rectb.area() << endl;
-
-
return (0);
-
}
a constructor can also be overloaded with more than one function that
have the same name but different types or number of parameters. Remember
that for overloaded functions the compiler will call the one whose
parameters match the arguments used in the function call.
Notice how if we declare a new object and we want to use its default
constructor (the one without parameters), we do not include parentheses ()
Default Constructor:
If you do not declare any constructors in a class definition, the
compiler assumes the class to have a default constructor with no
arguments.
But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor.
It provides three special member functions in total that are implicitly declared if you do not declare your own. These are the
copy constructor, the copy assignment operator, and the default destructor.Struct and union:
Classes can be defined not only with keyword class, but also with keywords struct and union.
The only difference between both is that members of classes declared with the keyword
struct have public access by default, while members of classes declared with the keyword
class have private access. For all other purposes both keywords are equivalent.
The concept of unions is different from that of classes declared with struct and class,
since unions
only store one data member at a time, but nevertheless
they are also classes and can thus also hold function members.
The
default access in union classes is public.
Overloading operator:
Overloadable operators |
---|
+ - * / = < > += -= *= /= << >>
<<= >>= == != <= >= ++ -- % & ^ ! |
~ &= ^= |= && || %= [] () , ->* -> new
delete new[] delete[]
|
The format is:
type operator sign (parameters) { /*...*/ }
- #include <iostream>
-
using namespace std;
-
-
class CVector {
-
public:
-
int x, y;
-
CVector () {};
-
CVector (int, int);
-
CVector operator + (CVector);
-
};
-
-
CVector::CVector(int a, int b)
-
{
-
x = a;
-
y = b;
-
}
-
-
CVector CVector::operator+ (CVector param)
-
{
-
CVector temp;
-
temp.x = x + param.x;
-
temp.y = y + param.y;
-
-
return (temp);
-
}
-
-
int
-
main(void)
-
{
-
CVector a(3,1);
-
CVector b(1,2);
-
CVector c;
-
c = a + b;
-
-
cout << c.x << "," << c.y << endl;
-
-
return (0);
-
}
This:
- #include <iostream>
-
using namespace std;
-
-
class CDummy {
-
public:
-
int isitme (CDummy ¶m);
-
};
-
-
int CDummy::isitme(CDummy ¶m)
-
{
-
if (¶m == this)
-
return true;
-
else
-
return false;
-
}
-
-
int
-
main(void)
-
{
-
CDummy a;
-
CDummy *b;
-
b = &a;
-
if (b->isitme(a))
-
cout << "yes, &a is b" << endl;
-
-
return (0);
-
}
The keyword this represents a pointer to the object whose member function is being executed. It is a pointer to the
object itself.
Static member:
- #include <iostream>
-
using namespace std;
-
-
class CDumy {
-
public:
-
static int n;
-
CDumy () {n++;};
-
~CDumy () {n--;}
-
};
-
-
int CDumy::n = 0;
-
-
int
-
main(void)
-
{
-
CDumy a;
-
CDumy b[5];
-
CDumy *c = new CDumy;
-
cout << a.n << endl;
-
delete c;
-
cout << CDumy::n << endl;
-
-
return (0);
-
}
Static data members of a class are also known as "
class variables",
because there is
only one unique value for all the objects of that same
class. Their content is not different from one object of this class to
another.
Just as we may include static data within a class, we can also include
static functions. They represent the same: they are global functions
that are called as if they were object members of a given class. They
can
only refer to static data, in no case to non-static members of the
class, as well as they
do not allow the use of the keyword this,
since it makes reference to an object pointer and these functions in
fact are not members of any object but direct members of the class.
阅读(1250) | 评论(0) | 转发(0) |