I am trying to compile a C++ program. I am learning the "virtual functions" concept. So I was trying to see what happens when we override a function without making it "virtual".
I have three directories headers, sources and objects.
In the header directory, I have 2 files, Pokemon.h and Charmander.h .
In the sources directory, I have 3 files Pokemon.cpp, Charmander.cpp and Main.cpp.
class Charmander in Charmander.h, inherits from class Pokemon from Pokemon.h.
This is Pokemon.h in the "headers" directory:
/********************************************************************************
* Pokemon -- *
* This is the base class for all pokemons. Every pokemon will inherit from *
* this class. *
* *
* Author -- Aditya R.Singh *
* Version -- 1.0 *
* Since -- 2014-06-21 *
********************************************************************************/
#ifndef POKEMON_H
#define POKEMON_H
using namespace std;
class Pokemon {
public:
string type();
string attack();
string weakness();
};
#endif
This is Charmander.h in the "headers" directory:
/*******************************************************************************
* Charmander -- *
* This class is the derived class from class Pokemon. This is specialized for *
* the pokemon Charmander. *
* *
* Author -- Aditya R.Singh *
* Version -- 1.0 *
* Since -- 2014-06-18 *
*******************************************************************************/
#ifndef CHARMANDER_H
#define CHARMANDER_H
#include "Pokemon.h"
using namespace std;
class Charmander: public Pokemon {
public:
string type();
string attack();
string weakness();
};
#endif
This is the Pokemon.cpp in the "sources" directory:
/**********************************************************************************
* Pokemon -- *
* This is the implementation of functions in class Pokemon. *
* This is a generic class for every pokemon. *
* *
* Author -- Aditya R.Singh *
* Since -- 1.0 *
* Version -- 2014-06-21 *
**********************************************************************************/
#include <iostream>
#include "../headers/Pokemon.h"
using namespace std;
string Pokemon::type() {
return "Normal";
}
string Pokemon::attack() {
return "Headbutt";
}
string Pokemon::weakness() {
return "Fighting";
}
This is the Charmander.cpp file in the "sources" directory:
/**********************************************************************************
* Charmander -- *
* This is the Charmander class's functions implementations. *
* This is specific to Charmander. *
* *
* Author -- Aditya R.Singh *
* Since -- 1.0 *
* Version -- 2014-06-21 *
**********************************************************************************/
#include <iostream>
#include "../headers/Charmander.h"
using namespace std;
string Charmander::type() {
return "Fire";
}
string Charmander::attack() {
return "Fireflame";
}
string Charmander::weakness() {
return "Water";
}
And this is the Main.cpp file in the "sources" directory:
/**********************************************************************************
* Main -- *
* This program will show the use of Polymorphism and Inheritance in C++. *
* *
* Author -- Aditya R.Singh *
* Since -- 1.0 *
* Version -- 2014-06-21 *
**********************************************************************************/
#include <iostream>
#include "../headers/Pokemon.h"
#include "../headers/Charmander.h"
using namespace std;
int main() {
Pokemon *charmander = new Charmander;
cout << "Charmander is a " << charmander->type() << " type pokemon." << endl;
cout << "Charmander can do a " << charmander->attack() << " attack." << endl;
cout << "Charmander is weak against " << charmander->weakness() << " type pokemon." << endl;
return 0;
}
Now I compiled the Pokemon.cpp and put the object file in the "objects" directory... Currently my terminal is in the "Program" directory...
gcc -c sources/Pokemon.cpp -o objects/Pokemon.o
I compiled the Charmander.cpp and the put the object file in the "objects" directory... like this ..
gcc -c sources/Charmander.cpp -o objects/Charmander.o
It's working fine uptill now. I have the object files in the "objects" directory now.
But now I am trying to compile my Main.cpp like this..
gcc sources/Main.cpp -o Main objects/Pokemon.o objects/Charmander.o
But my GCC compiler gives some big error stack like, undefined symbols for architecture x86_64.
I am using a mac book pro, 64 bit machine. OS Mac OS X maverics.