Thursday, October 27, 2011

Conditional operator (if operator)

conditional operator (also called "if operator"), if in the form of

(condition ? statements if condition is true : statements if condition is false)

It's equal to:
if(condition){
statements if condition is true;
}else{
statements if condition is false;
}

#include <iostream>

using namespace std;

int main()
{
cout << "conditional operator (if operator)" << endl << endl;

cout << (true ? "It's TRUE" : "It's FALSE") << endl;
cout << (false ? "It's TRUE" : "It's FALSE") << endl;

cout << "3 > 5: " << ((3 > 5) ? "Yes" : "No") << endl;
cout << "'Z' > 'X': " << (('Z' > 'X') ? "Yes" : "No") << endl;

return 0;
}


Conditional operator (if operator)



Wednesday, October 26, 2011

The C++/CX Episode

GoingNative 3: The C++/CX Episode with Marian Luparu




Tuesday, October 25, 2011

Generate random number with srand()

In C/C++, the rand() function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. This function return a pseudo-random integer, not true.

The srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand have been made, the same sequence shall be generated as when srand() is first called with a seed value of 1.

In this exercise, current date and time, time(0), is used as seek for srand(). Such that the generated pseudo-random numbers will be a more random.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
cout << "Random number generated by rand()" << endl;
for(int i=0; i < 10; i++){
cout << rand() % 100 << " ";
}
cout << endl;

cout << "Random number generated with srand()" << endl;
srand(static_cast<unsigned int>(time(0)));
for(int i=0; i < 10; i++){
cout << rand() % 100 << " ";
}
cout << endl;

return 0;
}


Generate random number with srand()

Friday, October 21, 2011

Comparison of Java and C++

The differences between the C++ and Java programming languages can be traced to their heritage, as they have different design goals.
  • C++ was designed for systems and applications programming, extending the C programming language. To this procedural programming language designed for efficient execution, C++ has added support for statically-typed object-oriented programming, exception handling, scoped resource management, and generic programming, in particular. It also added a standard library which includes generic containers and algorithms.
  • Java was created initially as an interpreter for printing systems but grew to support network computing. Sun Microsystems used it for the basis of their "HotJava" thin client system. It relies on a virtual machine to be secure and highly portable. It is bundled with an extensive library designed to provide a complete abstraction of the underlying platform. Java is a statically-typed object-oriented language that uses a syntax similar to C++, but is not compatible with it. It was designed from scratch with the goal of being easy to use and accessible to a wider audience.


Wikipedia have a article, Comparison of Java and C++, it's list the difference in more details.

It's a sample chapter of the book Java in a Nutshell, describe How Java Differs from C.

Tuesday, October 11, 2011

Exercise of STL stack

#include <iostream>
#include <stack>

using namespace std;

int main()
{
cout << "STL stack exercise" << endl << endl;

stack<int> MyStack;
cout << "init without anything" << endl;
cout <<"MyStack.size(): " << MyStack.size() << endl << endl;

cout << "push something in" << endl;
MyStack.push(1);
MyStack.push(2);
MyStack.push(3);
MyStack.push(4);
cout <<"MyStack.size(): " << MyStack.size() << endl << endl;

cout << "pop all out" << endl;
while (!MyStack.empty())
{
cout << MyStack.top() << endl;
MyStack.pop();
}

cout << endl << endl;

return 0;
}


Exercise of STL stack


Saturday, October 8, 2011

Doubly Linked List

#include <iostream>

using namespace std;

class Node {
private:
Node();
Node(string s, Node* p, Node* n);
string body;
Node* prev;
Node* next;
friend class DoublyLinkedList;
};

Node::Node(){
};

Node::Node(string s, Node* p, Node* n){
body = s;
prev = p;
next = n;
};

class DoublyLinkedList{
public:
DoublyLinkedList();
~DoublyLinkedList();
void AddInFront(string s);
void RemoveInFront();
void AddInBack(string s);
void RemoveInBack();
void ShowAll();
private:
Node* head;
Node* trail;
};

DoublyLinkedList::DoublyLinkedList(){
head = new Node();
trail = new Node();
head->next = trail;
trail->prev = head;
};

DoublyLinkedList::~DoublyLinkedList(){
while (head->next != trail){
RemoveInFront();
}
delete head;
delete trail;
};

void DoublyLinkedList::AddInFront(string s){
Node* nxFrontNode = head->next;
Node* n = new Node(s, head, head->next);
head->next = n;
nxFrontNode->prev = n;
};

void DoublyLinkedList::RemoveInFront(){
if(head->next != trail){
Node* nxFrontNode = head->next;
Node* nnxFrontNode = nxFrontNode->next;
head->next = nnxFrontNode;
nnxFrontNode->prev = head;
delete nxFrontNode;
}
};

void DoublyLinkedList::AddInBack(string s){
Node* nxBackNode = trail->prev;
Node* n = new Node(s, trail->prev, trail);
trail->prev = n;
nxBackNode->next = n;
};

void DoublyLinkedList::RemoveInBack(){
if(trail->prev != head){
Node* nxBackNode = trail->prev;
Node* nnxBackNode = nxBackNode->prev;
trail->prev = nnxBackNode;
nnxBackNode->next = trail;
delete nxBackNode;
}
};

