Question

In: Computer Science

In C++ ifdef QUESTION2    /*    2. Create a header and implementation for a class...

In C++

ifdef QUESTION2
   /*
   2. Create a header and implementation for a class named bottle. bottle objects should be able
   to set and get their size in ounces, the type of liquid it contains(as a string), the amount of
   liquid it contains and whether they are less than another bottle object(based on the amount
   of liquid they contain). Do not comment your code.
   */

   std::cout << "QUESTION 2: " << std::endl << std::endl;

   std::cout << std::boolalpha;

   bottle myBottle;
   bottle anotherBottle;

   myBottle.setLiquidType("water");
   myBottle.setSize(32.0);
   myBottle.setFillAmount(20.0);

   anotherBottle.setLiquidType(myBottle.getLiquidType());
   anotherBottle.setSize(myBottle.getSize());
   anotherBottle.setFillAmount(10.0);

   std::cout << "myBottle is less than anotherBottle: " << myBottle.isLessThan(anotherBottle) << std::endl;

   std::cout << std::endl << std::endl;

#endif

Solutions

Expert Solution

Solution:

I have createed 2 files.

  • bottle.h
  • bottle.cpp

Codes:

bottle.h

#ifndef BOTTLE_H
#define BOTTLE_H
#include <iostream>
#include <string>
using namespace std;

class bottle{
        private:
                string liquidType;
                float size;
                float fillAmount;
        public:
                string getLiquidType();
                void setLiquidType(string);
                float getSize();
                void setSize(float);
                float getFillAmount();
                void setFillAmount(float);
                bool isLessThan(bottle );
};
#endif

bottle.cpp

#include "bottle.h"

string bottle::getLiquidType()
{
        return liquidType;
}
void bottle::setLiquidType(string liquidType)
{
        this->liquidType = liquidType;
}
float bottle::getSize()
{
        return size;
}
void bottle::setSize(float size)
{
        this->size = size;
}
float bottle::getFillAmount()
{
        return fillAmount;
}
void bottle::setFillAmount(float fillAmount)
{
        this->fillAmount = fillAmount;
}

bool bottle::isLessThan(bottle obj)
{
        return this->fillAmount<obj.fillAmount;
}

Here is the driver code:

Note: I have used the given code in the question

#include <iostream>
#include "bottle.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
   std::cout << "QUESTION 2: " << std::endl << std::endl;

   std::cout << std::boolalpha;

   bottle myBottle;
   bottle anotherBottle;

   myBottle.setLiquidType("water");
   myBottle.setSize(32.0);
   myBottle.setFillAmount(20.0);

   anotherBottle.setLiquidType(myBottle.getLiquidType());
   anotherBottle.setSize(myBottle.getSize());
   anotherBottle.setFillAmount(10.0);

   std::cout << "myBottle is less than anotherBottle: " << myBottle.isLessThan(anotherBottle) << std::endl;

   std::cout << std::endl << std::endl;


   return 0;
}

Obtained output:


Related Solutions

A header file contains a class template, and in that class there is a C++ string...
A header file contains a class template, and in that class there is a C++ string object. Group of answer choices(Pick one) 1)There should be a #include for the string library AND a using namespace std; in the header file. 2)There should be a #include for the string library. 3)There should be a #include for the string library AND a using namespace std; in the main program's CPP file, written before the H file's include.
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
Write the header and the implementation files (.h and .cpp separately) for a class called Course,...
Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class...
Objective You are given a partial implementation of one header file, GildedRose.h. Item is a class that holds the information for each item for the inn. GildedRose is a class that holds an internal listing of many Item objects. This inventory should hold at least 10 items. For this you can use arrays, the std::array class, or even the vector class. Complete the implementation of these classes, adding public/private member variables and functions as needed. You should choose an appropriate...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp...
Problem Statement: Implement the MyString class using a header and implementation file named MyString.h and MyString.cpp respectively. Make sure to properly test your code on your own by creating a test driver that tests every function created in the MyString class. Deliverables: proj3-MyString.h proj3-MyString.cpp proj3-testMain.cpp Memory Requirements: Your MyString should start with 10 bytes of allocated memory and should grow in size by doubling. So, we should be able to predict the capacity of your MyString as acquiring a patten...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class...
C++ * Program 2:      Create a date class and Person Class.   Compose the date class in the person class.    First, Create a date class.    Which has integer month, day and year    Each with getters and setters. Be sure that you validate the getter function inputs:     2 digit months - validate 1-12 for month     2 digit day - 1-3? max for day - be sure the min-max number of days is validate for specific month...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork...
Using C++ Create the UML, the header, the cpp, and the test file for an ArtWork class. The features of an ArtWork are: Artist name (e.g. Vincent vanGogh or Stan Getz) Medium (e.g. oil painting or music composition) Name of piece (e.g. Starry Night or Late night blues) Year (e.g. 1837 or 1958)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT