Question on constructors in Inheritance

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

This discussion focuses on the intricacies of constructors in inheritance, specifically in C++. The user seeks clarification on whether derived class objects possess their own member variables and functions, as well as how to handle naming conflicts in constructors. It is confirmed that derived class objects do have their own member variables, but they also inherit member variables and functions from base classes. The confusion arises when constructor parameters share names with member variables, leading to ambiguity in which variable is being referenced.

PREREQUISITES
  • Understanding of C++ inheritance and constructors
  • Familiarity with member variables and functions in classes
  • Knowledge of scope resolution in C++
  • Basic debugging skills in C++ to inspect object memory
NEXT STEPS
  • Study C++ constructor initialization lists and their implications
  • Learn about scope resolution and how to differentiate between member variables and parameters
  • Explore the use of virtual functions in inheritance for polymorphism
  • Practice debugging C++ programs to observe object memory and variable states
USEFUL FOR

C++ developers, software engineers, and students learning object-oriented programming concepts, particularly those focusing on inheritance and constructor behavior.

  • #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
2K
  • · 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