Tuesday, September 13, 2011

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