Question

10. Does the following code compile? Does it run? Is there any problem with the code? If yes, how do you fix it? 1. #include < iostream > 2. using namespace std; 4 class Computer int Id; 7. public: Computer(int id) this -Id- id; ) void process() cout << Computer::process(); 9 10. H; 12. class Employee 13. 14. 15. public: 16 Computer* c; Employee )cnew Computer (123); 17.Employee() C 18. 19. 20 21. 22. ^; 23. 24. int main(){ 25. 26 27. 28 void foo() f cout << Employee::foo(); c-> process(); Employee ob; ob.foo); return 0;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer; The above code is valid and it will produce the below output as well

Employee::foo()Computer::process()

#include <iostream>

using namespace std;
class Computer{
int Id;
public:
Computer(int id){this->Id=id;};
void process(){cout<<"Computer::process()";}
};

class Employee{
Computer* c;
public:
Employee(){c=new Computer(123);
}
~Employee(){}
void foo(){
cout<<"Employee::foo()";
c->process();
}
};

int main()
{

Employee ob;
ob.foo();
return 0;
}

Output:

Employee::foo()Computer::process()

Add a comment
Know the answer?
Add Answer to:
10. Does the following code compile? Does it run? Is there any problem with the code?...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Any help will be appreciated please... Compile and run the file. If you find any compilation...

    Any help will be appreciated please... Compile and run the file. If you find any compilation errors,explain their reason as comments in the code. Then fix the errors such that the program compiles and runs. Explain the motivation of your modifications in the code and their effects on the execution. --- inheritance2 file--- #include<iostream> using namespace std; class A { int x; public: A(int i) { x = i; } void print() { cout << x; } }; class B:...

  • 5. What’s wrong with the code below? Feel free to compile and run the program to...

    5. What’s wrong with the code below? Feel free to compile and run the program to investigate. [6] #include <iostream> using namespace std; class HW1 { public:    char *word;    HW1() { }    ~HW1()    { delete [] word;    } }; void foo(HW1 a) { //some operation here } int main() { HW1 a; a.word = new char[80]; strcpy(a.word, "abc"); foo(a); return 0; }

  • Need help with a multiple inheritance work Question Compile and run the file. If you find...

    Need help with a multiple inheritance work Question Compile and run the file. If you find any compilation errors,explain their reason as comments in the code. Then fix the errors such that the program compiles and runs. Explain the motivation of your modifications in the code and their effects on the execution inheritance.cpp file #include<iostream> using namespace std; class A { int x; public: void setX(int i) {x = i;} void print() { cout << x; } }; class B:...

  • The following code will not compile. Fix it by inserting one line of code. Do not...

    The following code will not compile. Fix it by inserting one line of code. Do not change anything else. Hint: copy and paste the code below into your answer box, then make the required addition. Also include a comment indicating the line of code that you inserted, and why that line of code is required include <iostream> using namespace std int mainO cout <<"The word of the day is: sayword 0 return 0 void sayWordO f cout くく "WORD!";

  • Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change...

    Type, Compile, Run all syntactically complete or mostly complete example code .You may need to change some syntax to make them compile and run import java.io.*; import java.lang.*; import java.util.*; class MyThreadExampleMutex{ public static void main(String[] args) { new HelloThread (1). start (); new HelloThread (2). start (); new HelloThread (3). start (); System.out.print("Global.buffer Content = "); for (int i = 0; i < 9; i++) { System.out.print(Global.buffer[i] + "; "); }} } class Global { public static int[] buffer...

  • A) Fix any errors to get the following program to run in your environment.               B)...

    A) Fix any errors to get the following program to run in your environment.               B) Document each line of code with comments and describe any changes you had to make to the original code to get it to work. C) Write a summary of what your final version of the program does. You may also add white space or reorder the code to suit your own style as long as the intended function does not change. Program 3 #include...

  • Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are...

    Please answer in C++ ONLY, and please fill all comments elaboratively. Question 1 Let's consider the following code * * * * Compile and fix all errors No test cases are required for this question. Run and provide a single screenshot showing the output. Complete the missing comments to explain the action performed by each statement highlighted in red. The first comment is given as an example. Copy and paste the source code with the comments filled out in your...

  • JAVA CODE This program causes an error and crashes. Compile and give it a test run,...

    JAVA CODE This program causes an error and crashes. Compile and give it a test run, note the exception that is thrown. Then do the following to fix the problem 1. Write code to handle this exception 2. Use try, catch and finally blocks. 3. Display, the name of the exception 4. Display a message from the user about what happened. Here is the starting file BadArray.java: public class BadArray { public static void main(String[] args) { // Create an...

  • Please help! Studying for my c++ test! What does the following code produce? include <iostream>   ...

    Please help! Studying for my c++ test! What does the following code produce? include <iostream>    using namespace std; void my_function(int n); void main() {     my_function(546); } void my_function(int n) {     if (n < 10)         cout << n << endl;     else     {         my_function(n/10);         cout << (n%10) << endl;     } } What is the output of the following code?                   int j=32, k=5, r;                   r = j ^ k;                   cout << r...

  • C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code...

    C++ class 3.2.4: If-else statement: Fix errors. Re-type the code and fix any errors. The code should convert non-positive numbers to 1. if (userNum > 0) cout << "Positive." << endl; else cout << "Not positive, converting to 1." << endl; Answer #include using namespace std; int main() { int userNum; cin >> userNum; /* Your solution goes here */ return 0; userNum = 1; cout << "Final: " << userNum << endl;

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT