157k views
0 votes
Write the interface (.h file) of a class GasTank containing: A data member named amount of type double. A data member named capacity of type double. A constructor that accepts a parameter of type double. A function named addGas that accepts a parameter of type double and returns no value. A function named useGas that accepts a parameter of type double and returns no value. A function named isEmpty that accepts no parameters and returns a boolean value. A function named isFull that accepts no parameters and returns a boolean value. A function named getGasLevel that accepts no parameters and returns a double. A function named fillUp that accepts no parameters and returns a double.

User Marnie
by
3.3k points

1 Answer

3 votes

Answer:

The interface(.h file) in cpp language is shown below.

#ifndef GASTANK_H

#define GASTANK_H

class GasTank

{

//variables as mentioned

double amount, capacity;

//constructor

GasTank(double p);

//method to add gas amount

void addGas( double g);

//method to subtract used gas amount

void useGas(double u);

//method to verify if the tank is empty

bool isEmpty();

//method to verify if the tank is full

bool isFull();

//method to return current gas amount in the tank

double getGasLevel();

//method to return amount of gas to be filled in the tank

double fillUp();

};

#endif

Step-by-step explanation:

1. The class GasTank ends with a semicolon.

2. The variables are declared with double datatype as mentioned.

3. The constructor is declared accepting double parameter.

4. The methods, addGas() and useGas(), accept double parameter and return no value.

5. The methods, isEmpty() and isFull() and getGasLevel() return boolean, boolean and double values, respectively.

6. The header file begins with the following keywords.

#ifndef GASTANK_H

#define GASTANK_H

7. The header file ends with the following keyword.

#endif

8. This header file is saved as GasTank.h, the name of the class with .h extension.

9. This header file is imported in the program using the following code. The header file is enclosed within quotes and not brackets like the iostream header file.

#include "GasTank.h"

10. All the code beginning with # do not end with any punctuation mark.

11. All the methods in the header file are only declared and none of the methods have any definition.

12. The program which uses this header file is responsible to define all the methods in the header file and use all the variables in the header file.

13. The cpp language does not supports interface directly. Hence, the header file is a way to implement an interface which is a feature of completely object-oriented programming languages.

14. All the requirements mentioned are implemented in the above header file.

User Tero Niemi
by
3.4k points