Help with these C++ pointer questions and an implementation of the single-argume
ID: 3813095 • Letter: H
Question
Help with these C++ pointer questions
and an implementation of the single-argument constructor:
Question 1:
Using pointers to the implicit parameter, we know that the parameter can have the same name as the instance variable.
Complete the constructor such that the instance variable x takes on the value of the parameter, now also called x. "x = x;" won't work because now that we're calling a parameter of the constructor x, x now refers ONLY to the parameter, not the instance variable with the same name. On the left side of the assignment, you need to specifically access the implicit parameter's member x.
Question 2:
What if the prototype for the member function were:
Then the set_opponent method takes in an entire Zeta object rather than a Zeta* pointer, yet we still want to store only the memory address of that Zeta object in our instance variable opponent, becauseopponent is still a pointer (not an object). Finish the code below:
Explanation / Answer
Question 1:
Zeta::Zeta(int x){
// enter the code to replace this line
this->x = x;// this is a pointer in C++, which holds the memory address of the current object. We can access the current object var using this.
// -> is a arrow operator
}
Question 2:
void Zeta::Zeta(Zeta p){
// enter the code to replace this line
Zeta *opponent = &p; //now the instance variable opponent stores the address of the object p
}
Note: Feel free to ask any question in case of any doubt. God belss you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.