138k views
4 votes
ageValue and weightValue are read from input. Declare and assign pointer myDonkey with a new Donkey object. Then, set myDonkey's age and weight to ageValue and weightValue, respectively. Ex: if the input is 22 189, then the output is: Donkey's age: 22 Donkey's weight: 189 #include using namespace std; class Donkey { public: Donkey(); void Print(); int age; int weight; }; Donkey::Donkey() { age = 0; weight = 0; } void Donkey::Print() { cout << "Donkey's age: " << age << endl; cout << "Donkey's weight: " << weight << endl; } int main() { int ageValue; int weightValue; // Additional variable declarations go here cin >> ageValue; cin >> weightValue; // Your code goes here Donkey* myDonkey = new Donkey(); myDonkey->age = ageValue; myDonkey->weight = weightValue; myDonkey->Print(); return 0; }

User Nkaenzig
by
7.9k points

1 Answer

3 votes

Final answer:

The code provided demonstrates how to dynamically allocate a new Donkey object, assign it to the pointer myDonkey, and set its age and weight properties according to input values before printing this information.

Step-by-step explanation:

To declare and assign a pointer myDonkey with a new Donkey object, you will first need to create the pointer and then use the new keyword to allocate memory for a Donkey object. After this, you can set the age and weight of myDonkey using the input values ageValue and weightValue. Finally, the Print() method of the Donkey object is called to output the age and weight values.

Here is the code that accomplishes the above:

Donkey* myDonkey = new Donkey();

myDonkey->age = ageValue;

myDonkey->weight = weightValue;

myDonkey->Print();

By using pointers and object-oriented concepts, you correctly implement dynamic memory allocation and assign the Donkey's age and weight to the respective values read from the input. This code will produce the desired output, displaying the age and weight of the donkey.

User Hippo Fish
by
7.4k points