void DoublyLinkedList::ShowAll(){
Node* n = head->next;
while(n != trail){
cout << n -> body << endl;
n = n -> next;
}
}

int main()
{
cout << "- Doubly Linked List exercise -" << endl;

cout << "Create a new DoublyLinkedList: " << endl;
DoublyLinkedList myDoublyLinkedList;
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Remove nothing in a empty Doubly Linked List: " << endl;
myDoublyLinkedList.RemoveInFront();
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Insert something in Front: " << endl;
myDoublyLinkedList.AddInFront("ABC");
myDoublyLinkedList.AddInFront("DEFGH");
myDoublyLinkedList.AddInFront("I");
myDoublyLinkedList.AddInFront("JK");
myDoublyLinkedList.AddInFront("LMN");
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Remove something in Front: " << endl;
myDoublyLinkedList.RemoveInFront();
myDoublyLinkedList.RemoveInFront();
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Insert something in Back: " << endl;
myDoublyLinkedList.AddInBack("RST");
myDoublyLinkedList.AddInBack("UVW");
myDoublyLinkedList.AddInBack("XYZ");
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "Remove something in Back: " << endl;
myDoublyLinkedList.RemoveInBack();
myDoublyLinkedList.RemoveInBack();
myDoublyLinkedList.ShowAll();
cout << endl;

cout << "end!" << endl;
return 0;
}


Doubly Linked List


Monday, October 3, 2011

C++11 Features in Visual C++ 11

There's a new C++ Standard and a new version of Visual C++, and it's time to reveal what features from the former we're implementing in the latter!

Here is a article from MSDN Blogs talking about the new C++11 Features in Visual C++ 11.

Singly Linked List

example:
#include <iostream>

using namespace std;

class Node {
private:
Node(string s, Node* n);
string body;
Node* next;
friend class SinglyLinkedList;
};

Node::Node(string s, Node* n){
body = s;
next = n;
};

class SinglyLinkedList{
public:
SinglyLinkedList();
~SinglyLinkedList();
void AddInFront(string s);
void RemoveInFront();
void ShowAll();
private:
Node* head;
};

SinglyLinkedList::SinglyLinkedList(){
head = NULL;
};

SinglyLinkedList::~SinglyLinkedList(){
while (head != NULL){
RemoveInFront();
}
};

void SinglyLinkedList::AddInFront(string s){
Node* n = new Node(s, head);
head = n;
};

void SinglyLinkedList::RemoveInFront(){
if(head != NULL){
Node* tmp = head;
head = head->next;
delete tmp;
}
};

void SinglyLinkedList::ShowAll(){
Node* n = head;
while(n != NULL){
cout << n -> body << endl;
n = n -> next;
}
}

int main()
{
cout << "- Singly Linked List exercise -" << endl;

cout << "Create a new SinglyLinkedList: " << endl;
SinglyLinkedList mySinglyLinkedList;
mySinglyLinkedList.ShowAll();
cout << endl;

cout << "Remove nothing in a empty Singly Linked List: " << endl;
mySinglyLinkedList.RemoveInFront();
mySinglyLinkedList.ShowAll();
cout << endl;

cout << "Insert something: " << endl;
mySinglyLinkedList.AddInFront("ABC");
mySinglyLinkedList.AddInFront("DEFGH");
mySinglyLinkedList.AddInFront("I");
mySinglyLinkedList.AddInFront("JK");
mySinglyLinkedList.AddInFront("LMN");
mySinglyLinkedList.ShowAll();
cout << endl;

cout << "Remove something: " << endl;
mySinglyLinkedList.RemoveInFront();
mySinglyLinkedList.RemoveInFront();
mySinglyLinkedList.ShowAll();
cout << endl;

return 0;
}


Singly Linked List



Sort STL vector of C++

example:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>

using namespace std;

void showMyVector(const vector<int>& v);

int main()
{
vector<int> myVector;

cout << "Start with nothing" << endl;
showMyVector(myVector);

cout << "Insert something" << endl;
for (int i = 0; i < 10; i++) {
myVector.push_back(rand() % 100);
}
showMyVector(myVector);
cout << endl;

cout << "Sort the vector" << endl;
for (int i = 0; i < 10; i++) {
sort(myVector.begin(), myVector.end());
}
showMyVector(myVector);
cout << endl;

return 0;
}

void showMyVector(const vector<int>& v){
cout << "myVector.empty: " << v.empty() << endl;
cout << "myVector.size(): " << v.size() << endl;
for(unsigned i = 0; i < v.size(); i++){
cout << v[i] << " ";
}
cout << endl;
}


Sort STL vector of C++


Sunday, October 2, 2011

STL vector of C++

example:
#include <iostream>
#include <vector>

using namespace std;

void showMyVector(const vector<int>& v);

int main()
{
vector<int> myVector;

cout << "Start with nothing" << endl;
showMyVector(myVector);

cout << "Insert something" << endl;
myVector.push_back(0);
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
myVector.push_back(4);
myVector.push_back(5);
myVector.insert(myVector.begin()+2, 9);
myVector.insert(myVector.end()-1, 10);
myVector.pop_back();
showMyVector(myVector);

cout << "Clear" << endl;
myVector.clear();
showMyVector(myVector);

return 0;
}

