Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1a. What is the output of the following C++ program? #include <iostream> #includ

ID: 3556323 • Letter: 1

Question

1a. What is the output of the following C++ program?

    #include <iostream>

    #include <string>

    using namespace std;

    class Circle

    {

    public:

        Circle(double radius) {this->radius = radius; }

        void put() const {cout << "Radius = " << radius;}

    private:

        double radius;

    };

    class ColoredCircle: public Circle

    {

    public:

        ColoredCircle(double radius, string color);

        void put() const;

    private:

        string color;

    };

    ColoredCircle::ColoredCircle(double radius, string color)

        : Circle(radius), color(color) {}

    void ColoredCircle::put() const

    {

        Circle::put();

        cout << " Color = " << color;

    }

    int main()

    {

        ColoredCircle redCircle(100., "red");

        Circle* circle1 = &redCircle;

        circle1->put();

        cout << endl;

        Circle circle(50.);

        Circle* circle2 = &circle;

        circle2->put();

        cout << endl;

        return 0;

    }

1b. Modify the program so that the put function is virtual. What is the output after that change?

1c. Does Java allow both virtual and nonvirtual methods? If not, which does it allow? Rewrite this program in Java and identify at least four differences between the programs in the two languages.

2. Consider the following C++ main function in place of the one in previous problem.

    int main()

    {

        Circle circle1(100.), *circle2 = new Circle(200.);

        ColoredCircle redCircle(300, "red"), *blueCircle = new ColoredCircle(400., "blue");

        circle1 = redCircle;

        circle2 = blueCircle;

        circle1.put();

        cout << endl;

        circle2->put();

        cout << endl;

        return 0;

    }

    Assuming the put function was made virtual, what is the output of the above program? This program contains an example of object slicing. On what line does it occur? Why must it happen? Explain why this never happens in Java. Do some investigating and determine how C# avoids this problem.

Explanation / Answer

1 c

In java

-------------------------------Circle.java---------------------------------------------



public class Circle {
   double radius;
   public Circle(Double radius)
   {
       this.radius=radius;
       }
   public void put()
   {
       System.out.println("Radius ="+radius);
   }
}

-----------------------------------ColoredCircle.java--------------------------------------------------


public class ColoredCircle extends Circle{
   String color;
   public ColoredCircle(double radius,String color){
      
       super(radius);
       this.color=color;
   }
public void put()
   {
   super.put();
   System.out.println("color ="+color);
      
   }
public static void main(String a[])
   {
  
   ColoredCircle redCircle=new ColoredCircle (100., "red");
   Circle circle1=redCircle;
   circle1.put();
   Circle circle=new Circle(50.);
   Circle circle2=circle;
   circle2.put();
  
       }
}

Java doesn't have virtual method invocation, it determines at runtime depending on the actual class that put points to, we get result.

Radius =100.0
color =red
Radius =50.0

differences

1 java does not use pointers as we have used in c++ programs.

2 java does not have virtual methods.

3 output is different for same programs.

4 java does compile tiem binding to whoes method to call.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote