constructors

I am trying to make programs with constructors . Can anyone please tell me what is wrong with this program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
class wasim
{
int x,y;
public:
	int addition(int a,int b,int c);
	wasim(x,y);
};
wasim::waism(x,y)
{
	int z;
	z=x+y;
	return z;
}
int wasim::addition(int a,int b,int b)
{
	cout<<a<<b<<c;
}
int main()
{
	wasim masih;
	wasim tukhi;
	masih.addition(1,2,3);
	tukhi.wasim(1,2);
	cout<<tukhi.wasim(1,2)<<masih.addition(1,2,3);
	return 0;
}
Constructors are only called when the object is created. You don't need to repeat the class type, just put the parameters in parenthesis like:

wasim tukhi(1, 2);

Constructors do not return a value. Your definition is fine, but your implementation attempts to return an int.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;
class wasim
{
int x,y;
public:
	int addition(int a,int b,int c);
	wasim(x,y);
};
wasim::waism(x,y)
{
	cout<<x<<y;
}
int wasim::addition(int a,int b,int b)
{
	int z;
	return (a+b+c);

}
int main()
{
	wasim masih;
	wasim tukhi;
	masih.addition(1,2,3);
	tukhi.wasim(1,2);
	cout<<tukhi.wasim(1,2)<<masih.addition(1,2,3);
	return 0;
}



In this program , the constructor has no return type and the other function has a return value of int type but still its not working.
Constructors can only be called when your object is created so remove line 25, and edit out the call on line 26.
Also both masih and tukhi need paramaters (on line 22 and 23).
Do you mean this .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;
class wasim
{
int x,y;
public:
	int addition(int a,int b,int c);
	wasim(x,y);
};
wasim::waism(x,y)
{
	cout<<x<<y;
}
int wasim::addition(int a,int b,int b)
{
	int z;
	return (a+b+c);

}
int main()
{
	wasim masih(1,2,3);
	wasim tukhi(1,2);
	
	cout<<tukhi.wasim(1,2)<<masih.addition(1,2,3);
	return 0;
}
Do you mean that when we declare a constructor we declare the memeber functions as

1
2
wasim addition (3,5)
wasim subtraction (7,6)

and when we dont declare a constructor we declare the the member functions as

1
2
wasim addition;
wasim subtraction;


Sir, I have read everything here.I am still confused , I will be thankfull if you can explain this to me.
Last edited on
Would this not work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
class wasim
{
int x,y;
public:
	int addition(int a,int b,int c); //this declares a member function
	wasim(x,y);  //this declares the constructor.
};
wasim::waism(x,y)
{
	cout<<x<<y;   //this statement executes when you create a wasim.
}
int wasim::addition(int a,int b,int b)
{
	int z;
	return (a+b+c);

}
int main()
{
	wasim masih(1,2,3);  //this doesn't work, your constructor takes only two params
	wasim tukhi(1,2);       //this works and makes a wasim on the stack.

	
	//cout<<tukhi.wasim(1,2)<<masih.addition(1,2,3);  //this line is just weird.
          cout<<tukhi.x<<tukhi.y<<masih.addition(1,2,3);   //this might be what you want.
	return 0;
}


I don't think the statement tukhi.wasim(1,2) makes sense because what it is saying is call the constructor for this object. You already called the constructor when you declared tukhi.

I ran your original code and my computer exploded. j/k

I really hope I didn't mislead, somebody please correct me if I'm wrong, please. Also my terminology is probably wrong

--J
Last edited on
Sir, this program will not work because you used x and y outside the class which is by default private.
Last edited on
Consider class A:
1
2
3
4
5
6
7
8
class A
{
    int _a;
public:
    A() {} // default constructor
    A( int a ):  _a( a ) {}  // constructor
    A( A & a ) { _a = a._a; } // default copy constructor
};

Note that the default constructors will be added by the compiler, if you do not explicitly add them yourself.

Here is how you instantiate objects of class A:
1
2
3
4
5
A a; // construct using default constructor
A b( 10 ); // construct using user-defined constructor
A c( b ); // construct using default copy constructor

// use the "initialized" objects here 
Last edited on
Sir, this program will not work because you used x and y outside the class which is by default private.


Oh yeah, totally overlooked that. Apologies and thanks.
Last edited on
how can we make use of default constructor ?
As seymore15074 said
Note that the default constructors will be added by the compiler, if you do not explicitly add them yourself.
Default constructors are added for default (if not already declared in the class) so you don't need to do anything to use them
eg:
1
2
3
4
5
6
7
class Class
{
    //Notice: no constructor with no arguments nor copy constructor declared
};

Class a; // Called constructor with no arguments given by the compiler
Class b = a; //Called copy constructor given by the compiler 
Note however that the moment you create your own custom (non-default) constructor, the compiler no longer gives you a default one.
1
2
3
4
5
6
7
class Class
{
    //Notice: no constructor with no arguments nor copy constructor declared
};

Class a; // Called constructor with no arguments given by the compiler
Class b = a; //Called copy constructor given by the compiler  


Sir, In this program you said that
class a ;
is a default constructor
but this is exactly the same as object of the class
when we deaclare objects we do the same thing.
so how can we differentiate between default constructor and object of class.
When an object of a class is declared it calls the constructor
so Class a; calls Class() ( Constructor given by the compiler ) to build a (object of Class).
That is not the default constructor, it calls the default constructor
Last edited on
Topic archived. No new replies allowed.