Question

In: Computer Science

Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type)...

Lab11B:Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “green”; this is called a default constructor.Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Turtle objects. For the first Turtle object, call the first constructor. For the second Turtle object, call the second constructor (passing it a speed of 5 and a color of “purple”). Using the dot ‘.’ Operator, print out the first turtle’s speed and the second turtle’s color.This is a test to see if you can design constructors inside your class and initialize attributes.

Sample output #1

0

purple

Solutions

Expert Solution

#include <iostream>
using namespace std;

class Turtle
{
  public:
  double speed;
  string color;
  
  Turtle()
  {
      speed=0;
      color="green";
  }
  
  Turtle(string col, double sp)
  {
      color=col;
      speed=sp;
  }
  
};

int main()
{
    Turtle t1;
    cout<<t1.speed<<endl;
    
    Turtle t2=Turtle("purple",5);
    cout<<t2.color;
    
}

I hope it helps.


Related Solutions

I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the...
1. A constructor is a special Class member method. It is automatically called when an object...
1. A constructor is a special Class member method. It is automatically called when an object of the class is created. It can also be called more than once after the object is created. 2. In Java, the new operator is used to create an instance/object of a class in the heap. 3. A reference variable is a memory location contains an address of an object stored in the stack. 4. Encapsulation and abstraction are both the pillars of Object-Oriented...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method...
Constructor: -empty body Class method 1: goodFriend - return string value, "I like you!" Class method 2: badFriend - return string value, "I don't like you!" Main Method: - create new Friend object - call goodFriend and badFriend using Friend object /* class header */ { /* constructor method header */ { } public static void main (String [] args) { Friend f = /* new Friend object */ // call goodFriend using Friend object // call badFriend using Friend...
Complete the following method to return the median of three integers. Remember, you need to use...
Complete the following method to return the median of three integers. Remember, you need to use the names of the parameters as given in the method header. This is a return method, so you need to include return statement(s) NOT System.out.println. JAVA PROGRAM Exampe: Method call Value returned medof 5 medof 5 medof -2 medof 0 Method header: public static int medof(int n1,int n2, int n3)
Which is NOT a valid return type for a method declaration Select one: a. double b....
Which is NOT a valid return type for a method declaration Select one: a. double b. char c. null d. String e. boolean f. float g. int h. void Statements that allow a program to choose, based on a boolean expression, between alternative blocks of code are known as: Select one: a. Selection statements or Conditionals b. Declarations c. Methods d. Repetition Statements or Loops e. Composition
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
Justify jackknife method is a special case of bootstrap method.
Justify jackknife method is a special case of bootstrap method.
true or false C++ a.    (T or F)   A function or method cannot return a value of type...
true or false C++ a.    (T or F)   A function or method cannot return a value of type array. b.    (T or F)   C++ throws an exception if you try to refer to element 47 in an array with 20 elements. c.    (T or F)   I can copy one array to another by simply using an assignment statement which assigns the one array to the other. d.    (T or F)  An exception is the occurrence of an erroneous or unexpected situation during the execution of a program. e.    (T...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
The following code is for a class named Box. The class Box includes a constructor method...
The following code is for a class named Box. The class Box includes a constructor method Box, and a method getVolume(). For your assignment you are to develop a java class named MatchBox. Your class MatchBox must extend the class Box and in addition to the attributes width, height, and depth that are defined in the class Box, MatchBox must add a new attribute weight. The getVolume method must both report the values of width, height, depth, and weight, but...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT