Solved by Expert Tutors
Solved by Expert Tutors
Question

i have made a erroe code this is the code //////////////////////////////////////

//main:cpp



#include <iostream>

#include "Ship.h"

#include "Ship.h"

#include "Engine.h"

#include "Engine.h"


using namespace std;

using namespace sdds;


void printHeader(const char* title)

{

  char oldFill = cout.fill('-');

  cout.width(40);

  cout << "" << endl;


  cout << "|> " << title << endl;


  cout.fill('-');

  cout.width(40);

  cout << "" << endl;

  cout.fill(oldFill);

}


int main()

{

  {

    printHeader("T1: Testing Constants");


    cout << "TYPE_MAX_SIZE: " << sdds::TYPE_MAX_SIZE << endl;

    cout << "MIN_STD_POWER: " << sdds::MIN_STD_POWER << endl;

    cout << "MAX_STD_POWER: " << sdds::MAX_STD_POWER << endl;

    cout << endl;

  }


  {

    printHeader("T2: Testing Default Constructor");


    Ship invalid;

    invalid.display();

    invalid += Engine("D2", 2.1);

    cout << endl;

  }


  Engine engines[] = {

    Engine("V8", 4.4),

    Engine("V8", 5.0),

    Engine("Inline", 4.1),

    Engine("D3", 7.0),

    Engine("D0", 2.0),

    Engine("D1", 3.2),

  };


  {

    printHeader("T3: Testing Custom Constructor");

    

    Ship titanic("cruiser", engines, 6);

    titanic.display();

    cout << endl;

  }



{

    printHeader("T4: Testing Conversion to Bool Operator");

    Ship invalid;

    Ship titanic("liner", engines, 1);


    if (invalid)

      cout << "1. Test Failed! Object should be invalid.\n";

    else

      cout << "1. Test succeeded!\n";


    if (titanic)

      cout << "2. Test succeeded!\n";

    else

      cout << "3. Test Failed! Object should be valid.\n";

    

    cout << endl;

  }


  {

    printHeader("T5: Testing += and < Operators");


    Ship titanic("liner", engines, 3);


    char type[]{ "D0" };

    while (titanic < sdds::MIN_STD_POWER)

    {

      type[1]++;

      cout << "Ship not up to standard. Required power: "

       << sdds::MIN_STD_POWER << endl;

      titanic += Engine(type, 2.1);

    }


    titanic.display();


    if (sdds::MAX_STD_POWER < titanic)

      cout << "Too much power." << endl;

    else

      cout << "Ship doesn't exceed power regulation of: "

       << sdds::MAX_STD_POWER << endl;

  }


  return 0;




}

///////////////////////////////////////////////////////////////////////////////////

engine.cpp

#define _CRT_SECURE_NO_WARNINGS




#include <iostream>


#include "Engine.h"


#include "Ship.h"



using namespace std;



namespace sdds


{


Engine::Engine() {


defaultconstructore();


}





Engine::Engine(const char* t, double s) {



if (strlen(t) < 1 && s < 0) {


defaultconstructore();


}


else {


setEng(t, s);


}



}




void Engine::defaultconstructore() {



m_type[0] = '\0';


m_size = 0;


}



void Engine::setEng(const char* engtype, double sz){


if (engtype != nullptr && engtype[0] != '\0' && sz > 0) {



strcpy(m_type, engtype);


m_size = sz;


}


else {


defaultconstructore();


}


}



double Engine::get() const {


return m_size ;


}




void Engine::display() const {



if (m_type != nullptr && m_type[0] != '\0' && m_size > 0) {



cout << m_size << " liters - " << m_type << endl;



}



}



}

/////////////////////////

//Engine.h



#ifndef SDDS_ENGINE_H

#define SDDS_ENGINE_H


//#include <iostream>

#include <cstring>



namespace sdds

{


const int TYPE_MAX_SIZE = 30;


class Engine

{

double m_size ;

char m_type[TYPE_MAX_SIZE];




public:

//constructors

Engine();

Engine(const char* t, double s);


void defaultconstructore();


void setEng(const char* emngtype, double sz);


double get() const;


void display() const;

};




}



#endif


//////////////////////////////////////////////////////////////////////////////

Ship.cpp


#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<cstring>

#include"Ship.h"

using namespace std;


namespace sdds {

  void Ship::defaultconstrut() {

    m_type[0] = '\0';

    m_engCnt = 0;


  }



  Ship::Ship() {

    defaultconstrut();

  }



  Ship::Ship(const char* type, Engine engine[], int numberofEngines) {

    if (strlen(type) <= TYPE_MAX_SIZE && numberofEngines > 0) {

    set(type, engine, numberofEngines);

    }

    else invaild();

  }



  void Ship::set(const char* type, Engine engine[], int numofeng){

    strcpy(m_type, type);

    m_engCnt = numofeng;

    for (int i = 0; i < m_engCnt; i++){

      m_engines[i] = engine[i];

    }

  }



