Question
can anyone help me on fixing this code ? i am unable to finish this the code like this /////////// main.cpp /////////////////////////////////// #include #include #includeBasket.h
can anyone help me on fixing this code ?
i am unable to finish this
the code like this
///////////
main.cpp
///////////////////////////////////
#include
#include
#include"Basket.h"
#include"Basket.h" //intentional
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()
{
sdds::Fruit fruits[]{
{"apple", 0.65},
{"banana", 1.25},
{"pear", 0.50},
{"mango", 0.75},
{"plum", 2.00},
};
{
printHeader("T1: Default Constructor");
Basket aBasket;
cout << aBasket;
// conversion to bool operator
if (aBasket)
cout << "Test failed: the basket should be empty! ";
else
cout << "Test succeeded: operator said the basket is empty! ";
cout << endl;
}
{
printHeader("T2: Custom Constructor");
Basket aBasket(fruits, 2, 6.99);
cout << aBasket;
// conversion to bool operator
if (aBasket)
cout << "Test succeeded: operator said the basket has content! ";
else
cout << "Test failed: the basket should NOT be empty! ";
cout << endl;
}
{
printHeader("T3: += operator");
Basket aBasket;
aBasket += fruits[2];
(aBasket += fruits[0]) += fruits[4];
aBasket.setPrice(12.234);
cout << aBasket;
cout << endl;
}
this main should not change
and
/////////////////
Basket.h
////////////////////////////////////////////////
#ifndef SDDS_BASKET_H
#define SDDS_BASKET_H
#include
#include
namespace sdds
{
struct Fruit
{
char m_name[30 + 1]; // the name of the fruit
double m_qty; // quantity in kilograms
};
class Basket
{
char* m_fruits;
int m_cnt;
double m_price;
public:
Basket();
Baskett(const char* nameoffr, double pric);
~Basket(); //destructor
void setPrice(double prc);
bool isempty() const;
void display() const;
Basket(const Basket& bas);
Basket& operator+=(const Basket& basket);
};
}
///////////////////////
Basket.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Basket.h"
namespace sdds
{
Basket::Basket() {
m_fruits = nullptr;
m_cnt = 0;
m_price = 0;
}
Basket::Basket(const char* nameoffr,double pric) {
m_fruits = nullptr;
m_price = 0;
if (nameoffr != nullptr && nameoffr[0] != '\0' && pric > 0) {
setName(nameoffr);
setPrice(prix);
}
else {
m_fruits = nullptr;
m_price = 0;
}
}
Basket::~Basket() {
delete[] m_fruits;
m_fruits = nullptr;
}
void Basket::setPrice(double price) {
if (prc >= 0) {
m_price = prc;
}
}
bool Basket::isempty() const {
bool empty = false;
if (m_fruits == nullptr && m_cnt == 0 && m_price == 0) {
empty = true;
}
return empty;
}
void Basket::display const {
if (isempty()) {
cout << "The basket is empty!" << endl;
}
else {
cout << "Basket Content" << endl;
cout << " m_fruits: " << m_price << endl;
}
}
}
///////////////////////////////////////
the output should be like this
----------------------------------------
|> T1: Default Constructor
----------------------------------------
The basket is empty!
Test succeeded: operator said the basket is empty!
----------------------------------------
|> T2: Custom Constructor
----------------------------------------
Basket Content:
apple: 0.65kg
banana: 1.25kg
Price: 6.99
Test succeeded: operator said the basket has content!
----------------------------------------
|> T3: += operator
----------------------------------------
Basket Content:
pear: 0.50kg
apple: 0.65kg
plum: 2.00kg
Price: 12.23
help me about this plz , I am confused and confused
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started