#include "house.h" int main(){ house new_house (“Henan”, 3, 2019); house* ptr_to_house = nullptr; ptr_to_house = &new_house; return 0; }
时间: 2024-01-16 11:05:20 浏览: 51
The code you provided creates a new instance of the `house` class with the location "Henan", 3 rooms, and built in 2019. It then creates a pointer `ptr_to_house` and initializes it to `nullptr`. Finally, it assigns the address of `new_house` to `ptr_to_house`.
However, this code does not do anything with the `ptr_to_house` pointer. If you want to use the pointer to call the `print_house_details()` function, you would need to add that code. Here's an example of how to call the function using the pointer:
```
#include "house.h"
int main() {
// Create a new house object
house new_house("Henan", 3, 2019);
// Create a pointer to the house object
house* ptr_to_house = nullptr;
ptr_to_house = &new_house;
// Call the print_house_details function using the pointer
ptr_to_house->print_house_details();
return 0;
}
```
In this example, we call the `print_house_details()` function using the `ptr_to_house` pointer and the arrow operator `->`. This will output:
```
Location: Henan
Number of rooms: 3
Year built: 2019
```
阅读全文