I finally see the benefit of information hiding so I am trying to learn it. I have the header class, implementation class, and the main function setup correctly but when I include a ".h" header file as my book and Bucky [youtube] implies, I get an error.
This is the error in the main function
C:\Users\D Taqee\Desktop\C++\Dummy Folder - All Practice Exercises\Dummy Code\addemup learning to hide info.o:addemup learning to hide info.cpp|| undefined reference to `addemup::addemup()'|
And my header file won't even compile because it is looking for an .exe file that does not exist.
BUT, when I include the ".cpp" file instead of the ".h" file the program runs just fine. Can someone please explain what is happening and how can I include the .h file as it should be. Thanks.
#include <iostream>
#include <string>
#include "addemup.h"
usingnamespace std;
int main()
{
addemup math;
int f, s;
cout << "Enter 2 numbers to add and multiply"<<endl<<endl;
cout<<"First number: ";
cin>>f;
cout<<"Second Number: ";
cin>>s;
cout<<endl<<endl;
cout<<"The numbers added together are "<<math.addition(f,s)<<endl;
cout<<"The number subtracted from each other are "<<math.subtraction(f,s)<<endl;
return 0;
}
Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef ADDEMUP_H
#define ADDEMUP_H
class addemup
{
public:
addemup();
int addition(int, int);
int subtraction(int, int);
};
#endif // ADDEMUP_H
#include <iostream>
#include <string>
#include "addemup.h"
usingnamespace std;
addemup::addemup()
{
cout<<"yeehaw"<<endl;
}
int addemup::addition(int a, int b)
{
return a + b;
}
int addemup::subtraction(int a, int b)
{
return a - b;
}