constructor for different objects

I have the following struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct one
		{

			char q[11];
			int a;
			char n[15];

		};			
		one punch={"punch",
				   15,"kill"
				   } ;

		one kick={"kick",
				15,"faint"
                                } ;

		one toss={"toss",
				10,"k.o."
				} ;


I tried to convert it into class but my problem is that I am not able to initialize constructor for some reason

anyways here's what i tried

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
class one
		{

			char q[11];
			int a;
			public:
			char n[15];
			one();//this is constructor i suppose...

		};		
		one::one(){q="punch";
				a=15;
				n="kill";
				}

		one::one(){q="kick";
				a=15;
				n="faint";
				}

		one::one(){q="toss";
				a=10;
				n="k.o.";
								}


I am getting multiple errors... Please help
Last edited on
Hi, you are defining the same method multiple times, and this is not allowed for obvious reasons.
Instead, what I think you are trying to do is having three objects of type 'one'. If this is the case, you have to define only one constructor (maybe passing data as parameters) and call it as many times as you want from a main method.
Cant I do something like 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
class one
		{

			char q[11];
			int a;
			public:
			char n[15];
			one();//this is constructor i suppose...

		};	
one punch,kick,toss;	
		punch::one(){q="punch";
				a=15;
				n="kill";
				}

		kick::one(){q="kick";
				a=15;
				n="faint";
				}

		toss::one(){q="toss";
				a=10;
				n="k.o.";
								}


I know this one is not correct but is something similar possible?
Last edited on
No, I am sorry but I think you should definitely take a look at some programming book. You can find good resources on the web too.
please suggest some resources... I am a noob....
You can find a tutorial here on the site of this forum: http://www.cplusplus.com/doc/tutorial/
Thanks now I found the solution (I am using parenthesized constructors now ) but now I have this new problem.

Why isn't this code working?

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

class A
{
        int a;
        int b;
        public:
        int c;
        int d;
        A()
                {
                a=b=c=d=14;
                }
void change(A one,A two)
        {
        one.a=one.a/2;
        two.a=two.a/2;
        }
} ;
int main()
{
A p,o;

A::change(p,o);//this one here gives errors 
                       //what is wrong with this???
return 0; 
}
Last edited on
bump
I'm not sure why you would want to do it like this. If you just wanted a function to divide a member variable by 2 then.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 class A
{
     //info

     void change()
     {
            a /= 2;
     }
};

int main()
{
     A p,o;

     p.change();
     o.change();
}


If you wanted to do it your way you could make that function static and it would work, but there is no reason for that function to be part of the class anyway. It could just be a free standing function.
If you don't want to call the function on an object obj.func() but instead only mention the type TypeOfObj::func() then the function has to be a static member function.

To make A::change(A, A) into a static function all you need to do is to add static at the beginning of line 13.
Last edited on
oh! thanx...
Topic archived. No new replies allowed.