MHB I have a question on constructor and overload?

  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
AI Thread Summary
The discussion centers on the implications of declaring constructors as private within a class in C++. It highlights that making a constructor private restricts object creation to within the class itself, which can lead to issues when trying to instantiate the class from outside, such as in a main function. Participants express curiosity about the functionality of private constructors and the consequences of such design choices. The conversation also touches on the importance of understanding object-oriented programming principles, specifically encapsulation, inheritance, polymorphism, and abstraction, in relation to constructors and class behavior. One participant notes their lack of familiarity with these principles, indicating a learning opportunity in the context of data structures.
needOfHelpCMath
Messages
70
Reaction score
0
Can overload and constructor be in private? for example my overload constructor:
HTML:
class Node {
      
      private:
             int  Nodedata;
             Node *next;
             Node() {next =  NULL; Nodedata = 0;}
             
             Node(int data = 0, Node *nextNode) { //overloaded constructor
             Nodedata = data;
             next = nextNode;
           }
		   };
     public:
           void insertToFront();
           void insertBack();
           void insertMiddle();
 
Technology news on Phys.org
What happens if you try to create an object of the type Node in the main? Or some class other than the Node class.
 
Setting a function, in an object, "private" means that it can only be seen or used by that object. Setting a constructor "private" means that object can only be constructed by the object itself- which hasn't been constructed yet!
 
Joppy said:
What happens if you try to create an object of the type Node in the main? Or some class other than the Node class.

doesn't work ahaha XD but i was just curious if it works
 
needOfHelpCMath said:
doesn't work ahaha XD but i was just curious if it works

Good that you tried! Now read HallsofIvy's post for why.

Never forget the four key principles of object orientated programming!

Quiz: Which key principle does your question mostly relate to?
 
Joppy said:
Good that you tried! Now read HallsofIvy's post for why.

Never forget the four key principles of object orientated programming!

Quiz: Which key principle does your question mostly relate to?

hmmm...i think the principle is to understand constructors and class and how they work together?
 
needOfHelpCMath said:
hmmm...i think the principle is to understand constructors and class and how they work together?

That's not quite what i was getting at :p :D. But good point nonetheless.

Have you learned about the four principles of OO;

Encapsulation, inheritance, polymorphism, and abstraction?
 
Joppy said:
That's not quite what i was getting at :p :D. But good point nonetheless.

Have you learned about the four principles of OO;

Encapsulation, inheritance, polymorphism, and abstraction?

nope never heard of them and i am taking a class on data structures.
 

Similar threads

Back
Top