Concept explainers
Assume the definition of class foodType as given in Exercise 6.
Answer the following questions? (1, 2, 3, 5, 6)
Write the definition of the member function set so that private members are set according to the parameters. The values of the int and double instance variables must be nonnegative.
Write the definition of the member function print that prints the values of the data members.
Write the definitions of the member functions getName, getcalories, getFat, getSugar, getCarbohydrate, getPotassium to return the values of the instance variable.
Write the definition of the default constructor of the class foodType so that the private member variables are initialized to 0, 0. 0, 0, 0. 0, 0. 0, respectively.
Write the definition of the constructor with parameters of the class foodType so that the private member variables are initialized according to the parameters. The values of the int and double instance variables must be nonnegative.
Write a C + + statement that prints the value of the object fruit 2.
Write a C++ statement that declares an object my Fruit of type foodType, and initializes the member variables of myFruit to “Apple”, 52, 0. 2, 10,13.8, and 148.0, respectively.
Want to see the full answer?
Check out a sample textbook solutionChapter 10 Solutions
C++ Programming: From Problem Analysis to Program Design
- Object passing checklist1. Go over each member function and add the const keyword after their declaration ifthey do not modify any of the class' member variables.2. When accepting objects as parameters, prefer to pass them by reference. Add areference declarator (&) before the identifier name of an object parameter.3. If the member function does not modify the parameter, make the parameter constantby using the const keyword. apply the Object Passing Checklist. Modify food.h and volunteer.h to useobject references. Add const and & keywords in the appropriate places of the code.arrow_forwardI'm confused about this question. Any help is appreciated!arrow_forwardHow we can access protected data members of a class without the use of public or private functions? Explain with codearrow_forward
- Please solution in c++arrow_forwardNutritional information (classes/constructors) PYTHON ONLY Complete the FoodItem class by adding a constructor to initialize a food item. The constructor should initialize the name (a string) to "None" and all other instance attributes to 0.0 by default. If the constructor is called with a food name, grams of fat, grams of carbohydrates, and grams of protein, the constructor should assign each instance attribute with the appropriate parameter value. The given program accepts as input a food item name, fat, carbs, and protein and the number of servings. The program creates a food item using the constructor parameters' default values and a food item using the input values. The program outputs the nutritional information and calories per serving for both food items. Ex: If the input is: M&M's10.034.02.01.0 where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 1.0 is the number of servings, the output is:…arrow_forwardWrite a class ‘Box’ which has three members height, width and depth; To set values and use these members, write mutator and accessor functions for these members. (Hint: there would be 3 Accessor and 3 Mutator functions).arrow_forward
- Use C++arrow_forwardTrue or False: In order to derive class A from class B, class A must contain a public member function that may be called by the rest of the class. (It is not a half-finished project.) A) This is correct. B) The answer is False.arrow_forwardTrue or false? Only instance member functions can be defined as constant functions.arrow_forward
- Design a class bookType that defines a book as a class. a. Each object of the class bookType will hold the following information about a book: title number of authors up to four authors publisher year published ISBN 13 (with dashes) price number of copies in stock (may be 0) b. Include the member functions to perform the various operations on objects of type bookType: Include individual get and set functions for all member variables. c. Add a member function to update the number of copies in stock. d. Add the appropriate constructors and a destructor (if one is needed). Ensure a default constructor is coded and the constructor initializes all data members. Remember, an array of classes uses a default constructor only. e. ALL member variables must be private and accessed through member functions. The main client program CANNOT access the variables directly. f. Ensure the class declaration and class implementation files are in separate header and code (.cpp) files. This assignment will…arrow_forward6. True/False Questions: • The role of constructors is to initialize objects and data members. 1. 2. True False • A non-static member function may only refer to non-static data members of the class. 1. 2. True False All type parameters in the template prefix must appear in the body of a function template. 1. True 2. Falsearrow_forwardTask-1: Both computer and software engineering programs blend the broad engineering discipline courses with the computer science specific courses. Hence, it is a good idea to represent the "course" as an object before attempting to write a program to help students get register to the courses. The programmer can think of the abstraction of a course object as the representation of title, code, and credit values only. Write a class declaration for the course object that includes a constructor, a set and a get member function for each data member, and a member function that returns the course information upon request. For instance, for this course it should prepare a string like “COMP218 - Object-Oriented Programming". a) Use C++-strings for the representation of string of characters. b) Use C-strings for the representation of string of characters.arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr