Class Inheritance Problems

Guys I think I am getting confused now.
Say I have the following classes and want to change something




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
  class Bass
{
public:


virtual int getPrice()
{
return price;
}

protected:
int price;
}


class BaseA : Base
{
public:
BaseA(int t) : Base(t);
int getPrice()
{
Base::getPrice();
}

}

class BaseB : BaseA
{
BaseB() : BaseA(9) // here price is set to 9, will objective is to change the price of Base

int getPrice()
{
BaseA::getPrice();
}
}
class BaseC : BaseB
{
BaseC(BaseB * _b)
{
b = _b;
}
int getPrice()
{
return b->getPrice()+ 3;  // objective is to update and not change the price wi
}
private:
BaseB* b;
}

}





Assuming everything else is declared correctly.
Thank you
Say I have the following classes and want to change something

I do not understand what you are trying to achieve, or what the problem is.
I am trying to create a Decorator design pattern.It should update the valus.It should go from the bottom class up.

Thanks
Can you give us an example that compiles first?

What do you mean by this?
It should go from the bottom class up.

I thought a decorator added additional "features" to an object dynamically? I could be wrong though.
Yes it does.
Okay here is the the scenario. I did an abstract Factory, Now i need to combine it with a decorator.

Here are my partial classes.

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


class Unit
{


protected:
	int attack_damage;
  

public:

	Unit();
	Unit(int , int , int, string);
	virtual ~Unit();
	
	virtual int getAttack();

};


class Soldier : public Unit
{
	public:
		Soldier(){}
		Soldier(int, int, int, string); 
		virtual ~Soldier(); 
		int getAttack()
			{
				return Unit::getAttack(); 
			}
		
};


class HulkSoldier : public Soldier
{
	public:
		
HulkSoldier(string name) : Soldier(3, 7, 5, name){}
~HulkSoldier(){}
			
	//If a HulkSoldier is attacked, its enemy loses one health point.
	
int getAttack()
{
		
	return Soldier::getAttack(); 
}
};



class RagingHulk : HulkSoldier
{

	public:
		RagingHulk(){}
		RagingHulk(HulkSoldier * _soldier);
		RagingHulk(string);
		int getAttack()
			{
				return soldier->getAttack() + 3;
			}
	
		private:
		HulkSoldier * soldier;
};






I need to update the attack
Thank you
Last edited on
Why does the "decorator" (aka "adapter") inherit from the most derived class?


Lets say you have a type Window and your code creates and uses a window. Fine.

Then you invent a shiny window. Simple: inherit ShinyWindow from Window, specialize the shininess, and create a ShinyWindow instead of Window. Fine, the code still uses the object as a window like before. You can keep adding features via inheritance, but you have to replace the object creation code every time.

What if creation is inmutable? Lets create an another object that looks like a window (with a twist). Whatever you do that that object is actually done to the real window object (which might be shiny), but our adapter twists some operations. Rather than replacing an object, we have added a second object that "decorates" the first (assuming that we use the decorator as the window instead of the original object).

One can still upgrade the original object via normal inheritance, even though it is used by (possibly many) decorator objects.
But What am I doing wrong.Is there any thing that I am doing wrong
"decorator" (aka "adapter")

i don't believe these patterns are the same.

but yes, normal inheritance should do the trick. OP, i think you might be over-complicating things.
ow.I got it.Apparently I was suppose to make a new decorator class. Thanx alot though.Ow, and Decorator is similar to Composite and not Adapter I think.
But thanks though.
Last edited on
> Decorator is similar to Composite and not Adapter I think.

Decorator is similar to the Proxy pattern; in both, the wrapper exposes the same interface as that of the wrapped object.

The interface of the decorated object is identical to the object that it decorates; The interface of the proxy object is identical to the object that it proxies for.

Adapter, Bridge and Facade are similar; in all three the interface that is exposed is different from the interface of the object that is adapted.

Eliding memory management for brevity:

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
#include <iostream>
#include <typeinfo>

struct unit
{
    virtual ~unit() {}
    // ...

    virtual int getAttack() const { return 5 ; }

    // ...
};

struct soldier : unit
{
    // ...
};

struct hulk_soldier : soldier
{
    // ...

    virtual int getAttack() const override
    { return soldier::getAttack() + 1 ; }
};

struct raging_hulk : hulk_soldier
{
    // ...

    virtual int getAttack() const override
    { return hulk_soldier::getAttack() + 1 ; }
};

struct attack_enhanser : unit // decorator
{
    attack_enhanser( unit* enhance_this ) : decorated_object(enhance_this) {}

    virtual int getAttack() const override
    { return decorated_object->getAttack() * 3 ; }

    private: unit* decorated_object ;
};

struct tracing_proxy : unit // proxy
{
    tracing_proxy( unit* trace_this ) : traced_object(trace_this) {}

    virtual int getAttack() const override
    {
        auto type = typeid(*traced_object).name() ;
        std::cout << "getAttack() called on object of type " << type << '\n' ;
        auto result = traced_object->getAttack() ;
        std::cout << "object of type " << type << " returned: " << result << '\n' ;
        return result ;
    }

    private: unit* traced_object ;
};

int main()
{
    unit* p = new raging_hulk() ;
    int value = p->getAttack() ;
    std::cout << "p->getAttack() => " << value << "\n\n" ;

    p = new tracing_proxy(p) ;
    value = p->getAttack() ;
    std::cout << "p->getAttack() => " << value << "\n\n" ;

    p = new attack_enhanser(p) ;
    value = p->getAttack() ;
    std::cout << "p->getAttack() => " << value << "\n\n" ;

    p = new tracing_proxy(p) ;
    value = p->getAttack() ;
    std::cout << "p->getAttack() => " << value << "\n\n" ;
}

http://coliru.stacked-crooked.com/a/8070c191c0d0eecb
Eliding memory management for brevity


'Eliding'? i've learnt a new word today \o/
Topic archived. No new replies allowed.