c++ inheritance with default constructor [duplicate] - c++

This question already has answers here:
no default constructor exists for class x (inheritance) C++
(2 answers)
Default constructor for an inherited class
(2 answers)
Closed 8 years ago.
I have just started to learn inheritance in c++. I have question about it. Here is my code:
class employ
{
int xelfasi;
string manqana;
public:
employ(istream&is)
{
is >> xelfasi >> manqana;
}
~employ()
{
}
};
class manager : public employ
{
int bonusi;
manager(int x)
{
bonusi = x;
}
};
which gives me an error saying: no default constructor exists for class "employ".
I don't really get why I have to make an default constructor. Thanks

manager basically contains an employ (though in the form of inheritance). So to be able to construct a manager, you need to be able to construct the contained employ. So you have to either provide a default constructor for it, or construct it explicitly by calling an existing constructor.
To construct it explicitly, you have to do something like this:
manager(int x): employ(#some istream here#)
{}

Related

Is it possible to combine default member initialization and delegating constructors? [duplicate]

This question already has answers here:
Member initialization while using delegated constructor
(1 answer)
C++ - Is there a workaround to delegating constructors being not the only members in an initialization list
(2 answers)
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Suppose I have a class
class Widget
{
public:
explicit Widget();
explicit Widget(int foo, int bar);
private:
Gadget * gadget_;
int foo_{42};
int bar_{13};
}
with constructors that use some members (therefore initialization order is important):
Widget::Widget()
{
ComplicatedConstructionStandIn::frobnicate_gadget(&gadget_);
TheConstructorHasManyLines(foo_, gadget_);
ItDoesMultipleThings(bar_);
}
Widget::Widget(int foo, int bar)
: foo_{foo}
, bar_{bar}
{
ComplicatedConstructionStandIn::frobnicate_gadget(&gadget_);
TheConstructorHasManyLines(foo_, gadget_);
ItDoesMultipleThings(bar_);
}
Clearly, the second constructor is code duplication so you would think to delegate !BUT! you can't do that if you also want the default member initialization (int foo{42};).
By delegation I mean something like this (does not compile):
Widget::Widget(int foo, int bar)
: foo_{foo}
, bar_{bar}
, Widget()
{}
Is there an elegant way out of this? Wrapping the body of the constructor in an auxiliary init function is just kicking the question down the line.

Object creation with parameters not possible in c++? [duplicate]

This question already has answers here:
Why can't member initializers use parentheses?
(2 answers)
Initialization of member variable via parentheses doesn't work [duplicate]
(1 answer)
can not define and init a class member by Parentheses [duplicate]
(1 answer)
Closed 9 months ago.
I'm trying to neatly create a objects within a class and I've run into what seems to me to be an odd limitation in c++, and I wondered if I'm just missing a syntax trick or whether it's truly impossible; specifically it seems like I cannot explicitly instantiate a class object inside another class if the constructor of the former has parameters (the parameters are constant) generating the odd message: 'Expected parameter declarator'
It seems as though only default constructors are supported in this scenario, or am I missing a bit of magic?
Currently using c++17 (simply because that's the default in this IDE)
class Fred
{
public:
Fred(const int i)
{
}
Fred()
{
}
};
Fred fred1(0); // This compiles
class Charlie
{
Fred fred2(); // This compiles
Fred fred3(0); // This does not compile
};

Using default arguments in constructor [duplicate]

This question already has answers here:
default arguments in constructor
(5 answers)
Closed 9 years ago.
Is it possible to make constructor with default arguments.
Something like this
Object(int size = 1) {
//SMTH
}
I am trying to do like this in visual studio, but error apears.
Is it possible to create constructor with default arguments or it only remains to use overloading ?
EDIT
It is possible, sorry for posting this question, I am newbie in c++. The problem was that I was declaring default argument in source file (cpp) , I have changed my class like this and it works great.
class MyClass
{
public:
MyClass (int i = 0, std::string s = ""); // constructor declared
private:
int x;
int y;
std::string z;
};
MyClass :: MyClass(int i, std::string s) // constructor defined
{
x = 100;
y = i;
z = s;
}
Yes, it is possible to have constructors with default arguments.
Two caveats regarding your particular example:
Make sure you don't also have a no-args constructor as this would lead to ambiguity.
You might want to make this constructor explicit so that it's not used for implicit conversion from int to Object.

what compiler does internally for initializing a variable and assigning a variable while constructing object? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why should I prefer to use member initialization list?
Class A has a member variable i. i can be initialized or assigned during object creation.
A) Initialise
class A {
int i;
public:
A(int _i) : i(_i){}
}
B) assign
class A {
int i;
public:
A(int _i) : { i = _i}
}
My question is what is the basic difference between these 2 approach?
The difference lies in which C++ mechanism is used to initialize i in your class. Case (A) initializes it via constructor, and case (B) uses the assignment operator (or a copy constructor if no assignment operator is defined).
Most C++ compilers would generate exactly the same code for this particular example, because you're using int, which is a "plain old data" type. If i were a class type, it could make a great deal of difference.

C++: Initializing something before the bracket [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does a colon following a C++ constructor name do?
Class construction with initial values
I saw code that looked like this:
class Demo
{
Joystick joystick;
public:
Demo(void):
joystick(1) // The first USB port
{
/* snip */
}
};
Joystick is being initialized before the bracket in the constructor. What does it mean when you do that? What is this called? I'm assuming it differs in some way then initializing joystick inside the bracket -- in what ways does it differ?
It is called an initializer list, and it does differ from initializing inside the body of the constructor.
You can call the constructors of every data member in the class in the initializer list. Also you can call a custom parent class(s) constructor within it, if you didn't, every data member or parent class you don't initialize with the initializer list will be initialized with its default constructor, if it doesn't have, you will see a compiler error.
This is an extended example:
class Parent
{
bool b;
public:
Parent(bool B): b(B)
{
}
};
class Child: public Parent
{
int i;
double d;
public:
Child(int I, double D, bool B): i(I), d(D), Parent(B)
{
}
};
For the order they are called, see this question and this question.
In fact explaining it is an entire article as it's a basic and important thing in classes, just try Googling it and reading some results.