c++, Base class constructor seperate declearation and implementation -


in c++ program have separate .h , .cpp files , working far except when want use base class constructor derived class. working if put function definition if class deceleration. here's working code .h file.

#include <iostream> using namespace std;  class property {     public:         property();         property(const property & src);         property(int src);         ~property();         virtual int disp() const = 0;         int get_ownable();               private:      protected:          int ownable; };  class rr : public property {     public:     rr();     rr(const rr & src);     rr(int src):property(src)     {cout << "\nderived class called\n";};     ~rr();      virtual int disp() const;            private:         protected:       }; 

the imp.cpp (implementation) file is

#include "head.h"  #include <iostream>   using namespace std;   //property class implimentations  property::property() {     ownable = 0; }   property::property(const property & src) {     ownable = src.ownable; }    property::property(int src) {     ownable = src;     cout << "\nparent class called\n"; }  property::~property() {  }  int property::get_ownable() {     return ownable; }  rr::rr() {} rr::rr(const rr & src) {     ownable = src.ownable; }     /* rr::rr(int src):property(src) {     cout << "\nderived class called\n"; } */    rr::~rr() { } int rr::disp() const { } 

there other code working fine , not connected this. output

parent class called derived class called 

so works fine if un-comment out function in .imp file , remove declaration in .h

    rr(int src):property(src); 

i error

head.h: in constructor 'rr::rr(int)': head.h 113: error: expeted '{' @ end of input imp.cpp: @ global scope: imp.cpp:348:error: redefiniton of 'rr::rr(int); head.h.113: error: 'rr::rr(int); previousle defined here 

all examples can find on line of how functions defined in class declaration. can't find examples of how 2 files. can tell me how define base class constructor call in separate file? on linux system using g++ compiler.

like

baseclass.h

#pragma once  class baseclass { public:     baseclass(int a);  private:     int a_private; }; 

baseclass.cpp

#include "baseclass.h" #include <iostream> using std::cout; using std::endl;  baseclass::baseclass(int a) {     cout << "base class constructor called" << endl;     this->a_private = a; } 

derived.h

#pragma once  #include "baseclass.h"  class derived : public baseclass { public:     derived(int a); private:     int a_private; }; 

derived.cpp

#include "derived.h" #include <iostream> using std::cout; using std::endl;  derived::derived(int a) : baseclass(a) {     cout << "derived class constructor called" << endl;     this->a_private = a; } 

main.cpp

#include "baseclass.h" #include "derived.h"  int main() {     derived d(2);      return 0; } 

compiling command g++ main.cpp derived.cpp baseclass.cpp , running result in following output

base class constructor called derived class constructor called 

as mentioned in comments (credits @igortandetnik), initializer lists should used in implementation file. not in header file (provided class not templated).


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -