Archive for the ‘C++’ Category

How to add external libraries for compilation in VC++?

July 5, 2010

This is my first adventure using Visual C++ in Visual Studio 2010. It was quite an adventure…

Right click project:

1. “Configuration properties”->”C/C++”->”General”->”Additional Include Directories” to add location(s) of the libraries;

2. “Configuration properties”->”Linker”->”General”Additional Library Directories” to repeat locations(s) from #1.

Advertisements

Block statements in control flow constructs

December 2, 2009

There is no need to block (using braces to wrap) single line statements in a control flow (as-if, do-while, for/while); and you can mix blocks and non block statements together like this:

if(num > 0)
trace(num+” is positive;\n”);
else if(num <0)
{
trace(num+” is negative.”);
trace(num+” don’t be negative;\n”);
}
else if(num == 0)
trace(num+” is neutral;\n”);

This applies to all the control statements although the example here is just of if/else .

With this being said, The Elements of C# Style recommends Always us block statements in control flow constructs. Reason one is that it makes it easy to add additional statements in it; reason two is it’s easier to read than those without blocks.

Postfix operators: x++

March 30, 2009

Although postfix operators are unary operators, they are classified separately from the rest of the unary operators because of their higher precedence and special behavior. When a postfix operator is used as part of a larger expression, the expression’s value is returned before the postfix operator is processed. In other unary operators, such as prefix increments/decrements, the increment or decrement operation is completed before the value of the overall expression is returned.

#include <iostream>
using namespace std;

int main()
{
int max = 3;
int total = 7;

int count = 0;
cout<<“unary test for postfix:”<<endl;
for(int i = 0; i< total; i++)
{
count = count >= max? 0 : count++;
cout<<“#”<<i<<“: “<<“count is “<<count<<endl;
}
cout<<endl;

count = 0;
cout<<“unary test for prefix:”<<endl;
for(int i = 0; i< total; i++)
{
count = count >= max? 0 : ++count;
cout<<“#”<<i<<“: “<<“count is “<<count<<endl;
}
cout<<endl;

cout<<“for loop test:”<<endl;
count = 0;
for(int i = 0; i< total; i++)
{
if(count >= max)
{
count = 0;
}else
{
//both are the same.
//count++;
++count;
}
cout<<“#”<<i<<“: “<<“count is “<<count<<endl;
}
cout<<endl;
}
/**result
unary test for postfix:
#0: count is 0
#1: count is 0
#2: count is 0
#3: count is 0
#4: count is 0
#5: count is 0
#6: count is 0

unary test for prefix:
#0: count is 1
#1: count is 2
#2: count is 3
#3: count is 0
#4: count is 1
#5: count is 2
#6: count is 3

for loop test:
#0: count is 1
#1: count is 2
#2: count is 3
#3: count is 0
#4: count is 1
#5: count is 2
#6: count is 3
*/

Pointers in C++

January 17, 2009

1. Addresses in memory are essentially numbers that can be stored in (unsigned) integer variables. In my computer, it showed up as hexadecimals such as “0xbfb3a7b4”;

2. Pointer should be considered “a data type that holds an address of some variable”. A pointer variable is declared as “float*”, “char*”, etc.. “float*” holds a variable of “a type of float pointer” and “char*” holds a variable of “a type of char pointer” (which means it stores an address of a char variable). A primary motivation for having a pointer type for each variable type is to help the compiler. See “Pointer Arithmetic” section in this tutorial;

3. Two distinct steps when you try to access the value of a variable. First is to & (find the address in memory) and second is to *(retrieve the value from the address(es));

4. None pointer type can be cast to pointer type and vice versa. Since Pointer is a special type of object, it can be used to cast other types of object to the pointer type of choice. The following example casts type of unsigned integer to float pointer before retrieving its value and casts a pointer to unsigned integer.

float fl=3.14;
unsigned int addr=(unsigned int) &fl;
*(float*) addr = 12.34567;
int* flPtr = (int*) &fl;
unsigned int addr2 = (unsigned int) flPtr;
std::cout << fl <<” “<< &fl<< ” “<<flPtr<< ” “<<addr2<<std::endl;

