Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More
( 8 months ago )

How to override abstract class constructor in sub class

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Kajal Gaur

User

( 8 months ago )

You seem to misunderstand how the constructor initializer list works.

It's used in the definition (i.e. implementation) of the constructor, it's not needed for the declaration of your constructor.

In fact, your current declaration in the AlienSpaceship class actually defines (implements) the constructor, because of the ending {}.

So for the class, all you need to do is

class AlienSpaceship: public Spaceship{
    ...
    public:
        AlienSpaceship(float, float, float, float, OrganismType);
};

usr_profile.png
Yasmin Mirza

User

( 8 months ago )

 

Ive just moved from java to c++ and I'm really struggling with what were once basic concepts. All I want to do is create an abstract class called 'Spaceship', give it a constructor that takes 4 arguments, and then in a sub class called 'Alienspaceship', I want to make a constructor that takes 5 arguments but inherits the 'spaceship' constructors first 4 arguments. Ive been looking all over the place and can't seem to find exactly what I'm looking for. Below is my code. Please and thank you to anyone who can help me!

header

#ifndef SPACESHIP
#define SPACESHIP

#include "Enum.h"

class Spaceship{
    public:
    float health;
    float food;
    float cash;
    float misc;

public:
    Spaceship(float, float, float, float);

};

class AlienSpaceship: public Spaceship{
    OrganismType race;

    public:
        AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float){}
};

#endif

cpp

#include "Spaceship.h"

Spaceship::Spaceship(float health, float food, float cash, float misc){
    this->health = health;
    this->food = food;
    this->cash = cash;
    this->misc = misc;
}

AlienSpaceship::AlienSpaceship(float health, float food, float cash, float misc, OrganismType race):Spaceship(health, food, cash, misc){
    this->race = race;
}

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif