Using C++. Please do the following: 3) Given a class “SmartPhone” that contains
ID: 3579879 • Letter: U
Question
Using C++. Please do the following:
3) 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
note* we can't set "SmartPhone default constructor followed by setting the barStrength to 1". because barStrength property was present in IPhone we can access it in SmartPhone class. so we get error if we mention barStrength to 1 in SmartPhone class
Here is code:
class SmartPhone {
public:
int getNumberOfBars();
};
// Derived class
class IPhone: public SmartPhone {
public:
int barStrength;
IPhone()
{
barStrength = 1;
}
int getNumberOfBars()
{
return 5;
}
int getNumberOfBars(int b)
{
barStrength = b;
return barStrength;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.