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...
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
Justify jackknife method is a special case of bootstrap method.
Justify jackknife method is a special case of bootstrap method.
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...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
Calculator Internal Rate of Return Method The internal rate of return method is used by King...
Calculator Internal Rate of Return Method The internal rate of return method is used by King Bros. Construction Co. in analyzing a capital expenditure proposal that involves an investment of $73,600 and annual net cash flows of $10,000 for each of the 10 years of its useful life. Present Value of an Annuity of $1 at Compound InterestYear6%10%12%15%20%10.9430.9090.8930.8700.83321.8331.7361.6901.6261.52832.6732.4872.4022.2832.10643.4653.1703.0372.8552.58954.2123.7913.6053.3522.99164.9174.3554.1113.7843.32675.5824.8684.5644.1603.60586.2105.3354.9684.4873.83796.8025.7595.3284.7724.031107.3606.1455.6505.0194.192 a. Determine a present value factor for an annuity of $1 which can be used in determining the internal rate of return....
Internal Rate of Return Method The internal rate of return method is used by Testerman Construction...
Internal Rate of Return Method The internal rate of return method is used by Testerman Construction Co. in analyzing a capital expenditure proposal that involves an investment of $75,285 and annual net cash flows of $15,000 for each of the 10 years of its useful life. Present Value of an Annuity of $1 at Compound Interest Year 6% 10% 12% 15% 20% 1 0.943 0.909 0.893 0.870 0.833 2 1.833 1.736 1.690 1.626 1.528 3 2.673 2.487 2.402 2.283 2.106...
Remember we're only using C to program this. That does not mean C++, just no! This...
Remember we're only using C to program this. That does not mean C++, just no! This one's long so pay attention. You are going to create a structure that resembles a university’s profile (Any university name is ok, just pick one). This structure must contain 5 members. They are listed here: 1. One member for number of undergraduate students 2. One member for number of graduate students 3. One member for number of classrooms 4. One member for the name...
What is the cash flow in finance and what is the special type of that?  
What is the cash flow in finance and what is the special type of that?  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT