I am trying to make a program for myself to keep a database of the ingredients and chemicals used in the drinks and foods I drink. I am kinda of new to C++, but I am not an absolute beginner, so I am somewhere between beginner and mediocre.

Anyways

I created a class called "Drink", I want to make the objects within the class the different drinks I drink and their info. SO FAR I think I have it down, but what I need to learn is how to make this work with different files? I want to make a program that uses different files but also saves on the compilation time, basically a method where if I edit one item it won't cause a 15 min compilation period to change everything, which is why, even though this works fine on one program it's not really what I want.

What I've learned so far is : 1. need header files. 2. need a(or many) different cpp files. 3. need to connect them.

If you can and will help, I would like a detailed explanation please, if you can add things like better methods than my current method, shortcuts or anything of that sort I would really appreciate it. Also, I know how to select and answer and up-vote.Thanks in advance.

 #include <iostream>
 #include <string>
 using namespace std ;

 class Drink
{
public:
    void output_information()
    {
        cout << "Details: " << info << endl ;
    }
    string info ;
};

int main()
{
    string input ;

    Drink Arizona_Green_Tea ;
    Arizona_Green_Tea.info = "\n Premium Brewed Green Tea \n Filtered Water \n High-
        Fructose Corn Syrup \n Honey \n Citric Acid \n Natural Flavors \n Ginseng
        Extract \n Ascorbic Acid " ;

    Drink Honest_Ade_Green_Tea ;
    Honest_Ade_Green_Tea.info = "\n FILTERED WATER \n ORGANIC CANE SUGAR \n FAIR
        TRADE ORGANIC GREEN TEA LEAVES \n ORGANIC HONEY \n NATURAL FLAVOR \n ASCORBIC 
        ACID (VITAMIN C) \n CITRIC ACID" ;

    Drink Vitamin_Water_power_c ;
    Vitamin_Water_power_c.info = "reverse osmosis water \n crystalline \n fructose
        and cane sugar (sweeteners) \n less than 0.5% of: \n vitamin C (ascorbic acid),
        citric \n acid, natural flavors, dragonfruit \n extract, vegetable juice 
       (color), \n magnesium lactate and calcium \n lactate and potassium phosphate \n 
       (electrolyte sources), taurine, \n vitamin B5 (calcium pantothenate), \n zinc
        gluconate, vitamin B6 \n (pyridoxine hydrochloride)\n vitamin B12 \n chromium 
        polynicotinate" ;

cout << "Search Database: " ;
getline(cin , input) ;
cout << endl ;

if(input == "Arizona Green Tea" || input == "arizona green tea" || input == "ARIZONA
    GREEN TEA")
    {
    Arizona_Green_Tea.output_information() ;
    }
if(input == "Honest Ade Green Tea")
    {
    Honest_Ade_Green_Tea.output_information() ;
    }
if(input == "Vitamin Water Power" || input == "vitamin water power-c" || input ==
    "VITAMIN WATER POWER C" || input == "Vitamin Water Power C" || input =="vitamin 
    water red" || input == "Vitamin Water Red" )
     {
    Vitamin_Water_power_c.output_information() ;
     }

     return 0 ;
 }
有帮助吗?

解决方案

If you have a source file foo.cpp, you can build an executable zap from it like so:

g++ foo.cpp -o zap

Or you can build the executable in two steps, first building the object file foo.o from foo.cpp, then building zap from foo.o:

g++ -c foo.cpp -o foo.o
g++ foo.o -o zap

(The reason for doing it in two steps isn't immediately obvious, but bear with me.)

If you have two source files, foo.cpp and bar.cpp, you can build the executable from them like so:

g++ foo.cpp bar.cpp -o zap

or in two stages, like so:

g++ -c foo.cpp -o foo.o
g++ -c bar.cpp -o bar.o
g++ foo.o bar.o -o zap

After you have done this, suppose you modify foo.cpp. You can rebuild zap this way:

g++ -c foo.cpp -o foo.o
g++ foo.o bar.o -o zap

There's no reason to rebuild bar.o, because you haven't changed bar.cpp and you still have bar.o left over from the earlier build.

(A utility like Make can help you manage this process, handling the different steps and keeping track of which files are up to date, but let's not try to do too much at once.)

Now, how to divide the code up into different files? This gets into the topic of a translation unit, which is subtle, but generally declarations go into header files, and a source file contains a set of related functions. If there is a class Foo, the class definition (which includes the member function declarations) goes into Foo.h, and the member function definitions go into Foo.cpp.

So in your case you'd have Drink.h:

#include <string>
using std::string;

class Drink
{
public:
  void output_information();
  string info;
};

Drink.cpp:

#include "Drink.h"
#include <iostream>

using namespace std; // saves typing, but not a good idea generally

void Drink::output_information()
{
    cout << "Details: " << info << endl;
}

and Main.cpp:

#include "Drink.h"
#include <iostream>

using namespace std; // saves typing, but not a good idea generally

int main()
{
  ...
}

其他提示

Think about making the program read the drink descriptions from a file. That way you won't have to recompile when your drink recipes change.

Words for today:google, ifstream, vector.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top