Question on constructors in Inheritance

  • Thread starter Thread starter yungman
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
36 replies · 3K views
Vanadium 50 said:
But it's not in a function.

Think about it this way - when do you expect it to run?
Ah! I have to try to execute the line, the line is just sitting there!

I'll be back. Thanks so much.
 
Physics news on Phys.org
Thank you, it works. Never thought of this! That the line needs to be called, too used to just count in main().

Thanks
 
yungman said:
I have a strange error I cannot explain.
C++:
#include<iostream>
using std::count;
class Bclass  //members = a, b, fun0 and fun1
   {public: int a = 2, b = 3;
    void fun0() { count << " Bclass fun0()." << "\n\n";}
    void fun1() { count << " Bclass fun1()." << "\n\n";}};
class Dclass : public Bclass//members = a, fun1 and fun2
{public: int a = 5;//This overrides a in Bclass.
    count << " Dclass.a = " << a << ", Bclass.a = " << Bclass.a << "\n\n";
    void fun1() { Bclass::fun1(); }//use fun1 in Bclass
    void fun2() { count << " Dclass fun2()." << "\n\n";} };
int main()
   {Dclass DC2; DC2.fun0(); DC2.fun1(); DC2.fun2();
    count << " DC2.a= " << DC2.a << ", DC2.b= " << DC2.b << "\n\n";
}//a in Dclass overrides a in Bclass, DC2.a=5. b inherits from Bclass.
If you didn't cram all your code so tightly, it might have been more obvious that you have an executable statement (the output line) in amongst the declarations of the Dclass members. It's a false economy to jam everything together like you often seem to do.
 
  • Like
Likes   Reactions: Vanadium 50
Mark44 said:
If you didn't cram all your code so tightly, it might have been more obvious that you have an executable statement (the output line) in amongst the declarations of the Dclass members. It's a false economy to jam everything together like you often seem to do.
It's one line of code per line. It's not tight without the comments.
 
yungman said:
It's one line of code per line.

That is tautologically true. It also misses the point: most people would write
Code:
void fun1() { cout << " Bclass fun1()." << "\n\n";}};
as either 4 or 5 lines.

Mark told you many times that this is good practice.
You have not done this - even after running into problems doing it "your way". Repeatedly.
Why is that? Do you think you're smarter than Mark? Do you think you're a better programmer? Is it because you know that if you get into trouble we will bail you out again and again and again?
 
  • Like
Likes   Reactions: Mark44
jbunniii said:
Your attempt to call count is not inside any function definition. This is not allowed. Did you mean to do it inside the Dclass constructor? If so, you have to define a Dclass constructor!
Sorry I did not see your post till now.

Thanks
 
Here's the same code you posted, minus the comments. It's still tight
C++:
#include<iostream>
using std::count;
class Bclass
   {public: int a = 2, b = 3;
    void fun0() { count << " Bclass fun0()." << "\n\n";}
    void fun1() { count << " Bclass fun1()." << "\n\n";}};
class Dclass : public Bclass
{public: int a = 5;
    count << " Dclass.a = " << a << ", Bclass.a = " << Bclass.a << "\n\n";
    void fun1() { Bclass::fun1(); }
    void fun2() { count << " Dclass fun2()." << "\n\n";} };
int main()
   {Dclass DC2; DC2.fun0(); DC2.fun1(); DC2.fun2();
    count << " DC2.a= " << DC2.a << ", DC2.b= " << DC2.b << "\n\n";
}
When you wrote "It's one line of code per line." you probably meant one instruction per line, but in main() you have a declaration and three function calls all on one line.

Here's the same code as above, but spaced out to increase readability.
C++:
#include<iostream>
using std::count;

class Bclass
{
public:
    int a = 2, b = 3;
    void fun0() { count << " Bclass fun0()." << "\n\n";}
    void fun1() { count << " Bclass fun1()." << "\n\n";}
};

class Dclass : public Bclass
{
public:
    int a = 5;
    count << " Dclass.a = " << a << ", Bclass.a = " << Bclass.a << "\n\n";
    void fun1() { Bclass::fun1(); }
    void fun2() { count << " Dclass fun2()." << "\n\n";}
};

int main()
{
    Dclass DC2;
    DC2.fun0();
    DC2.fun1();
    DC2.fun2();
    count << " DC2.a= " << DC2.a << ", DC2.b= " << DC2.b << "\n\n";
}
With a reasonable amount of whitespace, it's much more obvious that in the declaration section of Dclass, you have an executable statement (count << ...) mixed in with the declarations.
 
Last edited:
  • Like
Likes   Reactions: jbunniii and Vanadium 50