Problem in Constructors

Posted by gurramsra1-10894 under C# on 2/5/2012 | Points: 10 | Views : 1833 | Status : [Member] | Replies : 7
Why the following code shows error like...
Error 1 'ConsoleApplication1.A' does not contain a constructor that takes '0' arguments.
Please help me.... the code is.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class A
{
int a, b;

public A(int p, int q)
{
a = p;
b = q;
Console.WriteLine( a + b);

}
public int add()
{

return a + b;

}

}
public class B:A
{
int c, d;

public B()
{
c = 2;
d = 1;


}
public B(int x, int y)
{
c = x;
d = y;
}
public int sub()
{
return c - d;
}
}
class Program
{
static void Main(string[] args)
{

B b = new B(15,10);

Console.WriteLine(b.add());
Console.WriteLine(b.sub());
Console.Read();
}
}
}

Sra1


Responses

Posted by: Anand on: 2/5/2012 [Member] Starter | Points: 25

Up
0
Down
same error i got
does anyone knows the solution for this :)

-

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/5/2012 [Member] Starter | Points: 25

Up
0
Down
just see whether this is right or not
using System;
public class A
{
int a, b;
public A(int p, int q)
{
a = p;
b = q;
Console.WriteLine(a + b);
}
public int add()
{
return a + b;
}
}
public class B : A
{
int c, d;
//public B()
//{
// c = 2;
// d = 1;
//}
public B(int x, int y):base( x, y)

{
c = x;
d = y;
}
public int sub()
{
return c - d;
}
}
class Program
{
static void Main(string[] args)
{

B b = new B(15, 10);

Console.WriteLine(b.add());
Console.WriteLine(b.sub());
Console.Read();
}
}


-

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Avdesh on: 2/5/2012 [Member] Starter | Points: 25

Up
0
Down
make a default constructor of class A the program will work fine
thanks Regards
AVDESH

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 2/6/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Either make a default constructor of class A or you have to intialize class A construstor whereever inheritance taking place.
Exp:

public class B : A

{
int c, d;

public B() :base(2,1)
{
c = 2;
d = 1;


}
public B(int x, int y) :base(x,y)
{
c = x;
d = y;
}
public int sub()
{
return c - d;
}
}


Regards,
Sunil

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/6/2012 [Member] Starter | Points: 25

Up
0
Down
i am not able to understnd

-

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Avdesh on: 2/6/2012 [Member] Starter | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
public class A
{
int a, b;
public A////THIS IS DEFAULT CONSTRUCTOR OF CLASS A. I ADD THIS TO YOUR CODE ONLY
{
}

public A(int p, int q)
{
a = p;
b = q;
Console.WriteLine( a + b);

}
public int add()
{

return a + b;

}

}
public class B:A
{
int c, d;

public B()
{
c = 2;
d = 1;


}
public B(int x, int y)
{
c = x;
d = y;
}
public int sub()
{
return c - d;
}
}
class Program
{
static void Main(string[] args)
{

B b = new B(15,10);

Console.WriteLine(b.add());
Console.WriteLine(b.sub());
Console.Read();
}
}
}


run this code this code will work fine.


THANKS REGARDS
AVDESH

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anand on: 2/6/2012 [Member] Starter | Points: 25

Up
0
Down
thank u that worked out


-

gurramsra1-10894, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response