By: Shraa
Hi I am learning CPP and I tried below code but it doesn’t work. #include <iostream> #include<string> using namespace std; class Base { public: int m_id;...
View ArticleBy: Alex
This is a tricky one. You’re declaring object dr with no parameters, but you’ve given it empty parenthesis. This will cause the compiler to think you’re declaring a function prototype for a function...
View ArticleBy: shraa
Thanks Alex but it wont work by removing empty parenthesis as then there will be ambiguous call to overloaded Derived (constructor)..In all way its giving me error.. Can’t I declare below two...
View ArticleBy: shraa
I just tried below and it worked: class Base { public: int m_id; Base(int i=0):m_id(i){} int getID() const { return m_id;} }; class Derived:public Base {...
View ArticleBy: Alex
The issue is that you’ve defined two default constructors, which causes ambiguity when you try to do something that requires a default constructor. In the latter case, there’s no ambiguity because...
View ArticleBy: Martin
Hi Alex, How would you do list initialization for base class? #include <array> using namespace std; class float3 : public array<float, 3>{}; int main() { float3 mypos = { 1.0f, 2.0f,...
View ArticleBy: AMG
Alex, Could you please tell me if in my version "const" is redundant in class constructors and access functions. 1234567891011121314151617181920212223242526272829 class Fruit{private: std::string...
View ArticleBy: Alex
You’ll have to define your own derived class constructor (in this case, in float3) that takes a std::initializer_list by const reference. If the base class has a constructor that takes a...
View ArticleBy: Alexxx
About that part: Note that it doesn’t matter where in the Derived constructor initialization list the Base constructor is called -- it will always execute first. If i do 12 Derived(double cost=0.0,...
View ArticleBy: Alex
This warning will occur (for compilers that support it) any time you initialize class members in a different order than in which they are defined in the class (or, in this case, with the base class...
View ArticleBy: Ada
how is this? i used a different main function from the one given in the quiz #include<iostream> #include<string> using namespace std; class Fruit { private: string m_name;...
View ArticleBy: Alex
The code seems fine. But I did give you a main() function to use and you didn’t use it. 😐
View ArticleBy: xjuggy
I initially set my classes and member functions up in a similar fashion to yours, Alex, and all was well. Then I decided to experiment with the "never reuse code" mantra: 1 out << "Apple ("...
View ArticleBy: Alex
There’s a better answer. Remove the overloaded operator<< functions for Apple and Banana. You don’t need them. Without them, Fruit::operator<< will be called, and that function already...
View ArticleBy: Ran
My code is less efficient than Alex's solution because I re-write getName() twice in the derived class. A better solution is to put getName() in the base class. I tried to do Alex's style, but I was...
View ArticleBy: nascardriver
Hi Ran! * @m_name is never initialized not used. * @Banana doesn't have a color on it's own, it's using the default value, it works, but it's not nice. * Uniform initialization is preferred. *...
View ArticleBy: Mau
Hello, thanks for the great tutorial and also a question. I got a bit confused by all those constructors being called, but despite that, in the end there is only one object on the call stack, isn't it?
View ArticleBy: nascardriver
Hi Mau! There are no objects on the call stack, there are addresses on the call stack. Assuming you're talking about regular objects, there's only one object, no matter how many constructors where...
View Article