0
how to pass pointer to constructor in c++
i have a derived class that needs to take a pointer as a constructor for the base class. how do you do this in c++, i tried it but it gave me a bug. https://code.sololearn.com/cr3r6Rv2teYp/#cpp
1 Answer
+ 1
#include <iostream>
using namespace std;
class Base{
public:
int *num;
Base(int *num);
virtual void print(){}; //print defination, virtual function must be defined, in base class.
};
Base::Base(int *num){
this->num = num;
}
class Derived : public Base {
public:
Derived(int *num) : Base(num){}; //passing pointer
void print();
};
void Derived::print(){
cout << "int value : " << *(this->num);
};
int main(){
int num = 5;
int *p = #
Derived derived(p);
derived.print();
return 0;
}