Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can anyone help me a hand ? this is the function ,i want to change this fucntion ,to use dynamic memory for Engine m_engines[SIZE]; char

can anyone help me a hand ?

this is the function ,i want to change this fucntion ,to use dynamic memory for

Engine m_engines[SIZE];

char m_type[TYPE_MAX_SIZE]; to make them use arrays of any size.

this is the code below .

//Ship.cpp

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

#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;

}

}

//

Ship::operator bool() const

{

return m_type && m_type[0];

}

void Ship::display() const {

if (!invaild()) {

cout << setw(6) << m_type << " - ";

printf("%.2f ",totalpower());

cout << 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 {//extra

if ( totalpower() < power) {

return true;

}

else {

return false;

}

}

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

if (power < theShip. totalpower()) {

return true;

}

else {

return false;

}

}

}

=====================================================================

//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);

explicit operator bool() const;

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;

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

};

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

}

#endif

i cant do this work . my try makes full of errors ,

i changed the function

can anyone tell me how to change this function ?

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions