bent jenen مديرة منتديات ملتقى الابداع
عدد المساهمات : 78 السٌّمعَة : 0 نقاط : 0 تاريخ التسجيل : 25/02/2009
| موضوع: c++لغة برمجة مع بعض الامثلة الجزء الثالث السبت فبراير 28, 2009 5:07 am | |
| Here we have declared a constructor that takes two parameters of type int. Therefore the following object declaration would be correct: CExample ex (2,3); But, CExample ex; Would not be correct, since we have declared the class to have an explicit constructor, thus replacing the default constructor. But the compiler not only creates a default constructor for you if you do not specify your own. 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. The copy constructor and the copy assignment operator copy all the data contained in another object to the data members of the current object. For CExample, the copy constructor implicitly declared by the compiler would be something similar to: CExample::CExample (const CExample& rv) { a=rv.a; b=rv.b; c=rv.c; } Therefore, the two following object declarations would be correct: CExample ex (2,3); CExample ex2 (ex); // copy constructor (data copied from ex) Pointers to classes It is perfectly valid to create pointers that point to classes. We simply have to consider that once declared, a class becomes a valid type, so we can use the class name as the type for the pointer. For example: CRectangle * prect; is a pointer to an object of class CRectangle. As it happened with data structures, in order to refer directly to a member of an object pointed by a pointer we can use the arrow operator (->) of indirection. Here is an example with some possible combinations: // pointer to classes example #include <iostream> using namespace std;
class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} };
void CRectangle::set_values (int a, int b) { width = a; height = b; }
int main () { CRectangle a, *b, *c; CRectangle * d = new CRectangle[2]; b= new CRectangle; c= &a; a.set_values (1,2); b->set_values (3,4); d->set_values (5,6); d[1].set_values (7,; cout << "a area: " << a.area() << endl; cout << "*b area: " << b->area() << endl; cout << "*c area: " << c->area() << endl; cout << "d[0] area: " << d[0].area() << endl; cout << "d[1] area: " << d[1].area() << endl; delete[] d; delete b; return 0; } a area: 2 *b area: 12 *c area: 2 d[0] area: 30 d[1] area: 56 Next you have a summary on how can you read some pointer and class operators (*, &, ., ->, [ ]) that appear in the previous example: expression can be read as *x pointed by x &x address of x x.y member y of object x x->y member y of object pointed by x (*x).y member y of object pointed by x (equivalent to the previous one) x[0] first object pointed by x x[1] second object pointed by x x[n] (n+1)th object pointed by x Be sure that you understand the logic under all of these expressions before proceeding with the next sections. If you have doubts, read again this section and/or consult the previous sections about pointers and data structures. Classes defined with struct and union Classes can be defined not only with keyword class, but also with keywords struct and union. The concepts of class and data structure are so similar that both keywords (struct and class) can be used in C++ to declare classes (i.e. structs can also have function members in C++, not only data members). 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. Previous | |
|