#include<iostream.h>

class alpha
{
	int x;
	public:
	alpha(int i)
	{
		x=i;
		cout<<"\n alpha constructor";
		
	}
	void show_alpha(void)
	{
	 cout<<"x="<<x<<"\n";
	 
	}
	
};
class beta
{
	float p,q;
	public:
	  beta( float a,float b):p(a),q(b+p)
	  {
  		cout<<"\n beta constructor";
  	}
  	void show_beta(void)
{
	cout<<"p="<<p<<"\n";
	cout<<"q="<<q<<"\n";
}
};

class gamma:public beta,public alpha
{
	int u,v;
	public:
	 gamma(int a,int b,float c):alpha(a*2),u(a)
	 { 
	 beta(c,c);
	 v=b;
	 cout<<"\n gamma constucted";
	 }
	 void show_gamma(void)
	 {
 		cout<<" u ="<<u<<"\n";
 		cout<<"v = "<<v<<"\n";
 	}
};
int main()
{
	gamma g(2,4,2.5);
	cout<<"\n\n display members values"<<"\n\n";
	g.show_alpha();
	g.show_beta();
	g.show_gamma();
	return 0;
}

got strucked up with the error

First, it's not <iostream.h> it's just <iostream> Second, you don't have any namespace declared. You need to somehow activate the std namespace so that you can use cout. I'll leave the method to you, there are at least 3 options available to you.

Third:

gamma(int a,int b,float c):alpha(a*2),u(a)
	 { 
	 beta(c,c);
	 v=b;
	 cout<<"\n gamma constucted";
	 }

This is trying to use the beta-class' default constructor, which you do not have. You need to move the line beta(c,c) to your gamma-class' initialization list.

Fbody,thanks for giving repli.But ,i dint get u ,in the gamma constructor the beta constructor is called explicitly.so,where is the question of calling beta class default constructor

Fbody,thanks for giving repli.But ,i dint get u ,in the gamma constructor the beta constructor is called explicitly.so,where is the question of calling beta class default constructor

It's difficult for me to explain, I'll try to, but perhaps someone else can explain better.

Basically the issue is that the beta-class part of your gamma object is being created before what you consider your call to its constructor. Because it is being created before you call the constructor, the compiler is looking for a "default" constructor to call (a constructor with no arguments). It causes an error because you have only defined an "overloaded" constructor and there is no "default" constructor defined.

Read this page for more information. Specifically, look for the sections "Constructors and Destructors", "Overloading Constructors" and "Default Constructors" in the middle of the page. It doesn't cover initialization lists, but it should give you the basic idea. In general, you should define a "default" constructor, so that you have it, then define your overloaded constructor(s) to do what you want to do.

Edited 6 Years Ago by Fbody: n/a

thank you.got some idea.so , as per ur explanation in C++ one cant create overloaded constructor with out creating default constructor??also when i created a empty default constructor for beta class and called it ,p and q variables are assigned to some garbage values.though i called overloaded constructor explicitly!

It's difficult for me to explain, I'll try to, but perhaps someone else can explain better.

ditto :)

When you say: int x; or alpha my_alpha The computer has to create the space in memory that it needs to
store all of the information about the variable.
So for int x the computer allocates space for an int in memory
and points something to it that you are calling x.

now when you say

int x = 4;

in effect an x is created with an unknown value using x() in it
then this value is set to 4

where as

int x(3);

creates x but this time with 3 set immediately as if there is
x(int val) constructor

When you use the : You can call the constructors of other classes before the computer has finished creating the class
when it reaches the first { all of the memory has to be stored

so in your code it is as if you are saying:

gamma(int a, int b, float c)
:alpha(a * 2), u(a), [B]beta()[/B]
{
//all variables need to be created with defaults by here
gamma(c, c);
//you can then set specific values in here too
}

this implicit beta() constructor does not exist as you did not define it
and beta(float a, float b) cannot be used as the computer has no values for the floats

Edited 6 Years Ago by tetron: meant beta for gamma

class beta
{
	float p,q;
	public:
	beta(){
	}
	
	
	  beta( float a,float b):p(a),q(b+p)
	  {
  		cout<<"\n beta constructor";
  	}

this the code .where i've edited according to your explanation

That is a good fill in for the error you are getting, though you will want to assign values to p and q so that they don't have "garbage" in them. Since you can't call constructors directly, you will still get an error when you try to call beta(c, c) though.

You're better off including the overloaded version of the beta-class constructor in the gamma-class constructor's initialization list.

gamma(int a,int b,float c):alpha(a*2),u(a), beta(c,c) // <-- move to here
{
/*
beta(c, c); <-- move from here
...
*/
}

Edited 6 Years Ago by Fbody: n/a

This question has already been answered. Start a new discussion instead.