void showMyVector(const vector<int>& v){
cout << "myVector.empty: " << v.empty() << endl;
cout << "myVector.size(): " << v.size() << endl;
for(unsigned i = 0; i < v.size(); i++){
cout << v[i] << endl;
}
cout << endl;
}


STL vector of C++


Friday, September 30, 2011

Qt, a cross-platform application framework - updated version 1.1.3

Qt
The Qt SDK includes the tools you need to build desktop, embedded and mobile applications with Qt from a single install. This is the recommended way to get started with Qt. The latest SDK has a self updater feature that will keep you up to date as new versions are made available.

To align with the Nokia N9 release, Qt SDK updated version 1.1.3.

website: http://qt.nokia.com/



Wednesday, September 28, 2011

First exercise using Keil uVision 4 for 8051 micro-controller

It's easy to create a 8051 project using Keil 8051 Development Tools, uVision 4. You can also debug the program using the build-in simulator.

- New a uVision Project in uVision 4 IDE.
New a uVision Project

- Select project location and file name.
Select project location and file name

- Select 8051 (all Variants) under Generic in Data base, and click OK.
Select 8051 (all Variants)

- Accept Copy Standard 8051 Startup Code to Project Folder and Add File to Project. The IDE will copy STARTUP.A51 file for you.
Copy Standard 8051 Startup Code to Project Folder and Add File to Project

- Right click th project "Target1" and select Options for Target 'Target1'...
Select Options for Target

- Select Small Memory Model and Small Code Rom Size, and click OK.
Config memory model

- Click File -> New... to creat your main.c

- Type in the program, and save it as main.c.
#include <reg51.h>

void DummyDelay();

void main(void){
char c = 0x00;
while(1){
P1 = c++;
DummyDelay();
}

}

void DummyDelay(){
unsigned char i, j;

for(i = 0; i < 0xFF; i++){
for(j = 0; j < 0xFF; j++){
}
}
}

Edit the code

- Right click Source Group 1 under Target1 in Project, and Add Files to Group 'Source Group 1'...
Add Files to Source Group

- Browse to select the main.c just created.
Add main.c

- Click Project to Build Target.
Build Target

- After finished without Errors. Click Debug and Start/Stop Debug Session.
Start Debug Session

- Click Peripherals -> I/O - Ports -> Port 1 to open the Parallel Port 1 monitor window.
Open Port 1 Monitor
Port 1 Monitor

- Finally click on the Run icon, or F5, to run the program in simulated mode. You can see the Port 1 Bits in Parallel Port 1 monitor change while the program running.
Running in Simulator

- Click Debug and Start/Stop Debug Session, or Ctrl-F5 to exit and back to edit mode.

Tuesday, September 27, 2011

Pointer and Array

In this exercise, an array of int and another array of char are created. On the other hand, a pointer to int and a pointer to char is defined. And then the pointers are assigned to the address of the arrays, and the elements of the array can be accessed by *(pointer + offset).

Pointer and Array

#include <iostream>

using namespace std;

const int LEN = 10;
int A_int[LEN];
int* ptA_int;
char A_char[LEN];
char* ptA_char;

int main()
{
cout << "Pointer and Array" << endl;

int i;
char c;

for (i = 0, c = 'a'; i < LEN; i++, c++){
A_int[i] = i;
A_char[i] = c;
cout << A_int[i] << "/" << A_char[i] << " ";
}
cout << endl;

ptA_int = A_int;
ptA_char = A_char;
for (i = 0; i < LEN; i++){
cout << *(ptA_int + i) << " ";
}
cout << endl;

for (i = 0; i < LEN; i++){
cout << *(ptA_char + i) << " ";
}
cout << endl;

return 0;
}



Keil 8051 Development Tools

The Keil 8051 Development Tools are designed to solve the complex problems facing embedded software developers.
  • When starting a new project, simply select the microcontroller you use from the Device Database and the µVision IDE sets all compiler, assembler, linker, and memory options for you.
  • Numerous example programs are included to help you get started with the most popular embedded 8051 devices.
  • The Keil µVision Debugger accurately simulates on-chip peripherals (I²C, CAN, UART, SPI, Interrupts, I/O Ports, A/D Converter, D/A Converter, and PWM Modules) of your 8051 device. Simulation helps you understand hardware configurations and avoids time wasted on setup problems. Additionally, with simulation, you can write and test applications before target hardware is available.
  • When you are ready to begin testing your software application with target hardware, use the MON51, MON390, MONADI, or FlashMON51 Target Monitors, the ISD51 In-System Debugger, or the ULINK USB-JTAG Adapter to download and test program code on your target system.

Overview of C51 Microcontrollers Development Tool


Download FREE Keil C51 Evaluation Kit
C51 Evaluation Tools Limitations
  • The 8051 compiler, assembler, linker, and debugger are limited to 2 Kbytes of object code. Source code may be of any size.
  • Programs that generate more than 2 Kbytes of object code will not compile, assemble, or link.
  • The debugger supports programs that are 2 Kbytes or smaller.
  • The startup code generated includes LJMPs. Code generated cannot be used in single-chip devices that support 2 Kbytes or less of program space.
  • Programs start at offset 0x0800. Programs generated with the evaluation software may not be programmed into single-chip devices with less than 2 Kbytes of on-chip ROM.
  • No hardware support for multiple DPTR registers is provided.
  • No support for floating-point arithmetic and no support for user libraries is provided.
  • No support for in-line assembly using #pragma ASM.
  • The following components which are present in the PK51 Full Version are not included in the Evaluation Version: Linker for Code Banking, Library Manager, and RTX51 Tiny Real-time Operating System.


