Thursday, December 30, 2010

C++ Strings

Intro to strings this morning

#include iostream

using namespace std;

int main () {

char buffer[10];

cout << "Type some text and shit" << endl;
//hold space for the null character
cin.getline(buffer,9);

cout << "You typed : " << buffer << endl;

return 0;


}

Wednesday, December 29, 2010

C++ Dynamic Pointers

#include

using namespace std;

int main()
{
//star means its derefrenced
double *myDubPtr = new double;
*myDubPtr = 64;

cout << "*myDubPtr = " << *myDubPtr << endl;
cout << "memAdd = " << myDubPtr << endl;

//delete the memory in the heap, dont use the star, you want to delete the memory address allocation
delete myDubPtr;

return 0;
}

C++ Static Pointers

An example of how pointers work on static variables


#include

using namespace std;

int main()
{

int *ptr;
int x = 64;

//& is the memory address of the variable
ptr = &x;
cout << "ptr = " << ptr << endl;
cout << "x= " << x << endl;
cout << "&x = " << &x << endl;
cout << "*ptr = " << *ptr << endl;


return 0;
}

C++ Header files

header.h files have the function prototypes

#ifndef _SIMPLE_MATH_H_
#define _SIMPLE_MATH_H_


int myfunc1( int x , int y);
int myfunc2( int x , int y);
int myfunc3( int x , int y);
int myfunc4( int x , int y);

#endif

the correspoding header.cpp file contains the body of the functions, it is located in the header file folder

#include "simple_math.h"

int myfunc1( int x , int y) {
return x + y;

}
int myfunc2( int x , int y) {
return x - y;

}

int myfunc3( int x , int y) {
return x * y;

}
int myfunc4( int x , int y) {
return x / y;
}


and an examle of a main.cpp program calling the header files


#include iostream
#include "simple_math.h"

using namespace std;

int main() {

cout << "4+2 =" << myfunc1(4,2) < return 0;
}

Tuesday, December 28, 2010

C++ Functions

#include



int addNum(int x,int y) {
return x+y;
}


int main() {


int numOne;
int numTwo;
std::cout << "Enter Two numbers Please:"; std::cin >> numOne >> numTwo;

int sum = addNum(numOne,numTwo);
std::cout << "The sum of " << numOne << "And " << numTwo <<"is"<< sum << std::endl;

return 0;

}

Thursday, December 23, 2010

0054 Hand Studys

0053 Hand Construction Prac

C ++ Tutorial Loop Structures

#include

using namespace std;

int main () {

cout << "Hello, World! " << endl;

cout << "My For Loop " << endl;

for (int i = 0; i < 6; i++) {
cout << i;
if (i != 5)
cout << ",";
}
cout << endl;


cout << "My While Loop " << endl;

int i = 0;
while ( i < 6) {
cout << i;

if ( i != 5)
cout << ",";
i++;
}
cout << endl;

cout << " My Do-While Loop " << endl;

i = 0;
do {
cout << i;
if ( i != 5)
cout << ",";
i++;
} while ( i < 6);
cout << endl;


return 0;

}

C++ Tutorial

I'm learning C++ this holiday :D

#include

using namespace std;

int main(int argc, char * const argv[])
{

//variable block

int yourAge;
int myAge = 1000000;

//main block
cout << "Hello, World!\n";

cout << "What is your age: ";
cin >> yourAge;
if (yourAge > myAge) {
cout << "Your " << (yourAge-myAge) << " years older than me." << endl;
}
else {
if (yourAge == myAge) {
cout << " You are the same age as me" << endl;
}
else {
cout << "Your " << (myAge-yourAge) << " years younger than me." << endl;
}
}
return 0;
}


Tuesday, December 21, 2010