I have a question on constructor and overload?

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

Discussion Overview

The discussion revolves around the use of private constructors and method overloading in object-oriented programming, specifically within the context of a class definition in C++. Participants explore the implications of making constructors private and the resulting accessibility issues when attempting to create instances of the class.

Discussion Character

  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions whether constructors and overloads can be private, providing an example of a class with a private constructor.
  • Another participant asks about the consequences of trying to create an object of the Node class in the main function or from another class.
  • A participant explains that making a constructor private restricts object creation to within the class itself, which raises questions about how an object can be constructed if it hasn't been created yet.
  • Some participants express curiosity about the functionality of private constructors, noting that attempts to create an object do not work.
  • There is a mention of the four key principles of object-oriented programming, with participants reflecting on their understanding of these principles in relation to the discussion.
  • One participant admits to not being familiar with the four principles of object-oriented programming despite taking a class on data structures.

Areas of Agreement / Disagreement

Participants express curiosity and confusion regarding the implications of private constructors, but there is no consensus on the best approach or understanding of the principles involved. The discussion remains unresolved regarding the practical applications and limitations of private constructors.

Contextual Notes

Some participants reference the four principles of object-oriented programming without fully defining them, leading to potential gaps in understanding. The discussion also reflects uncertainty about the accessibility and instantiation of objects with private constructors.

Who May Find This Useful

This discussion may be useful for students learning about object-oriented programming concepts, particularly those exploring class design and constructor accessibility 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