Question:
what is constructor and explain constructor overloading with program?
1970-01-01 00:00:00 UTC
what is constructor and explain constructor overloading with program?
Seven answers:
Priyanka
2011-08-03 03:39:56 UTC
Constructors

A constructor is a function that initilizes the members of an object. A constructor only knows how to build an object of its own class.

Constructors aren't automatically inherited between base and derived classes. If you don't supply one in the derived class, a default will be provided but this may not do what you want.



If no constructor is supplied then a default one is created by the compiler without any parameters. There must always be a constructor, even if it is the default and empty. If you supply a constructor with parameters then a default will NOT be created.



Some points about constructors



•Constructors are just functions with the same name as the class.

•Constructors are intended to initialize the members of the class when an instance of that class is created.

•Constructors are not called directly (except through initializer lists)

•Constructors are never virtual.

•Multiple constructors for the same class can be defined. They must have different parameters to distinguish them.





Overload the constructor





#include

using namespace std;



class MyClass {

public:

int x;

int y;



// Overload the default constructor.

MyClass() { x = y = 0; }



// Constructor with one parameter.

MyClass(int i) { x = y = i; }



// Constructor with two parameters.

MyClass(int i, int j) { x = i; y = j; }

};



int main() {

MyClass t; // invoke default constructor

MyClass t1(5); // use MyClass(int)

MyClass t2(9, 10); // use MyClass(int, int)



cout << "t.x: " << t.x << ", t.y: " << t.y << "\n";

cout << "t1.x: " << t1.x << ", t1.y: " << t1.y << "\n";

cout << "t2.x: " << t2.x << ", t2.y: " << t2.y << "\n";



return 0;

}



t.x: 0, t.y: 0

t1.x: 5, t1.y: 5

t2.x: 9, t2.y: 10
gurucharan
2016-11-16 10:14:49 UTC
Constructor Overloading
2016-10-02 13:55:59 UTC
(a) somebody already gave you a superbly solid answer the final time you published this comparable homework exercising - attempt interpreting your solutions earlier re-posting the comparable question (b) you forgot to ask a particular question (back)
2011-08-03 05:47:21 UTC
A constructor has got same name as class name.

it does not return any value.

constructors are called automatically when objects are created.



thr are two types of construtors:default constructor and parameterized construcutors.

constructors having the same name but differnt parametes is called constructor overloading.
Jayaprakash
2011-08-03 03:59:59 UTC
constructor - a run time method, which is used to initialize default values to the variables defined in a method. constructor name is same as method name.



constructor overloading - using the same constructor for different methods to initialize default values to the variables defined in a method, but differs based on there parameter.



hope Priyanka example program is enough to understand this concept.
?
2011-08-03 03:59:24 UTC
A constructor is a special type of member function which get activated when the instance of a class

is created.A constructor name and class are same.A constructor doesn't return any value not even

void but may receive any types of argument there for the constructor can be overloaded.

(overloading means , a class share a function having same name but different arguments.)

Constructor overloading program.

class Demo

{

public Demo() // default constructor

{

cout<<"Default constructor call";

}

public Demo(int a) // constructor with one parameter

{

cout<<"Constructor with one parameter:"<
}

public Demo(int a,int b) // constructor with two parameter

{

cout<<"Constructor with two parameter:"<
}

}

void main()

{

Demo d1; // here default constructor will invoke(call automatically)

Demo d2(10);//here one parameter constructor will invoke(call automatically)

Demo d3(5,10)// here two parameter constructor will invoke(call automatically)

}
AnalProgrammer
2011-08-03 03:37:10 UTC
A class is made up of State - variables and Behaviour - methods.

A constructor is a special method which has the same name as the class and is called when an object is created from a class.

The role of the constructor is to give the variables an initial setting.



The constructor can have zero or more parameters. It is the type and number of parameters that makes a constructor unique. So you can have different constructors with a different number of and type of parameter.



Have fun.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...