inheritance - C++ Interface issues with creating an instance of it -
so have assignment in c++ class asking me create abstract base class called project, , interface called task. gave driver code main, , within it's asking create instance of interface "task", , keep getting error saying " cant create instance of abstract base class" here's "task" interface header file , main.. hope it's enough.
#pragma once #ifndef task_h #define task_h class task { public: task(); virtual ~task(); virtual void addprereq(task *pt) = 0; virtual bool ready() = 0; virtual bool done() = 0; virtual void doit() =0; }; #endif
and main:
#include "stdafx.h" #include "compositeproject.h" #include "minorproject.h" #include "simpleproject.h" #include "task.h" int _tmain(int argc, _tchar* argv[]) { // create tasks task* tasks[7]; (int = 0; != 7; ++i) tasks[i] = new task();//this error coming // set prerequisites tasks[1]->addprereq(tasks[0]); tasks[2]->addprereq(tasks[1]); tasks[3]->addprereq(tasks[2]); tasks[3]->addprereq(tasks[4]); tasks[4]->addprereq(tasks[0]); tasks[5]->addprereq(tasks[4]); tasks[6]->addprereq(tasks[3]); tasks[6]->addprereq(tasks[5]); return 0; }
now knowledge difference between abstract base class , interface abstract base class has protected constructor, , has atleast 1 pure virtual method. interface has it's methods pure virtual, have template. don't understand how or why teacher asking create instance of interface task. explicitly says elsewhere in assignment, expected files " task.h , task.cpp ". feel there's key element i'm missing here. thanks!
by default can not create object of abstract base class because abc acts blueprint on create actual implementation. give example can define shape
abstract base class has method called virtual void form()=0;
redefining form of geometric shape , on class can define class called cube
, 1 calledcircle
but each 1 of classes have inherited void form();
function need redefine both cube
, circle
classes...so going problem abstract class should called project (as saying) , code should this
#ifndef task_h #define task_h class project { //project abstract base class public: project(); virtual ~project(); virtual void addprereq(project *pt) = 0; virtual bool ready() = 0; virtual bool done() = 0; virtual void doit() =0; }; class task:public project { task(); virtual ~task(); virtual void addprereq(task *pt); virtual bool ready(); virtual bool done(); virtual void doit(); } #endif // project_h
in conclusion:
- when class declaration contains pure virtual function, can't create object of class.
- any method in abc not virtual inherited defined, if have
void move();
function inshape
class can use function in inherited classes don't need declare functions virtual - an abc describes interface uses least 1 pure virtual function, , clases derived abc use regular virtual functions implement interface in terms of properties of particular derived class
Comments
Post a Comment