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. 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:
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
May be interested
- Storage class in C programminga storage class defines the scope (visibility) and the lifetime of a variable or / and functions in the c program. the preceding type specifications can be changed. here are the storage classes that can be used in the c program.
- Instructions for creating mouse pointer highlights on Windowsthe cursor highlighter application will create a highlight effect for the mouse cursor on your computer, highlighting the cursor, making it easier for you and your viewers to follow during presentations, tutorials and live streams.
- A brand new AI cursor is coming to Windows 11according to the plan, microsoft will announce 'ai explorer' along with pcs running windows 11 ai on may 21. a recent leak shows a new cursor designed specifically for ai actions.
- Steps to customize the cursor with Custom Cursor in Google Chromethe google chrome browser does not have a built-in option to change the cursor icon, but users can use third-party integrations to set it up.
- Interface in C ++ (Abstract class)an interface describes the behavior or capabilities of a class in c ++ without signing to a specific implementation of that class.
- Cursor in Cpointer in the c language is easy to learn. some tasks in c language are made easier by pointers, and other tasks become more flexible, such as in memory allocation, which cannot be performed without using a cursor.
- How to Write a Class in Pythonin python, classes can help to wrap up data and functionality at the same time. several classes have already been written for us in python 3, called builtins. here are a few: int (integer class), str (string class), list (list class). this...
- Contructor and Destructor in C ++a contructor class is a special member function of a class that is executed whenever we create new objects of that class.
- Learn Class and Object in Pythonpython is an object-oriented programming language. unlike procedural programming, which emphasizes functions, object-oriented programming focuses on objects. this article quantrimang will explore class and object in depth, inviting readers to follow.
- Class member functions in C ++a member function of a class is a function that has its definition or prototype inside the class definition like any other variable. it works on any object of the class that is a member, and has access to all members of a class for that object.