Thread: Abstract base class

  1. #1
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68

    Abstract base class

    Hello, I want to use abstract base classes in my code but I'm having trouble doing this. I keep getting an error that say my derive constructor already has a body. I need to initialize the variables of my derive class but I really want to use the polymorphism effect I could get from my base class.


    Is there an existing compromise? Can a derive class constructor have a body? Can a abstract base class constructor have a body? How do I initialize my derive class variables to set it up if it can't have a body? What would you do?
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Derived classes can have a copy constructor and a definition obviously needs a body. Show the smallest possible compilable example that demonstrates the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    In case you're missing out on some syntax, a simple example.

    Code:
    // DerivedBase.cpp : Defines the entry point for the console application.
    //
    
    
    #include "stdafx.h"
    #include <iostream>
    
    
    class Base
    {
    public:
    	Base ( );
    	~Base ( );
    
    
    	int getData ( );
    
    
    	virtual void Print ( ) = 0;
    
    
    private:
    	int data;
    };
    
    
    Base::Base ( )
    {
    	data = 10;
    }
    
    
    Base::~Base ( )
    {
    
    
    }
    
    
    int Base::getData ( )
    {
    	return data;
    }
    
    
    class Derived : public Base
    {
    public:
    	Derived ( );
    
    
    	~Derived ( );
    
    
    	void Print ( );
    
    
    private:
    	int score;
    
    
    };
    
    
    Derived::Derived ( ) : Base ( )
    {
    	score = 30;
    }
    
    
    Derived::~Derived ( )
    {
    
    
    }
    
    
    void Derived::Print ( )
    {
    	std::cout << "Data value " << getData ( ) << "  Score Value " << score << std::endl;
    }
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	Derived derived;
    	derived.Print ( );
    	std :: cin.get ( );
    }

  4. #4
    The Negativity. LAURENT*'s Avatar
    Join Date
    May 2012
    Posts
    68
    thank you, this was just what I needed.
    UPDATE! As of 10/6/2014

    https://www.dropbox.com/s/2sj6qwpfbb...t%201.zip?dl=0
    Just find the application file and double click it. Controls are (Arrow keys) (Z) (Z + arrow key) (Spacebar)
    Don't play this crappy update. Wait for the newest one which is far more impressive.

    Official Sonic character poll hosted by some guy at Sega..... Sega of America. Vote for blaze because she OP.
    http://blogs.sega.com/2015/08/28/the...down-heats-up/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloading with abstract base class
    By R.Stiltskin in forum C++ Programming
    Replies: 4
    Last Post: 12-04-2010, 02:19 AM
  2. Array of Base(Abstract) Class Problem
    By Aramil in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2008, 02:58 PM
  3. Linked list with abstract base class?
    By mariolov in forum C++ Programming
    Replies: 5
    Last Post: 12-21-2007, 07:00 PM
  4. Abstract Base Class and References
    By Thantos in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2004, 01:35 PM
  5. Abstract Base Class discription.
    By curlious in forum C++ Programming
    Replies: 8
    Last Post: 11-08-2003, 04:24 PM