Need to verify my code. Using C++. I am making use of inheritance. And need to o
ID: 3578060 • Letter: N
Question
Need to verify my code. Using C++. I am making use of inheritance. And need to override the function in the Base class. Do not need the code for the main. Nor code for the parent class. Just for the child class
Here's the instructions:
Given a class “SmartPhone” that contains a method “int getNumberOfBars()”, write a class called “IPhone” that inherits from SmartPhone. IPhones will have a property called “int barStrength”. Write the code for the default constructor which uses the SmartPhone default constructor followed by setting the barStrength to 1. Then, override the getNumberOfBars method that simply returns a value of 5. After that, overload the getNumberOfBars method that takes the barStrength as an input parameter and returns that value as the number of bars.
Explanation / Answer
class IPhone extends SmartPhone{
int barStrength;
public IPhone()
{
super(); //calls the base class constructor
this.barStrength = 1;
}
//Overriding method (just assumed there are no parameters to it as nothing is given in description
int getNumberOfBars()
{
return 5;
}
//OverLoading method
int getNumberOfBars(int barStrength)
{
int numberOfBars;
numberOfBars = barStrength;
return numberOfBars;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.