Question on constructors in Inheritance

  • Thread starter Thread starter yungman
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around understanding constructors in inheritance, specifically how member variables and functions are inherited and instantiated in derived classes. Participants explore the implications of naming conflicts and the behavior of constructors in both base and derived classes, with examples provided to illustrate their points.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks confirmation that an object of a derived class has its own member variables and functions, specifically questioning the ownership of variables when names conflict between base and derived classes.
  • Another participant supports the idea that derived class objects contain their complete set of member variables, citing memory address differences to demonstrate that each class maintains its own state.
  • Concerns are raised about the implications of having member variables with the same name in both the base and derived classes, particularly in constructor definitions.
  • A participant discusses the behavior of virtual functions in the context of member functions with the same name across base and derived classes.
  • Examples are provided that illustrate the complexities of constructor initialization and variable scope, particularly when using parameters with the same name as member variables.

Areas of Agreement / Disagreement

Participants generally agree that derived class objects have their own member variables and functions. However, there is ongoing uncertainty regarding the implications of naming conflicts and how constructors handle these situations, indicating that the discussion remains unresolved.

Contextual Notes

Participants express confusion over the handling of member variables with the same name in constructors, which may lead to ambiguity in understanding which variable is being referenced. The discussion highlights the need for clarity in constructor initialization and variable scope.

  • #31
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.
 
Technology news on Phys.org
  • #32
Thank you, it works. Never thought of this! That the line needs to be called, too used to just count in main().

Thanks
 
  • #33
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
  • #34
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.
 
  • #35
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
  • #36
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
 
  • #37
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

Similar threads

  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
89
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
3K