Refer: First exercise using Keil uVision 4 for 8051 micro-controller


Monday, September 26, 2011

Implement Insertion sort in C/C++

Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.

Insertion sort

#include <iostream>
#include <cstdlib>
using namespace std;

void InsertionSort(int* ar, int len);

const int LENGTH = 10;
int data[LENGTH];

int main()
{
//Prepare data
for (int i = 0; i < 10; i++) {
data[i] = rand() % 100;
}

cout << "data before Insertion Sort:" << endl;
for (int i = 0; i < 10; i++)
cout << data[i] << " ";
cout << endl << endl;

InsertionSort(data, LENGTH);

cout << endl << "data before Insertion Sort:" << endl;
for (int i = 0; i < 10; i++)
cout << data[i] << " ";
cout << endl;

return 0;
}

void InsertionSort(int* ar, int len){
int cur, j;

for (int i = 1; i < len; i++) {
cur = ar[i];
j = i - 1;
while ((j >= 0) && (ar[j] > cur)) {
ar[j + 1] = ar[j];
j--;
}
ar[j + 1] = cur;

for (int ii = 0; ii < 10; ii++)
cout << ar[ii] << " ";
cout << endl;
}
}

Sunday, September 25, 2011

Create HelloWorld Windows Form Application, using Visual C++ 2010 Express

Create HelloWorld Windows Form Application, using Visual C++ 2010 Express

- Create New Project using template of Visual C++ Windows Form Application, with Name of "HelloWorld", and click OK.
Create Visual C++ Windows Form Application

- Click on "Toolbox" on right to expand Toolbox, drag a label and button over the Form.
Place components

- Double Click on the button, to generate the code of button1_Click() method. Visual C++ will open the code designer for you.

- Add the code inside button1_Click() method:
 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
label1->Text = "Hello World";
}
};

Add code

- Save and Run.
Click on the button, the text on the label will be changed to "Hello World".
Hello World

FREE NetBeans IDE

The NetBeans IDE is an award-winning integrated development environment available for Windows, Mac, Linux, and Solaris. The NetBeans project consists of an open-source IDE and an application platform that enable developers to rapidly create web, enterprise, desktop, and mobile applications using the Java platform, as well as PHP, JavaScript and Ajax, Groovy and Grails, and C/C++.

NetBeans web site: http://netbeans.org/

Thursday, September 22, 2011

Windows Runtime internals: understanding "Hello World"



The Windows Runtime is a key piece of technology used by all Metro style apps in Windows. How exactly does it work though? You too can become a Windows Runtime Expert by taking a deep dive into the internals of the Windows Runtime using not much more than a new project in Visual Studio and a debugger. After this talk, you will understand how A "Hello World" Metro style app uses the Windows Runtime, enabling you to build better apps.

Sunday, September 18, 2011

Virtual Function and Virtual Destructor

Consider the case: There is a base class BASE, and a derived class DERIVED. Both have a member function getInfo(). It's a array of pointer to BASE object. Because object of DERIVED "is a" BASE object also, so it can be pointed by the array.



When we call the getInfo() function of DERIVED object pointed by a pointer to BASE, which function will be called? BASE::getInfo()? or DERIVED::getInfo()? - The answer is BASE::getInfo()!



It's also happen on the destructor, when we delete a DERIVED object pointed by a pointer to BASE, the destructor of BASE will be called.



It's called static binding.



#include <iostream>

using namespace std;

class BASE{
public:
BASE(int x, int y, int z);
~BASE();
void getInfo();
int a;
protected:
int b;
string Im;
private:
int c;
};



BASE::BASE(int x, int y, int z)
:a(x), b(y), c(z){
Im = "Base Class";
}

BASE::~BASE(){
cout << "BASE's destructor! " << Im << endl;
}

void BASE::getInfo(){
cout << "BASE::getInfo()" << " : " << Im << endl;
}

class DERIVED:public BASE{
public:
DERIVED(int x, int y, int z, int w);
~DERIVED();
void getInfo();
private:
int d;
};

DERIVED::DERIVED(int x, int y, int z, int w)
:BASE(x, y, z), d(w){
Im = "Derived Class";
}

DERIVED::~DERIVED(){
cout << "DERIVED's destructor! " << Im << endl;
}

void DERIVED::getInfo(){
cout << "DERIVED::getInfo()" << " : " << Im << endl;

}

int main()
{
BASE* base[2];
base[0] = new BASE(1, 2, 3);
base[1] = new DERIVED(4, 5, 6, 7);
base[0]->getInfo();
base[1]->getInfo();
cout << endl;
cout << "delete base[0]" << endl;
delete base[0];
cout << endl;
cout << "delete base[1]" << endl;
delete base[1];
return 0;
}




static binding



To solve this problem, we can define both the getInfo() and destructor as virtual, to specify dynamic binding on the functions.



#include <iostream>

using namespace std;

