I have a class Child and a class Human where Human has all the functions declared in Child as virtual functions. And a class Child inherits from Human class.
I want to use Human as a interface file to hide implementation in Child.
I didn’t really set up a constructor, but I set up an init() function which initializes basic settings.
Now, what is a good way that I can use Child functions using Human interface file?
I tried
Human *John = new Child();
but I got the following errors.
main.cpp:7: error: expected type-specifier before ‘Child’
main.cpp:7: error: cannot convert ‘int*’ to ‘Human*’ in initialization
main.cpp:7: error: expected ‘,’ or ‘;’ before ‘Child
I don’t understand where int* came from either. None of my functions declared returns int*.
edit
main.cpp
#include <stdlib.h>
#include <stdio.h>
#include "Human.h"
using namespace std;
int main(){
Human *John = new Child();
return 0;
}
human.h
#ifndef __HUMAN_h__
#define __HUMAN_h__
class Human
{
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
};
#endif
Child.h
#ifndef __CHILD_h__
#define __CHILD_h__
#include "Human.h"
class Child : public Human
{
public:
void Init();
void Cleanup();
};
#endif
Child.cpp
#include "Child.h"
void Child::Init()
{
}
void Child::Cleanup()
{
}
Makefile
CC = g++
INC = -I.
FLAGS = -W -Wall
LINKOPTS = -g
all: program
program: main.o Child.o
$(CC) -Wall -o program main.o Child.o
main.o: main.cpp Human.h
$(CC) -Wall -c main.cpp Human.h
Child.o: Child.cpp Child.h
$(CC) -Wall -c Child.cpp Child.h
Child.h: Human.h
clean:
rm -rf program
You need to
#include "Child.h"in yourcppfile. You can do this along with includinghuman.hor you can not includehuman.has that will be brought in automatically by the include withinchild.h