Mathsniper study note

Wednesday, June 14, 2006

the problem of defination between "int" and "register int"

What is the difference between datatype "int" and "register int"?
Is "int" by default set to "register"? Since "register int"
increases the speed of loops, can I use them at all times, or there
are limitations?

"register" is one of the keywords used to give a hint to the compiler as to how to generate code. In this case, you are asking the compiler to try to keep a variable in a register instead of in memory. Registers can be accessed much faster than memory locations. A compiler is free to ignore hints like "register". The limitation is that most CPUs have very few registers free for use by the program. If you mark ten variables as "register", it is possible that none of them will actually be assigned to a register. Probably one or two concurrent "register" variables will work on some host systems. It all depends. It is probably better to write speed-critical software in assembly language for the specific host CPU; then you have full control over variable allocation and program execution.

Wednesday, June 07, 2006

binary search code(C version)

Here we are going to demostrate how to implement binary search engine.

int binarySearch(int *a, int l, int h, int x)
{
int r;
do {
r = (int)(l+h)/2;
if (a[r] == x) return m;
if (a[r] < x)
l = r+1;
else h = r-1;
} while(l <= h);
return -1;

Monday, April 17, 2006

the standard inheritance sample

Some program sources from "How to C++ program". And I edited some sources. I wanna demonstrate how to make a type casting between base class and derived class.(Certainly, we must type_cast derived class to base class.). I will recount more details later.
--------------------------------------------
employee1.cpp
--------------------------------------------
#include
#include
#include
#include

using namespace std;

class Employee {
public:
Employee( const char *, const char * );
~Employee();
const char *getFirstName() const;
const char *getLastName() const;
void print() const;
private:
char *firstName;
char *lastName;
};

Employee::Employee( const char *first, const char *last )
{
firstName = new char[ strlen( first ) + 1 ];
assert( firstName != 0 );
strcpy( firstName, first );

lastName = new char [ strlen( last ) + 1 ];
assert( lastName != 0 );
strcpy( lastName, last );
}

Employee::~Employee()
{
delete [] firstName;
delete [] lastName;
}

const char *Employee::getFirstName() const
{ return firstName; }

const char *Employee::getLastName() const
{ return lastName; }

void Employee::print() const
{
cout << lastName << ' ' << firstName;
}

class HourlyWorker : public Employee {
public:
HourlyWorker( const char *, const char *, double = 0.00, double = 0.00);
double getPay() const;
void print() const;
private:
double wage;
double hours;
};

HourlyWorker::HourlyWorker( const char *first,
const char *last,
double initHours, double initWage )
: Employee( first, last )
{
hours = initHours;
wage = initWage;
}

double HourlyWorker::getPay() const
{ return hours * wage; }

void HourlyWorker::print() const
{
cout << "HourlyWorker::print() is executing\n";
Employee::print();

cout << " is an hourly worker with pay of $"
<< setiosflags( ios::fixed | ios::showpoint )
<< setprecision( 2 ) << getPay() << endl;
}

int main()
{
HourlyWorker h( "Fantasy", "Final", 12.00, 43.00 );
h.print();

/* Demonstrating how to make base class type casting to derived class */
Employee *ePtr = 0, e( "Ken", "Stephen" );
HourlyWorker *hwPtr = 0, hw( "Job", "Steve", 31.00, 22.00 );
ePtr = &e;
cout << "\n\nePtr->print(), ePtr = &e:\n";
ePtr->print();

hwPtr = static_cast<> ( ePtr );
cout << "\n\nhwPtr->print(), before point to hw:\n";
hwPtr->print();

hwPtr = &hw;
cout << "\n\nhwPtr->print(), after point to hw:\n";
hwPtr->print();
cout << "\n\n";
return 0;
}
--------------------------------------------
output
--------------------------------------------
mathsniper:~# ./employee1
HourlyWorker::print() is executing
Final Fantasy is an hourly worker with pay of $516.00


ePtr->print(), ePtr = &e:
Stephen Ken

hwPtr->print(), before point to hw:
HourlyWorker::print() is executing
Stephen Ken is an hourly worker with pay of $0.00


hwPtr->print(), after point to hw:
HourlyWorker::print() is executing
Steve Job is an hourly worker with pay of $682.00

關于C++中繼承(inheritance)範例

這裏我會演示一下 C++中繼承的簡單操作,其中我會使用基類Point和派生類Circle,Point class中其數據成員為protected權限,這使得Circle Object function可以訪問Point數據。
point.cpp
#include

using namespace std;

class Point {
public:
Point( int = 0, int = 0 );
~Point();
protected:
int x, y;
};

Point::Point( int a, int b )
{
x = a;
y = b;
cout << "Point Constructor: "
<< '[' << x << ", " << y << ']' << endl;
}

Point::~Point()
{
cout << "Point Destructor: "
<< '[' << x << ", " << y << ']' << endl;
}

class Circle : public Point {
public:
Circle( double r = 0.0, int a = 0, int b = 0 );
~Circle();
private:
double radius;
};

Circle::Circle( double r, int a, int b )
: Point( a, b )
{
radius = r;
cout << "Circle Constructor: "
<< "radius = " << radius
<< ", x = " << x
<< ", y = " << y << endl;
}

Circle::~Circle()
{
cout << "Circle Destructor: "
<< "radius = " << radius
<< ", x = " << x
<< ", y = " << y << endl;
}
int main()
{
Point p( 11, 22 );
Circle circle1( 3.12, 14, 19 );
cout << endl;
Circle circle2( 11.2, 34, 13 );
cout << endl;
return 0;
}
[/code]
其中輸出數據如下:

mathsniper:~# ./point
Point Constructor: [11, 22]
Point Constructor: [14, 19]
Circle Constructor: radius = 3.12, x = 14, y = 19

Point Constructor: [34, 13]
Circle Constructor: radius = 11.2, x = 34, y = 13

Circle Destructor: radius = 11.2, x = 34, y = 13
Point Destructor: [34, 13]
Circle Destructor: radius = 3.12, x = 14, y = 19
Point Destructor: [14, 19]
Point Destructor: [11, 22]

留意下數據base class(Point)和derived class(Circle) constructor,desstructor的輸出順序,
當我們要生成一個Circle Object(這裏為Circle 1及Circle 2)時,它先會調用base class constructor,再調用自身的constructor,當main function執行return 0後,先調用自身destructor,再繼而調用base class destructor,整個過程似是Stack的操作。事實上是如似的。