Question on constructors in Inheritance

  • Thread starter Thread starter yungman
  • Start date Start date
Click For Summary
The discussion centers on understanding constructors in inheritance, specifically regarding member variables and functions in derived classes. It confirms that objects of derived classes possess their own member variables and can access inherited member functions from base classes, but do not have separate copies of inherited functions. The confusion arises with naming conventions in constructors, particularly when parameters share names with member variables from base and derived classes. It clarifies that while derived classes can access base class members, they do not create duplicates of those members. Overall, the conversation emphasizes the importance of understanding how inheritance and constructors interact in C++.
  • #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 cout in main().

Thanks
 
  • #33
yungman said:
I have a strange error I cannot explain.
C++:
#include<iostream>
using std::cout;
class Bclass  //members = a, b, fun0 and fun1
   {public: int a = 2, b = 3;
    void fun0() { cout << " Bclass fun0()." << "\n\n";}
    void fun1() { cout << " Bclass fun1()." << "\n\n";}};
class Dclass : public Bclass//members = a, fun1 and fun2
{public: int a = 5;//This overrides a in Bclass.
    cout << " Dclass.a = " << a << ", Bclass.a = " << Bclass.a << "\n\n";
    void fun1() { Bclass::fun1(); }//use fun1 in Bclass
    void fun2() { cout << " Dclass fun2()." << "\n\n";} };
int main()
   {Dclass DC2; DC2.fun0(); DC2.fun1(); DC2.fun2();
    cout << " 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 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 Mark44
  • #36
jbunniii said:
Your attempt to call cout 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::cout;
class Bclass
   {public: int a = 2, b = 3;
    void fun0() { cout << " Bclass fun0()." << "\n\n";}
    void fun1() { cout << " Bclass fun1()." << "\n\n";}};
class Dclass : public Bclass
{public: int a = 5;
    cout << " Dclass.a = " << a << ", Bclass.a = " << Bclass.a << "\n\n";
    void fun1() { Bclass::fun1(); }
    void fun2() { cout << " Dclass fun2()." << "\n\n";} };
int main()
   {Dclass DC2; DC2.fun0(); DC2.fun1(); DC2.fun2();
    cout << " 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::cout;

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

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

int main()
{
    Dclass DC2;
    DC2.fun0();
    DC2.fun1();
    DC2.fun2();
    cout << " 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 (cout << ...) mixed in with the declarations.
 
Last edited:
  • Like
Likes 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
5K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
3K