5. An Array variable is actually already a pointer. It stores the address of the first object in the array; Accessing the array (*arr) is actually accessing the first element in the array (arr[0]):

int arr3[3] = {19, 39, 59};
int* b = arr3;
std::cout<<b<<“==? “<<&arr[0]<<std::endl;
std::cout<<b<<” ==? “<<arr3<<” ==? “<<&arr3[0]<<std::endl;
std::cout<<*arr3<<” ==? “<<arr[0]<<std::endl;
cout<< *(arr3+1)<<” ==? “<<arr3[1]<<endl;

6. Pointer arithmetic for accessing array /array elements. Use “*(people+1)” to access the second element in Array people (The followings are all true:

*(people)==*(people+0)==people[0];
*(people+1)==*(1+people)==people[1];
[1]people==*(1+people)==*(people+1)).

C++ compiler does not prevent pointer index falling out of range (nor does it complain when this happens). For “int people[5]”, *(people+5) will print out some none sense (the code will compile and run!).

Pionters in C and C++;

Array in C and C++;

and an interesting code discussion about “Pointer to array”

Memery allocation in C++/C

December 14, 2008

Dynamic memeory allocation in C++

Why C has pointers

Class declarations and definitions

December 14, 2008

0. A declaration which reserves memory is more properly known as a definition in C++.

1. Class declarations is different than class definitions in C++.

2. Class declaration is what you do in a header file. The “class declaration block” consists of the keyword class, followed by the name of the object, a pair of braces, and a semi-colon. It basically declares class interface, with function prototypes and static constants.

3. Non constant static members (i.e., static class variables) need to be declared but not initialized, inside a class (header), like so:

static int totalStudents;

The member has to initialized outside the class (header) and inside a class instance definition (i.e., the source code file, *.cpp), like so:

int GradeBook::totalStudents = 10;

4. When you initializing a class instance whose constructor takes no parameters, you do it like this:

GradeBook gb;

as opposed to

GradeBook gb();

But if the constructor does take parameters, you do it like this:

GradeBook gb("C++ Programming");

Example:
// GradeBook2.h
#include
class GradeBook
{
public:
GradeBook(bool foo);
GradeBook();
~GradeBook();
void countGrades();
void displayResults();
//const int TEST = 12; //wrong.
static int totalStudents; // non constant static member needs to be declared inside class here.
static const int TOTAL_STUDENTS_SCHOOL = 200; //constants has to be declared and initialized here inside class.s
//static const int TOTAL; // wrong. will compile but get runtime error whenever this member data is referred. "undefined reference to `GradeBook::TOTAL'"
private:
int passedTotal;
int failedTotal;
};


// GradeBook2.cpp
#include "gradebook2.h"
#include <iostream>;
using namespace std;

int GradeBook::totalStudents = 10;
//int GradeBook::TOTAL = 300; //error: declaration of ‘const int GradeBook::TOTAL’ outside of class is not definition

GradeBook::GradeBook(bool foo)
{
cout<<“you said it’s “<<<“\n”;
countGrades();
};

GradeBook::GradeBook()
{
cout<<“you did NOT say anything\n”;
countGrades();
};

GradeBook::~GradeBook()
{
cout<<“bye bye!\n”;
};

void GradeBook::countGrades()
{
cout<<“this is a gradebook! and we have a total of “<< TOTAL_STUDENTS_SCHOOL <<” students in school!\n”;
//cout<<“the total is “<< TOTAL <<” though!\n”;
//cout<<“Total students in the book are “<<<“/n”;//test
}

void GradeBook::displayResults()
{
}


// runner.cpp
#include "gradebook2.cpp"
#include <iostream>;
using namespace std;

int main()
{
//int GradeBook::totalStudents;//error: invalid use of qualified-name ‘GradeBook::totalStudents’
GradeBook gb(true);
GradeBook gb2;
}

Some links:

C++ static varialbles

Declaring a class

Class declaration vs. class definition

The C++ ‘const’ Declaration: Why & How

How to declare/initialize a constant in a class?