class BASE{
public:
BASE(int x, int y, int z);
virtual ~BASE();
virtual void getInfo();
int a;
protected:
int b;
string Im;
private:
int c;
};



BASE::BASE(int x, int y, int z)
:a(x), b(y), c(z){
Im = "Base Class";
}

BASE::~BASE(){
cout << "BASE's destructor! " << Im << endl;
}

void BASE::getInfo(){
cout << "BASE::getInfo()" << " : " << Im << endl;
}

class DERIVED:public BASE{
public:
DERIVED(int x, int y, int z, int w);
virtual ~DERIVED();
virtual void getInfo();
private:
int d;
};

DERIVED::DERIVED(int x, int y, int z, int w)
:BASE(x, y, z), d(w){
Im = "Derived Class";
}

DERIVED::~DERIVED(){
cout << "DERIVED's destructor! " << Im << endl;
}

void DERIVED::getInfo(){
cout << "DERIVED::getInfo()" << " : " << Im << endl;

}

int main()
{
BASE* base[2];
base[0] = new BASE(1, 2, 3);
base[1] = new DERIVED(4, 5, 6, 7);
base[0]->getInfo();
base[1]->getInfo();
cout << endl;
cout << "delete base[0]" << endl;
delete base[0];
cout << endl;
cout << "delete base[1]" << endl;
delete base[1];
return 0;
}




dynamic binding


Writing modern C++ code - How C++ has evolved over the years



In this talk, Microsoft's C++ architect and chair of the ISO C++ standards committee Herb Sutter will showcase upcoming innovations in Visual C++ that show the Windows Runtime as a core part of native development. Windows 8 comes packed with rich APIs that are all Windows Runtime based, and C++ continues to deliver the tools you need to achieve the power and performance you have come to expect. Join us for a technical session that will serve as an overview of the new C++ programming model for the Windows Runtime and a prerequisite to other C++ sessions.

Download Microsoft® Visual Studio® 11 Developer Preview

Visual Studio 11 Developer Preview is an integrated development environment that seamlessly spans the entire life cycle of software creation, including architecture, user interface design, code creation, code insight and analysis, code deployment, testing, and validation.



Visual Studio 11 Developer Preview is an integrated development environment that seamlessly spans the entire life cycle of software creation, including architecture, user interface design, code creation, code insight and analysis, code deployment, testing, and validation. This release adds support for the most advanced Microsoft platforms, including the next version of Windows (code-named "Windows 8") and Windows Azure, and enables you to target platforms across devices, services, and the cloud. Integration with Team Foundation Server allows the entire team, from the customer to the developer, to build scalable and high-quality applications to exacting standards and requirements.



Visual Studio 11 Developer Preview is prerelease software and should not be used in production scenarios.



This preview can be installed to run side by side with an existing Visual Studio 2010 installation.



Note: This prerelease software will expire on June 30, 2012. To continue using Visual Studio 11 after that date, you will have to install a later version of the software.



Link: - Microsoft Download Center: Microsoft® Visual Studio® 11 Developer Preview (Web Installer)

Saturday, September 17, 2011

Constructor and Destructor of derived Class

Example of Constructor and Destructor of derived Class:



#include <iostream>

using namespace std;

class BASE{
public:
BASE(int x, int y, int z);
~BASE();
void doit();
int a;
protected:
int b;
string Im;
private:
int c;
};



BASE::BASE(int x, int y, int z)
:a(x), b(y), c(z){
Im = "Base Class";
}

BASE::~BASE(){
cout << "BASE's destructor! " << Im << endl;
}

void BASE::doit(){
cout << "BASE::doit()" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
}

class DERIVED:public BASE{
public:
DERIVED(int x, int y, int z, int w);
~DERIVED();
void doit();
private:
int d;
};

DERIVED::DERIVED(int x, int y, int z, int w)
:BASE(x, y, z), d(w){
Im = "Derived Class";
}

DERIVED::~DERIVED(){
cout << "DERIVED's destructor! " << Im << endl;
}

void DERIVED::doit(){
cout << "DERIVED::doit()" << endl;
BASE::doit();
cout << d << endl;
}

int main()
{

BASE base(1, 2, 3);
base.doit();
DERIVED derived(4, 5, 6, 7);
derived.doit();
return 0;
}




Constructor and Destructor of derived Class




Derived class cannot access private area of base class

example:



#include <iostream>

using namespace std;

class BASE{
public:
void doit();
int a;
protected:
int b;
private:
int c;
};

class DERIVED:public BASE{
public:
void doit();
};

void BASE::doit(){
cout << "BASE::doit()" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl;
};

void DERIVED::doit(){
cout << "DERIVED::doit()" << endl;
BASE::doit();
cout << "---" << endl;
cout << a << endl;
cout << b << endl;
cout << c << endl; //ERROR: cannot access private of base class

}

int main()
{

BASE base;
base.doit();
DERIVED derived;
derived.doit();
return 0;
}





Some function examples of STD string

example:



#include <iostream>

using namespace std;

string std_string;

