If I have only the object file of a C++ program can I use this object file as a base class to inherit the classes contained in them?
Or rather, is it possible to distribute the object files of my C++ programs when i need to use the classes conatained in them to create other newer classes from them??
If this is possible how do I do this? anyone has any examples?
C++ questions
Re:C++ questions
You can distribute code as object files rather than source, but you'd need to distribute the header files with them so that code calling the compiled code has the necessary class definitions, function prototypes and so on. This is exactly how the standard libraries work; indeed, you will probably want to collect your .o (or .obj in Windows) files into a library file (static libraries are .a in Unix and .lib in Windows; dynamic libraries are .so and .dll, respectively). How you would do this will depend on the development environment you are using.
Re:C++ questions
i tried the following but it doesnt work.
the first file a.cpp has the following
the next b.cpp has
i tried compiling separately but b.cpp gives me an error. what should i do?
the first file a.cpp has the following
Code: Select all
#include<iostream.h>
class a{
protected:
public:
a(){
cout<<"In a constructor"<<endl;
}
void meth1() {
cout<<"Method 1"<<endl;
}
};
Code: Select all
#include<iostream.h>
class b: public a
{
public:
b():a()
{
cout<<"In B constructor"<<endl;
}
void meth1()
{
a::meth1();
cout<<"Method 1"<<endl;
}
};
int main()
{
int a=10;
b b1;
b1.meth1();
cout<<"anselm"<<a<<endl;
}
Only Human
Re:C++ questions
Does the error message say " 'a' undeclared"?
In order to have class b inherite from a, the compiler needs to know what does the class look like. Furthermore, it needs to know it's a class. And so you need to include the class a definition:
Usually such information is stored in header files.
HTH
Adrian.
In order to have class b inherite from a, the compiler needs to know what does the class look like. Furthermore, it needs to know it's a class. And so you need to include the class a definition:
Code: Select all
#include<iostream.h>
//now a class' info
class a{
protected:
public:
a();
void meth1() ; //only the definition. the func's body is in the object file
}
};
//now the rest of b.cpp
class b: public a
{
public:
b():a()
......
Code: Select all
//listing: a.hpp
#ifndef __A_HPP
#define __A_HPP
class a{
protected:
public:
a();
void meth1() ; //only the definition. the func's body is in the object file
}
};
#endif
//listing: a.cpp
#include<iostream.h>
#include "a.hpp" //after this line, use "a" as much as you want.
class b: public a
{
public:
b():a()
...............
Adrian.
Re:C++ questions
EDIT: Oh, well, it looks like I was beaten to the punch by Adek. This is what comes of taking too long to write these damn things out.
This is what I was saying before about having to have the class definitions visible to the referencing code. You generally want to have class definitions in header files which the referring code can then [tt]#include[/tt].
In the case given above, all of the method functions are inline, so there is nothing to separately compile. If you want to have the function implementations separate from the class definitions, you need to have a separate source file for those.
Here is a version of the code you wrote, broken down in the manner described:
Note that the header declarations for IOSTREAM have been changed in the newer C++ standard, and that you need to have a [tt]using namespace[/tt] statement to make the I/O stream classes visible, as well. Also, while variable names can be scope-overridden, class names cannot be; declaring a variable 'a' after declaring a class 'a' causes a type-resolution conflict.
Finally, you will need to link the object files together before you can produce an executable; how you would do this will depend on your development kit. Most IDEs support some form of project or make file, which will allow you to link the files automagically after compilation. For a command line compiler, you can either compile the separate file then link them manually, or you can use a make utility like the standard GNU make (the gcc compiler-driver allows you to link at compile time, but any non-trivial compile would need too long an options list for this to be feasible). A simple make file for this (assuming Linux) might be something like this:
Note that this has not been tested; you would probably have to add the appropriate flags and so forth to make it work correctly.
This is what I was saying before about having to have the class definitions visible to the referencing code. You generally want to have class definitions in header files which the referring code can then [tt]#include[/tt].
In the case given above, all of the method functions are inline, so there is nothing to separately compile. If you want to have the function implementations separate from the class definitions, you need to have a separate source file for those.
Here is a version of the code you wrote, broken down in the manner described:
Code: Select all
// a.h - class definition for class a
// add a header guard to prevent duplicate includes
#ifndef __A_H__
#define __A_H__
class a {
protected:
public:
a();
void meth1();
};
#endif
Code: Select all
// a.cpp - method implementations for class a
#include "a.h"
#include <iostream>
using namespace std;
a::a(){
cout << "In a constructor" << endl;
}
void a::meth1() {
cout << "Class a Method 1" << endl;
}
Code: Select all
// main() program, with local definition of class b
#include <iostream>
#include "a.h"
using namespace std;
class b: public a
{
public:
b():a()
{
cout << "In b constructor" << endl;
}
void meth1()
{
a::meth1();
cout << "Class b Method 1" << endl;
}
};
int main()
{
b b1;
b1.meth1();
cout << "anselm" << endl;
}
Finally, you will need to link the object files together before you can produce an executable; how you would do this will depend on your development kit. Most IDEs support some form of project or make file, which will allow you to link the files automagically after compilation. For a command line compiler, you can either compile the separate file then link them manually, or you can use a make utility like the standard GNU make (the gcc compiler-driver allows you to link at compile time, but any non-trivial compile would need too long an options list for this to be feasible). A simple make file for this (assuming Linux) might be something like this:
Code: Select all
# makefile for b
b: a.o b.o
???gcc a.o b.o -o b
a.o: a.cpp a.h
???gcc -c a.cpp -o a.o
b.o: b.cpp a.h
???gcc -c b.cpp -o b.o
Re:C++ questions
oops i guess my last post didnt show up. what i had said was, thanks to schola-r-lea i used header files and got it working. thanks for all your replies guys
Only Human