I have a question on constructor and overload?

  • MHB
  • Thread starter needOfHelpCMath
  • Start date
In summary,Encapsulation is when you hide the implementation of a class from other classes. Inheritance allows a class to inherit the properties and methods of another class. Polymorphism allows a class to be treated as different types by code that is written to specifically handle those types. Abstraction allows a class to hide how it works from the user.
  • #1
needOfHelpCMath
72
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
  • #2
What happens if you try to create an object of the type Node in the main? Or some class other than the Node class.
 
  • #3
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!
 
  • #4
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
 
  • #5
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?
 
  • #6
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?
 
  • #7
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?
 
  • #8
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.
 

FAQ: I have a question on constructor and overload?

What is a constructor in relation to object-oriented programming?

A constructor is a special method or function that is used to initialize objects in object-oriented programming. It is typically used to set initial values for the object's properties and perform any necessary setup tasks.

Why is a constructor important in object-oriented programming?

Constructors are important because they allow for the creation and initialization of objects in an efficient and organized manner. They help ensure that all necessary variables and properties are set correctly before the object is used.

What is constructor overloading and how does it differ from method overloading?

Constructor overloading is the concept of having multiple constructors within a class, each with a different set of parameters. This allows for more flexibility in creating objects with different initial values. It differs from method overloading in that method overloading involves having multiple methods with the same name but different parameters, while constructor overloading involves having multiple constructors with different parameters.

What are some common uses of constructor overloading?

Constructor overloading is commonly used when creating objects with different initial values, such as creating a rectangle object with different length and width values. It can also be used for creating objects with optional parameters, or for creating objects with default values for certain parameters.

What are the benefits of using constructor overloading in object-oriented programming?

Some benefits of using constructor overloading include: increased flexibility in creating objects with different initial values, improved readability and organization of code, and the ability to have default values for certain parameters. It can also help reduce the amount of code needed for creating objects, as multiple constructors can handle different scenarios.

Similar threads

Replies
1
Views
1K
Replies
25
Views
2K
Replies
52
Views
3K
Replies
2
Views
1K
Replies
36
Views
3K
Replies
3
Views
3K
Replies
89
Views
5K
Back
Top