Question & Answer: Please help with this last part of the question for C++:…..

Please help with this last part of the question for C++:

Assume class CameraPhone has already been declared as a derived class of class Phone as described in the previous problems. Write an implementation of the constructor of class CameraPhone. The constructor accepts two integer parameters and uses them to initialize data members imageSize and memorySize. The model and part number should be set to empty string and retail price to O. Definition is supposed to be outside the class specification (i.e. no inline function definition accepted).

class Phone {
private:
string model;
string partNumber;
double retailPrice;
public:
Phone();
string getModel();
string getPartNumber();
double getRetailPrice();
void setPartNumber(string a);
void setModel(string a);
void setRetailPrice(double a);
friend ostream & operator << (ostream &out, const Phone &c);
friend istream & operator >> (istream &in, Phone &c);
};

Phone::Phone(){
model = “”;
partNumber = “”;
retailPrice = 0;
}
string Phone::getModel(){
return model;
}
string Phone::getPartNumber(){
return partNumber;
}
string Phone::getModel(){
return model;
}
void Phone::setModel(string a){
model = a;
}
void Phone::setPartNumber(string a){
partNumber = a;
}
void Phone::setRetailPrice(double a){
retailPrice = a;
}
ostream & operator << (ostream &out, const Phone &c)
{
out << c.model << ” ” << c.partNumber << ” ” << c.retailPrice << endl;
return out;
}

istream & operator >> (istream &in, Phone &c)
{
cout << “Enter Model “;
in >> c.model;
cout << “Enter Part Number “;
in >> c.partNumber;
cout << “Enter Retail Price “;
in >> c.retailPrice;

return in;
}

class CameraPhone : public Phone {
private:
int imageSize;
int memorySize;
public:
CameraPhone(int is, int ms) : Phone();
int numPictures();
};

CameraPhone::CameraPhone(int is, int ms) : Phone(){
imageSize = is;
memorySize = ms;
}
int CameraPhone::numPictures() {
return ( memorySize/imageSize);
}

int main(){

double price;

CameraPhone myPhone(2,64);
myPhone.setModel(“Droid Maxx2”);
myPhone.setPartNumber(“224455”);
cout << “Enter Retail Price:” ;
cin >> price;
myPhone.setRetailPrice(price);
return 0;
}

Assume class CameraPhone has already been declared as a derived class of class Phone as described in the previous problems. Write an implementation of the constructor of class CameraPhone. The constructor accepts two integer parameters and uses them to initialize data members imageSize and memorySize. The model and part number should be set to empty string and retail price to O. Definition is supposed to be outside the class specification (i.e. no inline function definition accepted).

Expert Answer

 

Code you have written satisfies the given requirement.

1. Base class constructor initializes the model, partNumber to empty string and retailPrice to 0.

2. Child class constructor constructor that accepts two integer arguments calls the base class constructor.

______________________________________________________________________________________________

Another way of initializing values for members of base class using setter methods:

#include <iostream>
#include <string>

using namespace std;

class Phone {
private:
string model;
string partNumber;
double retailPrice;
public:
Phone();
string getModel();
string getPartNumber();
double getRetailPrice();
void setPartNumber(string a);
void setModel(string a);
void setRetailPrice(double a);
friend ostream & operator << (ostream &out, const Phone &c);
friend istream & operator >> (istream &in, Phone &c);
};

Phone::Phone(){
model = “”;
partNumber = “”;
retailPrice = 0;
}
string Phone::getModel(){
return model;
}
string Phone::getPartNumber(){
return partNumber;
}
void Phone::setModel(string a){
model = a;
}
void Phone::setPartNumber(string a){
partNumber = a;
}
void Phone::setRetailPrice(double a){
retailPrice = a;
}
ostream & operator << (ostream &out, const Phone &c)
{
out << c.model << ” ” << c.partNumber << ” ” << c.retailPrice << endl;
return out;
}

istream & operator >> (istream &in, Phone &c)
{
cout << “Enter Model “;
in >> c.model;
cout << “Enter Part Number “;
in >> c.partNumber;
cout << “Enter Retail Price “;
in >> c.retailPrice;

return in;
}

class CameraPhone : public Phone {
private:
int imageSize;
int memorySize;
public:
          CameraPhone(int is, int ms);
int numPictures();
};

//CameraPhone Class Constructor implementation
CameraPhone::CameraPhone(int is, int ms){

        // Assigning values
imageSize = is;
memorySize = ms;

       //Assigning empty string to model and part number and retail price to 0
setModel(“”);
setPartNumber(“”);
setRetailPrice(0);
}

int CameraPhone::numPictures() {
return ( memorySize/imageSize);
}

int main(){

double price;

      CameraPhone myPhone(2,64);

      //Printing values
cout << “n Value after creating object: nn ” << myPhone << ” n”;

myPhone.setModel(“Droid Maxx2”);
myPhone.setPartNumber(“224455”);
cout << “Enter Retail Price:” ;
cin >> price;
myPhone.setRetailPrice(price);

      //Printing values
cout << “n Values after assigned using setter methods: nn ” << myPhone;

return 0;
}

Sample Run:

Still stressed from student homework?
Get quality assistance from academic writers!