• Advertisement
Sign in to follow this  

Quick question about classes.

This topic is 3976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Make A and inner class of B (if your language supports this), then A has access to all member variables of B.

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
Sign in to follow this  

  • Advertisement