int main()
{
cout << "STD String Test" << endl << endl;

std_string = "We wish you a merry Christmas and a Happy New Year!";
cout << "std_string = " << std_string << endl;
cout << "std_string.find(\"Christmas\") = " << std_string.find("Christmas") << endl;
cout << "std_string.substr(20, 9) = " << std_string.substr(20, 9) << endl;
cout << "std_string.insert(20, \"2011\") = " << std_string.insert(20, "2011 ") << endl;
cout << "std_string.erase(20, 5) = " << std_string.erase(20, 5) << endl;
cout << "std_string.replace(20, 6, \"X'\") = " << std_string.replace(20, 6, "X'") << endl;
return 0;
}




Some function examples of STD string




friend class

In definded friend class, it can access private data.



example:



#include <iostream>

using namespace std;

class MyClass{
public:
MyClass(const int mSize);
MyClass(MyClass& src);
~MyClass();
void fill(char c);
void getInfo();
MyClass& operator=(const MyClass& src);
private:
int size;
char* ptToChar;

friend class MyFriend;
};

//Constructor with default size
MyClass::MyClass(const int mSize = 10)
{
size = mSize;
ptToChar = new char[size];

}

//Copy Constructor
MyClass::MyClass(MyClass& src){
size = src.size;
ptToChar = new char[size];
for(int i = 0; i < size; i++)
ptToChar[i] = src.ptToChar[i];
}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << endl;
}

void MyClass::fill(char c){
for(int i = 0; i < size; i++)
ptToChar[i] = c;
}

void MyClass::getInfo()
{
cout << "size: " << size << endl;
for(int i = 0; i < size; i++)
cout << ptToChar[i];
cout << endl;
}

//Overload operator =
MyClass& MyClass::operator=(const MyClass& src){
if (this != &src){
delete[] ptToChar;

size = src.size;
ptToChar = new char[size];
for(int i = 0; i < size; i++)
ptToChar[i] = src.ptToChar[i];
}
return *this;
}

class MyFriend{
public:
void getFriendInfo(const MyClass& myfriend);
};

void MyFriend::getFriendInfo(const MyClass& myfriend){
cout << "size: " << myfriend.size << endl;
for(int i = 0; i < myfriend.size; i++)
cout << myfriend.ptToChar[i];
cout << endl;
}

int main()
{
MyClass myClass1(20);
myClass1.fill('A');
myClass1.getInfo();

MyClass myClass2(10);
myClass2 = myClass1;

myClass2.getInfo();

myClass1.fill('B');
myClass1.getInfo();
myClass2.getInfo();

cout << endl << "Create MyFriend friendA()" << endl;
MyFriend friendA;
friendA.getFriendInfo(myClass1);
friendA.getFriendInfo(myClass2);
return 0;
}




friend class


Overloading operator "=" for class with "new" object

Similar to the case in "using default Copy Constructor, when Class new some data". If we assign a object using "new" data inside, it will create a memory leak: because the default "=" operate assign the pointer to new value, without deleting the old data.



Here is a example to overloading operator "=":



#include <iostream>

using namespace std;

class MyClass{
public:
MyClass(const int mSize);
MyClass(MyClass& src);
~MyClass();
void fill(char c);
void getInfo();
MyClass& operator=(const MyClass& src);
private:
int size;
char* ptToChar;
};

//Constructor with default size
MyClass::MyClass(const int mSize = 10)
{
size = mSize;
ptToChar = new char[size];

}

//Copy Constructor
MyClass::MyClass(MyClass& src){
size = src.size;
ptToChar = new char[size];
for(int i = 0; i < size; i++)
ptToChar[i] = src.ptToChar[i];
}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << endl;
}

void MyClass::fill(char c){
for(int i = 0; i < size; i++)
ptToChar[i] = c;
}

void MyClass::getInfo()
{
cout << "size: " << size << endl;
for(int i = 0; i < size; i++)
cout << ptToChar[i];
cout << endl;
}

//Overload operator =
MyClass& MyClass::operator=(const MyClass& src){
if (this != &src){
delete[] ptToChar;

size = src.size;
ptToChar = new char[size];
for(int i = 0; i < size; i++)
ptToChar[i] = src.ptToChar[i];
}
return *this;
}

int main()
{
MyClass myClass1(20);
myClass1.fill('A');
myClass1.getInfo();

MyClass myClass2(10);
myClass2 = myClass1;

myClass2.getInfo();

myClass1.fill('B');
myClass1.getInfo();
myClass2.getInfo();
return 0;
}




Overloading operator


Friday, September 16, 2011

Using the Windows Runtime from C++



In this talk, Microsoft's C++ architect and chair of the ISO C++ standards committee Herb Sutter will showcase upcoming innovations in Visual C++ that show the Windows Runtime as a core part of native development. Windows 8 comes packed with rich APIs that are all Windows Runtime based, and C++ continues to deliver the tools you need to achieve the power and performance you have come to expect. Join us for a technical session that will serve as an overview of the new C++ programming model for the Windows Runtime and a prerequisite to other C++ sessions.

Visual Studio 11 Express

Microsoft Visual Studio 11 Express for Windows Developer Preview is a collection of tools that you can use to create, code, debug, localize, package, and deploy a Metro style app. In short, Visual Studio can help you do just about anything you need to do to develop a Metro style app.


know more: First look at Visual Studio 11 Express

Thursday, September 15, 2011

MDK-ARM Microcontroller Development Kit

