inheritance - What is a correct way of using an implementation with an interface class in C++? -
i have class child
, class human
human
has functions declared in child
virtual functions. , class child
inherits human
class.
i want use human
interface file hide implementation in child
.
i didn't set constructor, set init()
function initializes basic settings.
now, way can use child
functions using human
interface file?
i tried
human *john = new child();
but got following errors.
main.cpp:7: error: expected type-specifier before ‘child’ main.cpp:7: error: cannot convert ‘int*’ ‘human*’ in initialization main.cpp:7: error: expected ‘,’ or ‘;’ before ‘child
i don't understand int*
came either. none of 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 #include "child.h"
in cpp
file. can along including human.h
or can not include human.h
brought in automatically include within child.h
#include <stdlib.h> #include <stdio.h> #include "child.h" #include "human.h" // no longer necessary, child.h includes using namespace std; int main(){ human *john = new child(); return 0; }
Comments
Post a Comment