Thread: Derived class is still an abstract class, not sure why. Help please!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Derived class is still an abstract class, not sure why. Help please!

    So basically I'm making a program that has a Vehicle abstract base class and derived classes Truck and Car. From my understanding, a derived class must simply override all pure virtual functions in the abstract base class, and then objects of that derived class can be instantiated. However, I think that I have done this but the compiler still tells me that I cannot create an object of class Car or Truck. I can't figure out why. There are functions that Car needs and Truck doesn't and vice versa, so I thought that I could just override the unneeded functions, make them do something useless, and that would allow me to instantiate objects of Car and Truck, but this is not working. I'll post the necessary files, and if someone could tell me what's going wrong, or maybe just better explain to me how to deal with pure virtual functions that I don't need in derived classes, that'd be great.

    Code:
    //Vehicle.h
    #include <iostream>
    
    class Vehicle
    {
    	
    public:
    	virtual void draw() = 0;
    	virtual void setTonages() = 0;
    	virtual double getGrossTon() const = 0;
    	virtual double getNetTon() const = 0;
    	virtual void setModel() = 0;
    	virtual std::string getModel() const = 0;
    	virtual void print() = 0;
    	void setColumn(int);
    	int getColumn() const;
    	void indentCol(const int&);
    	
    private:
    	int columns;
    };
    
    #endif
    Code:
    //Truck.h
    #include "Vehicle.h"
    
    class Truck : public Vehicle
    {
    public:
    	virtual void draw();
    	virtual void setTonages();
    	virtual double getGrossTon() const;
    	virtual double getNetTon() const;
    	virtual void print();
    	
    private:
    	double grossTon;
    	double netTon;
    
    	virtual void setModel()
    		{  std::cout << "Empty function";  };
    	virtual void getModel()
    		{  std::cout << "Empty function";  };
    };
    Code:
    //Car.h
    #include "Vehicle.h"
    #include <iostream>
    
    class Car : public Vehicle
    {
    public:
    	virtual void draw();
    	virtual void setModel(const std::string &);
    	virtual std::string getModel() const;
    	virtual void print();
    
    private:
    	std::string model;
    	virtual void setTonages()
    		{   cout << "Empty function";  };
    	virtual double getGrossTon() const
    		{   cout << "Empty function";  };
    	virtual double getNetTon() const
    		{   cout << "Empty function";  };
    };
    Those are the 3 header files for my classes; I believe that something must be wrong with them. The client file that is actually trying to instantiate objects just gives me the error that my Car and Truck classes are still abstract. Please help me figure out why! Thanks.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    In Vehicle, getModel() returns a std::string, but in Truck, it returns nothing, so it's not really implementing it. Similarly, Vehicle has setModel() which has no parameters, but Car has setModel() which takes const std::string&, so it's not really implementing Vehicle::setModel().

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    So, I have to use the same return type and parameters as functions I'm trying to override? I didn't think that that's what I learned, and I can't find an example of this anywhere in my book. Could someone maybe give an example of what it should look like? It's still not working for me.

    EDIT: reread what you said and what I had, I believe I have fixed it now. Thank you.

    EDIT 2: hmm, it was for Car but not Truck still.

    Edit 3: had to change getModel in Truck to const, apparently that matters too. Thanks for the help.
    Last edited by Freddy92; 04-04-2011 at 12:18 AM.

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Ok so I was a little bit wrong. But only with some technical details; the errors are from the same mistakes.

    If you just use the wrong return type, it's a compilation error right there regardless of whether you instantiate the class - gcc says "Conflicting return types". If you get the parameters wrong, it is interpreted as overloading, i.e. it might as well have a different name - it's seen as a different function so the class itself compiles, but you can't instantiate it because the Base class method is still unimplemented.

    Here's some code to demonstrate:

    Code:
     1 class Base
     2 {
     3 public:
     4     virtual int F(double) = 0;
     5 };
     6
     7 class Derived1 : public Base
     8 {
     9 public:
    10     virtual int F() { }
    11 };
    12
    13 class Derived2 : public Base
    14 {
    15 public:
    16     virtual void F(double) { } // Error: conflicting return type
    17 };
    18
    19 class Derived3 : public Base
    20 {
    21 public:
    22     virtual int F(double) { }
    23 };
    24
    25 int main()
    26 {
    27     Derived1 d1; // Error Derived1 is abstract
    28     Derived2 d2; // See line 16
    29     Derived3 d3; // Ok
    30
    31     return 0;
    32 }

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Thanks a lot, that really helped. I got my problem all fixed up and my whole program is working now. Thank you good sir!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. derived class
    By BKurosawa in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2007, 02:18 PM
  2. C++ Newbie - Derived Class Issue
    By Gipionocheiyort in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2007, 12:20 AM
  3. Derived class can't declare
    By hdragon in forum Game Programming
    Replies: 13
    Last Post: 03-21-2006, 02:48 PM
  4. simple question
    By SuperNewbie in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2005, 03:03 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM