precondition and post condition check

Joined
Feb 12, 2008
Messages
108
Reaction score
0
Hello everyone,


I am looking for a good sample about how to implement C++ pre-condition and post condition check, but can not find a good sample code. Do you have any referred ones? :)

Since I can not find, I wrote the patterns in two ways, I am not sure which is correct and if both are not correct, how to implement this pattern?

Sample code 1,

Code:
#define MAX 1024

class Base
{
public:
	void foo(int i)
	{
	if (i > MAX)
	{
		// error handling
	}
	else
	{
		do_foo(i);
	}
}

private:
	virtual void do_foo(int i) = 0;
};

class Derived : public Base
{
private:
	virtual void do_foo(int i)
	{
	// i is never > MAX here
	}
};

int main()
{
	Derived d;
	d.foo (1000);

	return 0;
}

Sample 2,

Code:
#define MAX 1024

class Base
{
public:
	void foo(int i)
	{
	if (i > MAX)
	{
		// error handling
	}
	else
	{
		do_foo(i);
	}
}

private:
	virtual void do_foo(int i) = 0;
};

class Derived : public Base
{
public:
	virtual void do_foo(int i)
	{
		foo (i);
		// i is never > MAX here
	}
};

int main()
{
	Derived d;
	d.do_foo (1000);

	return 0;
}


thanks in advance,
George
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top