The MDK-ARM is a complete software development environment for Cortex™-M, Cortex-R4, ARM7™ and ARM9™ processor-based devices. MDK-ARM is specifically designed for microcontroller applications, it is easy to learn and use, yet powerful enough for the most demanding embedded applications.



MDK-ARM is available in four editions: MDK-Lite, MDK-Basic, MDK-Standard, and MDK-Professional. All editions provide a complete C/C++ development environment and MDK-Professional includes extensive middleware libraries. MDK-Lite (32KB) Edition is available for download. It does not require a serial number or license key.

Details: http://www.keil.com/arm/mdk.asp



Wednesday, September 14, 2011

Download Windows Developer Preview and free developer tools

- Windows Developer Preview with developer tools English, 64-bit (x64)
  • 64-bit Windows Developer Preview
  • Windows SDK for Metro style apps
  • Microsoft Visual Studio 11 Express for Windows Developer Preview
  • Microsoft Expression Blend 5 Developer Preview
  • 28 Metro style apps including the BUILD Conference app



- Windows Developer Preview English, 64-bit (x64)



- Windows Developer Preview English, 32-bit (x86)



Details.

Tuesday, September 13, 2011

new data in Copy Constructor

Refer to the Problem of using default Copy Constructor, when Class new some data, we can provide our own copy constructor to solve the problem:

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass(const int mSize);
MyClass(MyClass& src);
~MyClass();
void fill(char c);
void getInfo();
private:
int size;
char* ptToChar;
};

//Constructor with default size
MyClass::MyClass(const int mSize = 10)
{
size = mSize;
ptToChar = new char[size];

}

//Copy Constructor
MyClass::MyClass(MyClass& src){
size = src.size;
ptToChar = new char[size];
for(int i = 0; i < size; i++)
ptToChar[i] = src.ptToChar[i];

}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << endl;
}

void MyClass::fill(char c){
for(int i = 0; i < size; i++)
ptToChar[i] = c;
}

void MyClass::getInfo()
{
cout << "size: " << size << endl;
for(int i = 0; i < size; i++)
cout << ptToChar[i];
cout << endl;
}

int main()
{
MyClass myClass1(20);
myClass1.fill('A');
myClass1.getInfo();

MyClass myClass2(myClass1); //Using our Copy Constructor
myClass2.getInfo();

myClass1.fill('B');
myClass1.getInfo();
myClass2.getInfo();
return 0;
}




new data in Copy Constructor

Problem of using default Copy Constructor, when Class new some data

In case of there are some data is created in a class using new, if we create another new object using compiler auto generated default copy constructor; the default copy constructor copy the pointer only, without create the new data. Both object have the same data, such that changing the data in one object will affect in the another object.

It's a example:
Problem of using default Copy Constructor, when Class new some data

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass(const int mSize);
~MyClass();
void fill(char c);
void getInfo();
private:
int size;
char* ptToChar;
};

//Constructor with default size
MyClass::MyClass(const int mSize = 10)
{
size = mSize;
ptToChar = new char[size];

}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << endl;
}

void MyClass::fill(char c){
for(int i = 0; i < size; i++)
ptToChar[i] = c;
}

void MyClass::getInfo()
{
cout << "size: " << size << endl;
for(int i = 0; i < size; i++)
cout << ptToChar[i];
cout << endl;
}

int main()
{
MyClass myClass1;
myClass1.fill('A');
myClass1.getInfo();

MyClass myClass2(myClass1); //Using default Copy Constructor
myClass2.getInfo();

/*
* Both myClass1.ptToChar and myClass2.ptToChar point to
* the same char[size], such that fill in myClass1 affect
* the array also!!!
*/
myClass1.fill('B');
myClass2.getInfo();
return 0;
}




we can provide our own copy constructor to solve the problem.

Constructor with initializer list

example:
Constructor with initializer list

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass();
~MyClass();
private:
string myName;
string myNote;
};

//Constructor with initializer list
MyClass::MyClass()
:myName("initName"), myNote("initNote")
{
cout << "MyClass Constructor: " << myName << endl;

}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << myName << endl;
}

int main()
{
MyClass myClass1;
return 0;
}



Compiler auto-generated Copy Constructor

If you don’t write your own copy constructor, the compiler generate a defaut one for you, to copy member variables one-by-one.

example:
Compiler auto-generated Copy Constructor

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass();
MyClass(string note, const string mName = "DEFAULT_NAME");
~MyClass();
private:
string myName;
string myNote;
};

MyClass::MyClass(){
myName = "default constructor without argument";
myNote = "";
cout << "MyClass Constructor: " << myName << endl;

}

MyClass::MyClass(string note, const string mName){
myName = mName;
myNote = note;
cout << "MyClass Constructor: " << myName << " : " << myNote << endl;
}


MyClass::~MyClass(){
cout << "MyClass Destructor: " << myName << endl;
}

int main()
{
MyClass myClass1;
MyClass myClass2("Using default Name");
MyClass myClass3("with name", "MyClass 3");
MyClass copyClass(myClass3);
return 0;
}



Copy Constructor

A copy constructor is one that takes a single argument of the same type as the class, passed by reference. Such that you can copy member variable from another object of the same class.

example:
Copy Constructor

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass();
MyClass(string note, const string mName = "DEFAULT_NAME");
MyClass(MyClass& src); //Copy Constructor
~MyClass();
private:
string myName;
string myNote;
};

