The code below has five errors The errors may be syntax erro
The code below has five errors. The errors may be syntax errors or logic errors, and there may be more than one per line; examine the code carefully to find them. Indicate each of the errors you find by writing the line number and correction in the space provided below. Assume that include , are loaded as well as using namespace std.
You must find and correct all five of the errors.
Class Computer {
public:
void SetProcessor(string processor){
processor = m_processor;
}
void SetRam(int ram){
m_ram = ram;
}
public:
string m_processor;
int m_ram;
};
class Phone1 : public Computer{
public:
void MakeCall() {
cout << \"You made a call!\" << endl;
}
};
class Desktop : public Computer{
public:
void BurnDVD() {
cout << \"You burned a DVD!\" << endl;
}
};
int main () {
Desktop desk1;
desk1.SetProcessor(\"i7\");
desk1.SetRam(64);
Phone phone1;
phone1.SetProcessor(Snapdragon);
phone1.SetRam(8);
desk1.BurnDVD();
phone1.MakeCall();
return 0;
}
| Class Computer { public: void SetProcessor(string processor){ processor = m_processor; } void SetRam(int ram){ m_ram = ram; } public: string m_processor; int m_ram; }; class Phone1 : public Computer{ public: void MakeCall() { cout << \"You made a call!\" << endl; } }; class Desktop : public Computer{ public: void BurnDVD() { cout << \"You burned a DVD!\" << endl; } }; int main () { Desktop desk1; desk1.SetProcessor(\"i7\"); desk1.SetRam(64); Phone phone1; phone1.SetProcessor(Snapdragon); phone1.SetRam(8); desk1.BurnDVD(); phone1.MakeCall(); return 0; } |
Solution
// C++ code
#include <iostream>
#include <string.h>
#include <fstream>
#include <limits.h>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
using namespace std;
// class should be in small letters othrwize it does not name a type in C++
class Computer
{
public:
void SetProcessor(string processor){
// variable in class is m_processor
//processor = m_processor; => wrong
m_processor = processor;
}
void SetRam(int ram){
m_ram = ram;
}
// variables are declared as private
private:
string m_processor;
int m_ram;
};
class Phone1 : public Computer
{
public:
void MakeCall() {
cout << \"You made a call!\" << endl;
}
};
class Desktop : public Computer
{
public:
void BurnDVD() {
cout << \"You burned a DVD!\" << endl;
}
};
int main ()
{
Desktop desk1;
desk1.SetProcessor(\"i7\");
desk1.SetRam(64);
// class name is Phone1 not Phone
Phone1 phone1;
// input type of SetProcessor is string
phone1.SetProcessor(\"Snapdragon\");
phone1.SetRam(8);
desk1.BurnDVD();
phone1.MakeCall();
return 0;
}
/*
output:
You burned a DVD!
You made a call!
*/



