how to pass pointer to constructor in c++ - c++

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.
#include <iostream>
using namespace std;
class Base {
protected:
int *num;
public:
Base(int *num);
virtual void print();
};
Base::Base(int *num){
this->num = num;
};
class Derived : public Base {
public:
Derived(int *num) : Base(*num);
void print();
};
Derived::print(){
cout << "int value : " << *(this->num);
};
int main(){
int num = 5;
int *p = &num;
Derived derived(p);
derived.print();
return 0;
}

In the constructor initializer list of Derived you have to write Base(num) instead of Base(*num) as shown below:
Derived(int *num): Base(num)
{
//code here
}
Note that you don't have to dereference the pointer num which you were doing while using it in the constructor initializer list. When you dereference num, you got an int. And so you were passing that int to the Base constructor.

Related

Return class object with member variable

Why the function test() works even I'm not returning a Base class ? What happens with the compilation ? Can someone explain me ?
#include <iostream>
class Base {
public:
Base(){}
Base(int val): _val(val){};
~Base(){};
Base test(int n){
return (n);
}
int &operator *() { return (_val); };
private:
int _val;
};
int main()
{
Base base;
Base a;
a = base.test(42);
std::cout << *a << std::endl;
return (0);
}
You declared a constructor that takes in an int, and you declared that test(int n) should always return a Base class. The compiler knows that in order to create a Base object you need either nothing (default constructor) or an int, so it creates an object using the constructor that takes an int an returns that.
If you wanted to, you could be explicit about it and do something like the following and get the exact same behaviour:
Base test(int n){
return Base(n);
}
In short, n is implicitly cast to a Base object, as you declared a constructor that requires only an int.

Why is only derived class member function called?

In the following code:
Please tell me why only derived class member function is called, is it because of hiding? And why an error doesn't occur in b.func(1.1);, the parameter is int x.
#include <bits/stdc++.h>
using namespace std;
class A {
public:
virtual int func(float x)
{
cout << "A";
}
};
class B : public A {
public:
virtual int func(int x)
{
cout << "B";
}
};
int main()
{
B b;
b.func(1); // B
b.func(1.1); // B
return 0;
}
Yes it's because of hiding. This is one of the main reasons you should always add the special identifier override on functions you intend to override:
class B : public A {
public:
virtual int func(int x) override // <- Note the override here
{
cout << "B";
}
};
This will cause the compiler to complain about not actually overriding.
And floating point values can be implicitly converted to integers.

shared pointer isn't working when casting?

casting shared pointer from B class to A class is not working the console output isn't 12 it's (it outputs A's x but i want B's x)(probably an other memory address). what's wrong with my code
#include <iostream>
#include <memory>
class A
{
public:
int x;
};
class B : public A
{
public:
B(){}
B(const B*){}
int x = 12;
};
std::shared_ptr<A> create()
{
return std::make_shared<B>(new B);
}
int main(){
std::shared_ptr<A> p;
p = create();
std::cout << p->x << std::endl;
std::cin.get();
return 0;
}
A::x and B::x are different objects. Variable access is never polymorphic in C++, so when you access p->x, you're accessing A::x. A::x was never initialized though, so the behavior of your program is undefined.
You need to either have your derived class's constructor initialize the base class's object or delegate that responsibility to the base class's constructor:
class A
{
public:
A(int x) : x{x} {}
int x;
};
class B : public A
{
public:
B() : A{12} {}
};
Live Demo
Alternatively you could wrap x in a virtual accessor method:
class A
{
public:
virtual ~A() = default;
virtual int x() const = 0;
};
class B
{
public:
int x() const override
{
return x_;
}
private:
int x_ = 12;
};
Live Demo

Not able to call base class constructor from dericed class with parameter in c++

I have below scenario in c++ within DEV c++
class base{
int i=0;
public:
base(){
cout<<"base default constructor"<<endl;
}
base(int i){
cout<<"base with int"<<endl;
}
};
class derive : public base{
int j;
public:
derive(){
cout<<"derive default";
}
derive(int i){
int k=5;
base(k);//////error
cout<<"derived with int "<<i<<endl;
}
void fun(int i){
cout<<"finction "<<i<<endl;
}
};
int main()
{
derive d(9);
}
After running following error is coming
[Error] conflicting declaration 'base k'
[Error] 'k' has a previous declaration as 'int k'
Why this error is coming.
Its working fine when i call base(5) with any parameter except declared in derived constructor.
Thanks in advance.
If you call the base constructor within the derived class on the implicit parameter, it needs to be done in the field initialization list like so:
derive(int i) : base(i) {
cout<<"derived with int "<< i <<endl;
}
live demo
#include <iostream>
using namespace std;
class base{
public:
base(){
cout<<"base default constructor"<<endl;
}
base(int i){
cout<<"base with int "<<i<<endl;
}
};
class derive : public base{
public:
int j;
int k;
derive(){
cout<<"derive default\n";
}
derive(int i){
k=5;
base(this->k);//////error
cout<<"derived with int "<<i<<endl;
}
void fun(int i){
cout<<"finction "<<i<<endl;
}
};
int main()
{
derive p(10);
}
There are two interpretations of what base(k) could be:
it could be a the creation of temporary
it could be a variable declaration: the variable name can be in parenthesis
Out of these two choices, the compiler uses the second one: if something can be either a declaration or an expression, the declaration is chosen. This is related to the most vexing parse although it isn't that. The effect is that in your use base(k); tries to define a variable named k of type base.
You can disambiguate the meaning using one of
(base(k));
base{k};
If base also had a constructor taking an std::initializer_list<int> these two can have different meanings.
In main()
base b(3);
derive d(9);
base b1(2);
//create explicit object and pass integer parameter.then try
also pass particular integer value in derive->base.
in derive
base(8);
And always print the value of variable acting weirdly in a program

How to use a copy constructor with a base class?

I'm confused about base classes and copy constructors.
Say I have a class
class A {
public:
A(int m) : m(m) { return; }
virtual ~A() { return; }
int m;
}
And a class that inherits this
class B : public A {
public:
B(int n, int m) : A(m), n(n) { return; }
vitual ~B() { return; }
int n;
}
When I copy class B, how do I ensure that the m value in class A is copied as well?
The default copy constructor will copy all of the member variables, no matter whether they reside in the base class or a derived class.
If you create your own copy constructor for class B you will need to copy the class A members yourself, or better yet use the copy constructor for class A in the initializer list.
class B : public A {
public:
// ...
B(const B & b) : A(b), n(b.n) {}
// ...
};
The base copy constructor will be called before the derived copy constructor automatically. It's the same as for a regular constructor. (More accurately, the base constructor is called by derived initialization list before derived constructor continues). In your case, the default copy constructors are sufficient.
Be careful, however, that you do not copy a B into an A (search object splicing).
This code is your code just copy constructors are added with a class:
#include <iostream>
using namespace std;
struct Int
{
Int ()
{
}
Int(const Int&)
{
cout<< "Int copied" "\n";
}
};
class A
{
Int m;
public:
A(Int m) : m(m)
{
}
virtual ~A()
{
}
};
class B : public A
{
Int n;
public:
B(Int n, Int m) : A(m), n(n)
{
}
virtual ~B()
{
}
};
void f(B)
{
}
int main()
{
Int m,n;
B b(m,n);
cout<< "\n" "start:" "\n";
f(b);
cout<< "end";
}
Ubuntu Qt Creator Output says both are copied.
One can think there's a hidden member with type B in A and this hidden member can be initialized or copied as a usual member as in the above program with:
B(Int n, Int m) : A(m), n(n)
You should not return from a constructor/destructor, not even void.