Cursor to class in C ++
A pointer to a class in C ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in C ++ as the -> operator, as you do with pointers to the structure.
A pointer to a class in C ++ is executed in the same way as a pointer to a structure; and to access members of a pointer to a class you use the member access operator in C ++ as the -> operator, as you do with pointers to the structure. As with all pointers, you must declare the cursor before using it.
Try the following example to understand the concept of a pointer to a class in C ++:
#include using namespace std ; class Box { public : // phan dinh nghia Constructor Box ( double dai = 1.0 , double rong = 1.0 , double cao = 1.0 ) { cout << "Constructor duoc goi." << endl ; chieudai = dai ; chieurong = rong ; chieucao = cao ; } double theTich () { return chieudai * chieurong * chieucao ; } private : double chieudai ; // chieu dai cua mot box double chieurong ; // chieu rong cua mot box double chieucao ; // chieu cao cua mot box }; int main ( void ) { Box Box1 ( 2.4 , 4.2 , 2.2 ); // khai bao box1 Box Box2 ( 4.5 , 2.0 , 3.2 ); // khai bao box2 Box * controBox ; // khai bao con tro toi mot class. // luu giu dia chi cua doi tuong dau tien controBox = & Box1 ; // bay gio thu truy cap mot thanh vien boi su dung toan tu truy cap thanh vien cout << "The tich cua Box1 la: " << controBox -> theTich () << endl ; // luu giu dia chi cua doi tuong thu hai controBox = & Box2 ; // bay gio thu truy cap mot thanh vien boi su dung toan tu truy cap thanh vien cout << "The tich cua Box2 la: " << controBox -> theTich () << endl ; return 0 ; }
Compiling and running the above C ++ program will produce the following results:
Cursor to class in C ++ Picture 1
According to Tutorialspoint
Previous article: Cursor this in C ++
Next lesson: Static member of class in C ++
4.5 ★ | 2 Vote
You should read it
- Pointers to pointers in C ++
- Cursor in C / C ++
- Array of pointers in C ++
- How to Create Pointers in C
- Cursor in C
- Cursor NULL in C / C ++
- Cursor and Array in C ++
- How to Do a Presentation in Class
- Arithmetic pointer in C / C ++
- How to Check Null in C
- How to keep the default desktop icons and mouse pointer when changing the Windows 11 theme
- Access Modifier for class in C ++