011-001-005
Constructor Definition: Product Class
Easy
Problem Description
constructor Definition: Product class
In this problem, you will create a program that reads a product name from standard input, initializes the name field in a Product class constructor, and displays the product name to standard output using the showName() method.
Learning Objective: Learn how to initialize objects with constructors
Overview
A constructor is a special method called automatically when an object is created. The product name read from standard input is passed as an argument to the constructor and assigned to the name field, then displayed via showName().
Input
Product name (string, one line)
Specifications
- Initialize the
namefield in theProductclass constructor (assign the parameter value tothis.name) - Display the product name in
showName()method, prefixed with"Product: "
Output Format
Product: Apple
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up