Understanding Object Pointers in C++

Creating Pointer to Chicken Object

Read two doubles as the age and the weight of a chicken object. Declare and assign pointer mychicken with a new chicken object using the age and the weight as arguments in that order.



a) Chicken* mychicken = new Chicken(age, weight);

b) Chicken* mychicken = new Chicken(weight, age);

c) Chicken* mychicken = new Chicken(chicken, age, weight);

d) Chicken* mychicken = new Chicken(age);

Final answer:

The correct statement to declare and assign pointer mychicken with a new chicken object using the age and the weight as arguments in that order is option a) Chicken* mychicken = new Chicken(age, weight);

Explanation:

The correct statement to declare and assign pointer mychicken with a new chicken object using the age and the weight as arguments in that order is option a) Chicken* mychicken = new Chicken(age, weight);

When creating a new object, the arguments passed should match the order specified in the constructor definition. In this case, the constructor for the Chicken class takes the age as the first argument and the weight as the second argument.

Therefore, the correct syntax to create a new chicken object with the age and weight as arguments is Chicken* mychicken = new Chicken(age, weight);

Do you understand how to create a pointer to a Chicken object in C++?

Yes, to create a pointer to a Chicken object in C++, you need to correctly use the new operator with the appropriate constructor arguments in the declaration statement.

← Principle of inclusion exclusion solving the programmer puzzle Constructing data mart vs data warehouse which is faster and cheaper →