I have a question on constructor and overload?

  • Context: MHB 
  • Thread starter Thread starter needOfHelpCMath
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on the implications of declaring constructors as private within a C++ class, specifically the Node class. The private constructor prevents instantiation of Node objects outside the class, which leads to confusion regarding object creation in the main function. Key principles of object-oriented programming (OOP) such as encapsulation, inheritance, polymorphism, and abstraction are highlighted as foundational concepts that inform the understanding of constructors and class interactions.

PREREQUISITES
  • Understanding of C++ class structure and syntax
  • Familiarity with object-oriented programming principles
  • Knowledge of constructors and their access specifiers in C++
  • Basic understanding of data structures
NEXT STEPS
  • Study the concept of private constructors in C++ and their use cases
  • Learn about the four principles of object-oriented programming: encapsulation, inheritance, polymorphism, and abstraction
  • Explore C++ class member access specifiers and their implications
  • Investigate the role of constructors in managing object lifecycle in C++
USEFUL FOR

Students learning C++ programming, software developers interested in object-oriented design, and anyone seeking to deepen their understanding of class construction and encapsulation in C++.

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

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 52 ·
2
Replies
52
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
89
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K