Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Homework Questions 1)There is ambiguity in the variable names in the following function that sets the private instance variables width and height to the

C++ Homework Questions

1)There is ambiguity in the variable names in the following function that sets the private instance variables width and height to the incoming parameter values:

bool Rectangle::setWidthHeight(double width, double height) { if ( width <= 0 || height <= 0 ) { // don't allow negative width or height return false; } else { width = width; // oops! height = height; return true; } } 

Which of the following should you use to correct this?

a)
bool Rectangle::setWidthHeight(double width, double height) { if ( width <= 0 || height <= 0 ) { // don't allow negative width or height return false; } else { this->width = width; this->height = height; return true; } } 
b)
bool Rectangle::setWidthHeight(double width, double height) { if ( width <= 0 || height <= 0 ) { // don't allow negative width or height return false; } else { width = this->width; height = this->height; return true; } } 
c)
bool Rectangle::setWidthHeight(double width, double height) { if ( width <= 0 || height <= 0 ) { // don't allow negative width or height return false; } else { this->width = that->width; this->height = that->height; return true; } }

3)Which of the following is a static function of the Dog class?

a)
int main() { cout << Dog::getPopulation() << endl; }
b)
int main() { Dog fido; cout << fido.getBreed() << endl; }
c)
int main() { cout getSize() << endl; }

4)These are the constructors for the Galaxy class:

Galaxy::Galaxy(string myName) { if (myName.length() > 0) name = myName; else name = "undefined"; magnitude = 0.0; } Galaxy::Galaxy(string myName, double myMag) { if (myName.length() > 1) name = myName; else name = "undefined"; if (myMag >= -3 && myMag <= 30) magnitude = myMag; else magnitude = 0.0; } 

5)Which of the following object instantiation statements are valid for this class? (Select all that apply)

a)
Galaxy gal0;
b)
Galaxy gal1("M31", 8.9), gal2("Andromeda", 3.5); 
c)
Galaxy gal1("M31"); 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions