Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370671
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-19 09:17:09

Format:
  1. class class_name {
  2.   access_specifier_1:
  3.     member1;
  4.   access_specifier_2:
  5.     member2;
  6.   ...
  7.   } 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:
  1. #include <iostream>
  2. using namespace std;

  3. class CRectangle {
  4.                 int x, y;
  5.         public:
  6.                 void set_values (int, int);
  7.                 int area() {return (x*y);}
  8. };

  9. void
  10. CRectangle::set_values (int a, int b)
  11. {
  12.         x = a;
  13.         y = b;
  14. }

  15. int
  16. main(void)
  17. {
  18.         CRectangle rect;
  19.         rect.set_values(3,4);
  20.         cout << "area:" << rect.area() << endl;

  21.         return (0);
  22. }
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:
  1. #include <iostream>
  2. using namespace std;

  3. class CRectange {
  4.                 int width, height;
  5.         public:
  6.                 CRectange (int, int);
  7.                 int area() {return (width*height);}
  8. };

  9. CRectange::CRectange(int a, int b)
  10. {
  11.         width = a;
  12.         height = b;
  13. }

  14. int
  15. main(void)
  16. {
  17.         CRectange recta (3,4);
  18.         CRectange rectb (5,6);
  19.         cout << "recta area: " << recta.area() << endl;
  20.         cout << "rectb area: " << rectb.area() << endl;

  21.         return (0);
  22. }
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:
  1. #include <iostream>
  2. using namespace std;

  3. class CRectangle {
  4.                 int *width, *height;
  5.         public:
  6.                 CRectangle (int, int);
  7.                 ~CRectangle ();
  8.                 int area () {return (*width * *height);}
  9. };

  10. CRectangle::CRectangle(int a, int b)
  11. {
  12.         width = new int;
  13.         height = new int;
  14.         *width = a;
  15.         *height = b;
  16. }

  17. CRectangle::~CRectangle()
  18. {
  19.         delete width;
  20.         delete height;
  21. }

  22. int
  23. main(void)
  24. {
  25.         CRectangle recta (3,4), rectb (5,6);
  26.         cout << "recta area: " << recta.area() << endl;
  27.         cout << "rectb area: " << rectb.area() << endl;

  28.         return (0);
  29. }
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:
  1. #include <iostream>
  2. using namespace std;

  3. class CRectangle {
  4.                 int width, height;
  5.         public:
  6.                 CRectangle ();
  7.                 CRectangle (int, int);
  8.                 int area () {return (width * height);}
  9. };

  10. CRectangle::CRectangle()
  11. {
  12.         width = 5;
  13.         height = 5;
  14. }

  15. CRectangle::CRectangle(int a, int b)
  16. {
  17.         width = a;
  18.         height = b;
  19. }

  20. int
  21. main(void)
  22. {
  23.         CRectangle rect (3,4);
  24.         CRectangle rectb;
  25.         cout << "rect area: " << rect.area() << endl;
  26.         cout << "rectb area: " << rectb.area() << endl;

  27.         return (0);
  28. }
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) { /*...*/ }

  1. #include <iostream>
  2. using namespace std;

  3. class CVector {
  4.         public:
  5.                 int x, y;
  6.                 CVector () {};
  7.                 CVector (int, int);
  8.                 CVector operator + (CVector);
  9. };

  10. CVector::CVector(int a, int b)
  11. {
  12.         x = a;
  13.         y = b;
  14. }

  15. CVector CVector::operator+ (CVector param)
  16. {
  17.         CVector temp;
  18.         temp.x = x + param.x;
  19.         temp.y = y + param.y;

  20.         return (temp);
  21. }

  22. int
  23. main(void)
  24. {
  25.         CVector a(3,1);
  26.         CVector b(1,2);
  27.         CVector c;
  28.         c = a + b;

  29.         cout << c.x << "," << c.y << endl;

  30.         return (0);
  31. }




This:
  1. #include <iostream>
  2. using namespace std;

  3. class CDummy {
  4.         public:
  5.                 int isitme (CDummy &param);
  6. };

  7. int CDummy::isitme(CDummy &param)
  8. {
  9.         if (&param == this)
  10.                 return true;
  11.         else
  12.                 return false;
  13. }

  14. int
  15. main(void)
  16. {
  17.         CDummy a;
  18.         CDummy *b;
  19.         b = &a;
  20.         if (b->isitme(a))
  21.                 cout << "yes, &a is b" << endl;

  22.         return (0);
  23. }
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:
  1. #include <iostream>
  2. using namespace std;

  3. class CDumy {
  4.         public:
  5.                 static int n;
  6.                 CDumy () {n++;};
  7.                 ~CDumy () {n--;}
  8. };

  9. int CDumy::n = 0;

  10. int
  11. main(void)
  12. {
  13.         CDumy a;
  14.         CDumy b[5];
  15.         CDumy *c = new CDumy;
  16.         cout << a.n << endl;
  17.         delete c;
  18.         cout << CDumy::n << endl;

  19.         return (0);
  20. }
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.



阅读(1254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~