class inheritance problem

i can't seem to get the inheritance right can someone help for void attack


#pragma once
#include<iostream>
#include<stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

class characters
{

string name;
float health;
float strength;
float speed;
float criticalshot;

public:

characters(string n,float h, float st, float sp, float cs)
{
name = n;
health = h;
strength = st;
speed = sp;
criticalshot = cs;

}



#pragma once
#include "hdr.h"
#include<iostream>
#include<stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "fighter2.h"

class fighter1:public characters
{
public:


fighter1(string n,float h,float st,float sp, float cs): characters(n,h,st,sp,cs)
{

}
float attack(fighter2 target)
{
cout<<name<<"'s attack did "<<fighter1::strength<<" damage to "<<target.name<<endl;
cout<<target.name<<" health is now "<<(target.health -= fighter1::strength)<<endl;
return target.health;
}




#include<stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "fighter1.h"


class fighter2:public characters
{
string name;
float health;
float strength;
float speed;
float criticalshot;
public:
fighter2(string n,float h,float st, float sp, float cs):characters(n,h,st,sp,cs)
{

}
Last edited on
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#pragma once
#include<iostream>
#include<stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

class characters
{

string name;
float health;
float strength;
float speed;
float criticalshot;

public:

characters(string n,float h, float st, float sp, float cs)
{
name = n;
health = h;
strength = st;
speed = sp;
criticalshot = cs;

}



#pragma once
#include "hdr.h"
#include<iostream>
#include<stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "fighter2.h"

class fighter1:public characters
{
public:


fighter1(string n,float h,float st,float sp, float cs): characters(n,h,st,sp,cs)
{

}
float attack(fighter2 target)
{
cout<<name<<"'s attack did "<<fighter1::strength<<" damage to "<<target.name<<endl;
cout<<target.name<<" health is now "<<(target.health -= fighter1::strength)<<endl;
return target.health;
}




#include<stdio.h>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "fighter1.h"


class fighter2:public characters 
{
string name;
float health;
float strength;
float speed;
float criticalshot;
public:
fighter2(string n,float h,float st, float sp, float cs):characters(n,h,st,sp,cs)
{

}
you're using inheritance in that you are deriving from the class "characters"

you say you want help with your attack method. can you explain what you want help with>?
i am not sure what is different
attack the can't access the private data members in characters
make them protected instead of private

before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class characters
{

string name;
float health;
float strength;
float speed;
float criticalshot;

public:

characters(string n,float h, float st, float sp, float cs)
{
name = n;
health = h;
strength = st;
speed = sp;
criticalshot = cs;

}


after
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class characters
{

protected:

string name;
float health;
float strength;
float speed;
float criticalshot;

public:

characters(string n,float h, float st, float sp, float cs)
{
name = n;
health = h;
strength = st;
speed = sp;
criticalshot = cs;

}
by default class members are private so if you had:
1
2
3
4
5
6
class foo
{

    int bar;

}


bar would be a private member of foo
same error
What error's that?
As well as making members public, you also need to add the closing curly-braces to end each class definition.

Add }; after each class. Indenting your code correctly would help you identify errors such as these.

Cheers,
Jim
error 2248
Hahahaha ROTFL
.....don't get whats so funny, i just want help with this
Sorry, I've stopped laughing now. Saying "error 2248" is like going to the doctor's and saying "I think it hurts somewhere". In fact it's worse than that - you haven't even said which compiler you're using, so I can't even guess what error 2248 is ;-)

Have you added the }; after each class definition?

Can you copy and paste the compilation errors you're getting - the whole errors, with line numbers and everything.

Someone else may have to help you on this one though - our office has just had carpets laid and the glue stink is too bad - I'm off home!

Cheers,
Jim
Are fighter1 and fighter2 really supposed to be two unrelated classes both derived from characters? (you're not confusing the difference between instances of classes, with the classes themselves?)

If so, then fighter1::attack() really does not have access to the privates (or protected) members of fighter2 (as the compiler error is telling you :). You will need to either make fighter1 inherit from fighter2 (which would then be able to access protected members), or make fighter2 provide a bunch of public get-routines to allow fighter1 to access the data (eg "string getName() const { return name; };" etc). OR redesign your class hierarchy.
Last edited on
Topic archived. No new replies allowed.