MyClass::MyClass(){
myName = "default constructor without argument";
myNote = "";
cout << "MyClass Constructor: " << myName << endl;

}

//Copy Constructor
MyClass::MyClass(MyClass& src){
myName = src.myName;
myNote = src.myNote;
cout << "Copy Constructor: " << myName << " : " << myNote << endl;
}

MyClass::MyClass(string note, const string mName){
myName = mName;
myNote = note;
cout << "MyClass Constructor: " << myName << " : " << myNote << endl;
}


MyClass::~MyClass(){
cout << "MyClass Destructor: " << myName << endl;
}

int main()
{
MyClass myClass1;
MyClass myClass2("Using default Name");
MyClass myClass3("with name", "MyClass 3");
MyClass copyClass(myClass3);
return 0;
}




Related Post:
- Compiler auto-generated Copy Constructor


Constructor with default argument

example:
Constructor with default argument

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass();
MyClass(string note, const string mName = "DEFAULT_NAME");
~MyClass();
private:
string myName;
string myNote;
};

MyClass::MyClass(){
myName = "default constructor without argument";
myNote = "";
cout << "MyClass Constructor: " << myName << endl;

}

MyClass::MyClass(string note, const string mName){
myName = mName;
myNote = note;
cout << "MyClass Constructor: " << myName << " : " << myNote << endl;
}


MyClass::~MyClass(){
cout << "MyClass Destructor: " << myName << endl;
}

int main()
{
MyClass myClass1;
MyClass myClass2("Using default Name");
MyClass myClass3("with name", "MyClass 3");
return 0;
}



Sunday, September 11, 2011

Constructor and Destructor

example:

Constructor and Destructor

#include <iostream>

using namespace std;

class MyClass{
public:
MyClass(string mName);
~MyClass();
private:
string myName;
};

MyClass::MyClass(string mName){
myName = mName;
cout << "MyClass Constructor: " << myName << endl;
}

MyClass::~MyClass(){
cout << "MyClass Destructor: " << myName << endl;
}

int main()
{
cout << "Hello world!" << endl;
MyClass myClass("MyClass 1");
MyClass* myClassPtr = new MyClass("MyClass 2: created using new");
delete myClassPtr;
return 0;
}



Array arguments are passed to function by reference

example:
Array arguments are passed to function by reference

#include <iostream>

using namespace std;

void function(int ArrayPassed[], unsigned len);

int main()
{
int array[] = {1, 2, 3, 4, 5};
unsigned length = 5;

cout << endl << "before fnction()" << endl;
for(unsigned i = 0; i < length; i++){
cout << array[i] << " ";
}
function(array, length);

cout << endl << "after fnction()" << endl;
for(unsigned i = 0; i < length; i++){
cout << array[i] << " ";
}

cout << endl;
return 0;
}

void function(int ArrayPassed[], unsigned len){
for(unsigned i = 0; i < len; i++){
ArrayPassed[i] *= ArrayPassed[i];
}
}


Pass argument to function by value and by reference

example:

Pass argument to function by value and by reference

#include <iostream>

using namespace std;

void function(int byValue, int& byRef);

int main()
{
int a = 5;
int b = 10;
cout << endl << "before fnction()" << endl;
cout << "a (byValue) = " << a << endl;
cout << "b (byRef) = " << b << endl;
function(a, b);

cout << endl << "after fnction()" << endl;
cout << "a (byValue) = " << a << endl;
cout << "b (byRef) = " << b << endl;
return 0;
}

void function(int byValue, int& byRef){
byValue++;
byRef++;
}

break and continue

example:

break and continue
#include <iostream>

using namespace std;

int main()
{
cout << endl << "the loop break when i = 5" << endl;
for(int i = 1; i < 10; i ++){
if (i == 5) break;
cout << i << endl;
}

cout << endl << "the loop skip 5 with continue" << endl;
for(int i = 1; i < 10; i ++){
if (i == 5) continue;
cout << i << endl;
}
return 0;
}



Saturday, September 10, 2011

bool test ? true : false

bool test ? true : false


#include <iostream>

using namespace std;

int main()
{
cout << (true ? "It's True" : "It's False") << endl;
cout << (false ? "It's True" : "It's False") << endl;
return 0;
}




Friday, September 9, 2011

namespace

#include <iostream>

using namespace std;


namespace myspace {
int x;
string s;
}

namespace myspace2 {
string s2;
}

using namespace myspace2;

int main()
{
myspace::x = 5;
myspace::s = "Hello!";
cout << myspace::s << endl;

s2 = "Hello 2";
cout << s2 << endl;

return 0;
}



namespace

Thursday, September 8, 2011

References

#include <iostream>

using namespace std;

int main()
{
string name = "Eric L";
string& refName = name;
cout << refName << endl;

refName = "Changed!";
cout << refName << endl;
cout << name << endl;
return 0;
}




References

Test of string

Test of string

#include <iostream>

using namespace std;

int main()
{
string a = "Hello";
string b = "World!";
string c = a + b;
cout << c << endl;

cout << "c.size() = " << c.size() << endl;

for(unsigned i = 0; i < c.size(); i++){
cout << "c[" << i << "] : " << (char)c[i] << endl;
}

return 0;
}