Sign in to follow this  
Durfy

Quick question about classes.

Recommended Posts

If I have class A and class B. In class B i have an object of type A does the object know about the variables that are in the rest of B? example: class A{ protected: int power; public: void printvars(){ print power,magic; } }; class B{ private: int magic; A example; }; I do believe that it would not work would the alternative be to pass variables into printvars ie void printvars(array of ints){ print arrayofints; } I am a little confused. BTW this has to do with a pattern called strategy that i'm trying to learn. In all reality it is these classes:
class Account{
protected:
	var name;
	var phone;
	var address;
	AccountValidator validator;
public:
	bool validate(){
		validator.validate();
	}
};
class CreditAccount extends Account{
protected:
	var ccnumber;
	var ccexp;
	var ccpin;
public:
	CreditAccount(){
		validator = new CCValidator;
	}
};
class CheckAccount extends Account{
protected:
	var acctnumber;
	var routingnumber;
	var checknumber;
public:
	CheckAccount{
		validator = new CheckValidator;
	}
};

class AccountValidator{
public:
	validate(){
		if(name == ''){
			return false;
		}
		if(phone == ''){
			return false;
		}
	}
};
class CCValidator extends AccountValidator{
	validate(){
		super.validate();
		if(ccnumber == ''){
			return false;
		}
	}
};
class CheckValidator extends AccountValidator{
	validate(){
		super.validate();
		if(routingnumber == ''){
			return false;
		}
	}
};

thanks for lookin, durfy

Share this post


Link to post
Share on other sites
An instance of class A does not know, and should not know that (or whether) it's part of a B object.

If you need object of class A to alter properties of an object of class B, pass a reference to the target B object to A's constructor.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
An instance of class A does not know, and should not know that (or whether) it's part of a B object.

If you need object of class A to alter properties of an object of class B, pass a reference to the target B object to A's constructor.


Thx victor you've been the bomb on here for years. Remember ssjcory :-) thats me. I dont need to modify properties of class B just check the values for validation. Do I still need a reference to B or can i just pass an array?
Thanks,
Durfy

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
An instance of class A does not know, and should not know that (or whether) it's part of a B object.

If you need object of class A to alter properties of an object of class B, pass a reference to the target B object to A's constructor.


Also on a side note this is really in php this is more of just a pseudocode representation.
-Durfy

Share this post


Link to post
Share on other sites
A can't be a Inner class (basically a subclass) of B because A and B are two seperate things. A is accounting information. B is a processor for validation. I am keeping them seperate so that if I create a new payment type ie Debit I can still use the creditvalidation processor.
THanks for the comment tho,
Durfy

Share this post


Link to post
Share on other sites
Does anyone know how i would validate items from my Account classes using my validation classes. Would I just pass an array of strings through the validate method?
Thanks,
durfy

Share this post


Link to post
Share on other sites
I have already proposed that you pass the account to be verified to the validator:

class Account 
{
function Account()
{
$this->validator = new WhateverValidator($this)
}

function Validate()
{
return $this->validator->validate();
}

function SetName($name)
{
assert (is_string($name));
$this->name = $name;
}

function GetName()
{
assert (isset($this->name));
return $this->name;
}
}

class WhateverValidator
{
function WhateverValidator(& $target)
{
$this->target = & $target;
}

function Validate()
{
if ($target->GetName() == "") return false;
return true;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Durfy
A can't be a Inner class (basically a subclass) of B

An inner class and a subclass a two completely different things, they have nothing to do with each other.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this