Help with Headers

Hi.

Can you explain to me on how to "inherit" (if that's the right word) stuff from a header file, and on how to write one? (A header file)

Like if i wrote this;

class Foo {

int a = 123;

}

in a header, how would i call it?

For example, how would i do this:

Foo(); //Calls the Foo class

Using classes was just an example.
closed account (3hM2Nwbp)
I'm not sure what you mean by "inherit" - either you want to be able to use the header in another file - or polymorphically inherit from another class.

1
2
3
4
5
6
7
8
//ClassA.h
#ifndef CLASS_A_H
#define CLASS_A_H
class ClassA
{
    int a;
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
12
//ClassB.h
#ifndef CLASS_B_H
#define CLASS_B_H

#include "ClassA.h" // <-- This macro is a glorified copy and paste of ClassA.h

class ClassB : public ClassA
{
    // ClassB "is a" ClassA
};

#endif 


1
2
3
4
5
6
7
8
9
10
// Main.cpp
#include "ClassA.h"
#include "ClassB.h"

int main(void)
{
    ClassA classA;
    ClassB classB;
    return 0;
}
Last edited on
#include "ClassB.h" Just copies the whole content of the file to the file it's included in. :P
Topic archived. No new replies allowed.