1. What is the output of the following C++ program?
       #include <iostream>
#include <string>
using namespace std;

class Circle
    Circle(double radius) {this->radius = radius; }
    void put() const {cout << "Radius = " << radius;}
    double radius;

class ColoredCircle: public Circle
    ColoredCircle(double radius, string color);
    void put() const;
    string color;

ColoredCircle::ColoredCircle(double radius, string color)
    : Circle(radius), color(color) {}

void ColoredCircle::put() const
    cout << " Color = " << color;

int main()
    ColoredCircle redCircle(100., "red");
    Circle* circle1 = &redCircle;
    cout << endl;
    Circle circle(50.);
    Circle* circle2 = &circle;
    cout << endl;
    return 0;

Modify the program so that the put function is virtual. What is the output after that change? Does Java allow both virtual and non-virtual 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;
    cout << endl;
    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.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

    By purchasing this solution you'll be able to access the following files:
    Solution.docx and

    for this solution

    or FREE if you
    register a new account!

    PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

    Find A Tutor

    View available Java Programming Tutors

    Get College Homework Help.

    Are you sure you don't want to upload any files?

    Fast tutor response requires as much info as possible.

    Upload a file
    Continue without uploading

    We couldn't find that subject.
    Please select the best match from the list below.

    We'll send you an email right away. If it's not in your inbox, check your spam folder.

    • 1
    • 2
    • 3
    Live Chats