  bool Ship::invaild()const {

    if (m_type && m_engCnt > 0) {

      return false;

    }

    else {

      return true;

    }

  }

//




  void Ship::display() const {

    if (!invaild()) {

  cout << totalpower() << endl;

      for (int i = 0; i < m_engCnt; i++) {

        m_engines[i].display();

  

}

}

    else {

      cout << "No available data" << endl;


    }

  }

float Ship:: totalpower() const

{

    double pow = 0;

    for (int i = 0; i < m_engCnt; i++) {

      pow += m_engines[i].get()* 5;

    }

    return pow;

  }



//4-5

  Ship& Ship::operator+=(Engine engine) {

    if (invaild()) {

      cout << "The ship doesn't have a type! Engine cannot be added!" << endl;

    }

    else {

      if (m_engCnt < 10) {

        m_engines[m_engCnt] = engine;

        m_engCnt++;

      }

    }

    return *this;

  }

  //用private修饰为私有变量

//类内部赋值返回


bool Ship::operator<(double power) const

{

bool lower = false;

  for (int i = 0; i < m_engCnt; i++)

      power = totalpower();

// power out of private

  if (power < 4.4)

return lower;

}

bool operator<(double power, const Ship& theShip){

  


bool low = false;

if (theShip.power < power) {

low = true;

}

return low;

}


}


/////////////////////////

Ship.h

#ifndef SDDS_SHIP_H__

#define SDDS_SHIP_H__

#include "Engine.h"

namespace sdds {


const float MIN_STD_POWER = 90.111;

const float MAX_STD_POWER = 99.999;

const int SIZE = 10;

  class Ship {



Engine m_engines[SIZE];

char m_type[TYPE_MAX_SIZE];

int m_engCnt;

private:


float totalpower() const ;


  public:

    Ship();

    Ship(const char* type, Engine* engine, int numberofEngines);



    void defaultconstrut();

    bool invaild() const;

  

    void display() const;


Ship& operator+=(Engine engine);

    void set(const char* type, Engine* engine, int numofeng);

bool operator<(double power) const;

double power;

// a query that returns true if the total output power of the ship is less than the number,true


  };

bool operator<(double power, const Ship& theShip);


// if the first parameter is smaller than the total output power of the ship rreturns true




}


#endif


/////////////////////////////////////////////

so my output is like this

123455.png


the question ask me to use the explicit operator bool() const

i know this is used to i know it used to 

Implicit conversion of objects of this type to bool ,but i dont know how it works.

i think i will use this fuction to work on this 2 erroe ,th change this type to bool ,but i dont have an idea about how it works

this is the output i want to get


o.png


can anyone give me some help and explain about my question if i got any errors , and how to use the functionexplicit operator bool() const

i know this is used to i know it used to 

Implicit conversion of objects of this type to bool ,but i dont know how it works.

i think i will use this fuction to work on this 2 erroe ,th change this type to bool ,but i dont have an idea about how it works

this is the output i want to get




123455.png
o.png
1&gt; T1: Testing Constants
TYPE_MAX_SIZE : 30
MIN_STD_POWER: 90. 111
MAX_STD_POWER: 99.999
I T2: Testing Default Constructor
No available data
The object is not valid! Engine cannot be added!
I T3: Testing Custom Constructor
cruiser - 128.5
4.4 liters - V8
5 liters - V8
4.1 liters - Inline
7 liters - 03
2 liters - DO
3.2 liters - D1
&gt; T4: Testing Conversion to Bool Operator
1. Test succeeded!
2. Test succeeded!
&gt; T5: Testing += and &lt; Operators
Ship not up to standard. Required power: 90. 111
Ship not up to standard. Required power: 90. 111
Ship not up to standard. Required power: 90. 111
liner - 99
4.4 liters - V8
5 liters - V8
4.1 liters - Inline
2.1 liters - D1
2.1 liters
- D2
2.1 liters
- 03
If :
99. 999

Step-by-step answer

inia pulvinar tortor nec faci


sus ante, dapibus a molestie coupulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestieuipsum dolor sit amet,


, dictum vitae odio. Donec au, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipua molestie consequat,


ongue vel laoreet atriipisci, dictuatitur la molestrisusec facilisis. Peor neconsectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a m

congue veltriipisci, dictuatitur la molestrisus

amet, co

, dictum vitaetricongue vllento

, dictum vitaeicDiausce dui lectus, congue ve

inia pulvinar tortor nec facilisis. Pellentes

, dictum vitaeur laoreet. Nam risus a

, dictum vitaeuce dui lectus,

facilisis. Pellentesqueor nes a mol

cing elit. Nam lacinia pulvinar tortor

sum dolor


lestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec ali

ultrices ac magna. Fusce dui lectus, congue v

icitur laoreet. Nam risus ante, d

s a molestie consequat, ultrices ac magna. Fusce dui lectus, c

ctum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipisc

cing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus effic

lestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec ali

Subscribe to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question