======C/C++ Programming Journal====== ====Week 1: Welcome to Programming ==== The first time I have entered into the world of Linux! It was definitely a confusing first day with the new OS and using the bash. I have some pre-existing knowledge of how the Command Line works in Microsoft, so now I just need to learn the syntax of the bash in Linux. The area of networking is very new to me. I was able to install Putty on my laptop and I am now able to network in to Lab 46 via ssh. I learned the purpose of a repository, to backup up copies of your files. I created an account on bitbucket and Mercurial. I can definitely see the importance in using these programs! **1st Program** -- __Hello World__ The purpose of this introductory program was to output text. ---- ====Week 2: The new OS,Linux and the Bash==== This week we learned about data types. I took the C For Engineers course and so I am familiar with integer types, floating types, and arrays, but the pointer types (memory variables) and structures (data type containing different types of variables) were new for me. Also, I learned that data types can be either signed (only positive numbers) or unsigned (positive/negative numbers). A huge advantage of using C is it's source code portability. All you need is the source code in order to move from one system to another (possibly some minor program changes) and the program will run. I am becoming more and more fluent with the bash command line. The chart below includes the most useful commands that I have learned so far. ^ ls ^ list of variable files and directories ^ ^ cd ^ change directory ^ ^ nano ^ opens an executable file ^ ^ man ^ manual/description ^ ^ help ^ self-explanatory ^ ^ /usr/include ^ directory including all header files with descriptions ^ ^ exit ^ self-explanatory ^ ^ pwd ^ shows current path ^ ^ gcc -o "output file name" "executable file name" ^ compile programs ^ ^ ./ "executable file name" ^ run the program ^ **2nd Program** - __Ranges__ The purpose of this program was to determine the ranges for the various data types. We learned about variable declaration and assignment. Also, we learned about substition with the %, such as the following line of code: printf("an unsigned char is %hhu bytes", sizeof(uc)); Memory allocated and size of each data type: A signed char is 1 byte lower bound is -128 upper bound is 127 An unsigned char is 1 byte lower bound is 0 upper bound is 255 A signed short int is 2 bytes lower bound is -32768 upper bound is 32767 An unsigned short int is 2 bytes lower bound is 0 upper bound is 65535 A signed int is 4 bytes lower bound is -2147483648 upper bound is 2147483647 An unsigned int is 4 bytes lower bound is 0 upper bound is 4294967295 A signed long int is 8 bytes lower bound is -9223372036854775808 upper bound is 9223372036854775807 An unsigned long int is 8 bytes lower bound is 0 upper bound is 18446744073709551615 A signed long long int is 8 bytes lower bound is -9223372036854775808 upper bound is 9223372036854775807 An unsigned long long int is 8 bytes lower bound is 0 upper bound is 18446744073709551615 ---- ====Week 3: Pointers==== I now have some understanding of how the heading for the main function works. When the input parameters for the main are "argc, char *****argv", this represents a special case that allows the program to communicate with the Operating System. The term argc is the argument counter and the term argv allows access to the arguments at the command line interpreter. The double stars indicate a pointer. int main(int argc, char *****argv) Pointers are memory variables that have a block of memory stored in a memory address. When a pointer is declared, a star is placed in front of the pointer identifier and the identifier is declared a data type such as int or char. Pointers are initially assigned to NULL, not zero because there is no memory address with this location! In the following line of code that was used in a program this week, the function malloc was used: name = (char *)malloc(sizeof(char)*20); The malloc function is used to allocate memory and it returns the raw memory. The number of bytes that you wish to allocate is inputed into the malloc function. In the code snippet above, the sizeof function is used to retrieve the size of a char, which we know to be 1 byte. The 1 byte of memory is then multiplied by twenty, so we know that twenty bytes is allocated. It is then casted into the character pointer format using the (char *) and assigned to the variable name. The basic format of the for() and if() loops were discussed. I also worked on a more complete list of Microsoft/Linux commands. The following link: [[http://bitbucket.org/joppenheim/cscs1320f13/wiki/Linux/Unix%20Commands|Microsoft/Linux commands]] **3rd Week Programs** - __printf/scanf__ A program was created to print questions to the bash using the printf function and then information was pulled back into the program using the scanf function. Another program was created to demonstrate the versatility and wide range of formatting available in the printf function. In this program, we used the argc command (which counts the number of arguments at the command line interpreter) and the printf function to print the number of arguments with right/left justification, indented by a set amount, or padded with zeros. Another program was created to print the number of arguments based on the argv function. ---- ====Week 4: Graphics!!==== We learned about a new graphics library called gd. This gd library can be used to create many types of images and drawings, anything from complex shapes to simple lines or circles. When using the gd library, make sure to include the gd.h file at the beginning of the program. Also remember to include -lgd at the end of the compile call, which tells the compiler where the library of definitions is located. After asking Mr.Oppenheim, I now understand the Hexidecimal number system. I was somewhat familiar with the binary number system, and the logic behind the hexidecimal number system is exactly the same. There are 16 "symbols" (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F) and the base is 16, of course. In addition, we also spent more time learning/practicing the printf statements. The following is what I want to conclude from my learnings of the printf statement: A printf statement allows c string to be written to a standard output. Format specifiers can be used to replace values specified in additional arguments. A printf statement has the following format : %[flags][width][.precision][length]specifier Flags: ^ - ^ Left-justify within the given field width. Right justification is the default ^ ^ + ^ Forces the plus sign to be printed ^ ^ (space) ^ Forces a space to be printed ^ ^ 0 ^ Left pads the number with ^ The width is the number of characters to be printed and when the width is shorter than the number, the result is padded with blank spaces. .precision specifies the minimum number of digits to be printed. h = "half it" l = "double it" Length specifiers: ^ (none) ^ int, double ^ ^ hh ^ char ^ ^ h ^ short int ^ ^ l ^ long int ^ ^ ll ^ long long int ^ Specifiers: ^ d or i ^ Signed decimal integer ^ ^ u ^ Unsigned decimal integer ^ ^ c ^ Character ^ ^ s ^ String of characters ^ [[http://www.cplusplus.com/reference/cstdio/printf/|Printf Source]] {{ :opus:fall2013:jwebb4:checkerboard.png?200|}} **4rth Week Programs** - __shapes__ The labs this week involved printing more output to either a web page or the command line interpreter. In one lab, the goal was to experiment with more printf formatting, much of what is explained above. Another lab involved creating an image that looked like a checker board (shown to the right)with the 17th square being green. In order to efficiently create this pattern, a for loop embedded inside of another for loop was used. Squares were created using the gd library. {{ :opus:fall2013:jwebb4:house_and_snowman.png?200|}} The last lab of the week was to create a picture of a house, snowman, and the CCC logo. This involved more shapes used from the gd library. Another new concept was the ability to print to a web page: FILE * out; char outfile[] = "/home/jwebb4/public_html/image1.png"; The first line creates a file and the second line contains the location of where the file is to be stored. ---- ====Week 5: Arrays==== An array is a homogeneous composite variable modifier type, or in english, an array is a data type that can hold one or more variables that are of the same type. I picture an array like a box that has several compartments. The value inside of a "compartment" in an array can be accessed by //arrayname//[location]. Remember that an array's index begins at 0, so the 4rth value will be //arrayname//[3]. The question was asked "What is the difference between arrays and pointers". They have essentially the same purpose,to store data in an address. A pointer and an array can both be used to accomplish the goal. The following each do the exact same thing: char **b //Obtain address then access address using pointers char *b[] //Point to address then obtain value based on index of array char b[][] //access based on index of array When declaring and initializing a pointer, such as int *d; d = 24; //Address or location that you are pointing to d = 13; //Value at the address **5th Week Lab** - __Pointer/Arrays__ {{ :opus:fall2013:jwebb4:polygons.png?200|}} printf("%d, %c\n", *b, **b); //*b points to address while **b accesses what is in this address The following lines of code show that arrays and pointers can be used to accomplish the same thing int main (int a, char **b) //Two ways of printing the first token, first character printf("%d, %c\n", **b, **b); printf("%d, %c/n", *b[0], *b[0]); //Two ways of printing the second token, third character printf("%d, %c\n", b[1][2], b[1][2]); printf("%d, %c\n", *(*(b+1)+2), *(*(b+1)+2)); //third token,fourth character printf("%c\n", b[2][3]); Also, b++; //moves to next token (*b)++ //moves to next character in token === Pointer Example: === ^ Address ^ Data ^ ^ 20 ^ 22 ^ ^ 21 ^ 23 ^ ^ 22 ^ 24 ^ ^ 23 ^ 29 ^ ^ 24 ^ a ^ ^ 25 ^ b ^ ^ 26 ^ c ^ ^ 27 ^ d ^ ^ 28 ^ e ^ ^ 29 ^ f ^ ^ 30 ^ g ^ ^ 31 ^ h ^ ^ 32 ^ i ^ ^ 33 ^ j ^ Assuming that address b = 20, *b will be equal to 22, the value inside 20. **b is digging down one address deeper and would be equal to 24, since the value of 22, from address 20, points to the address of 22 which is 24. ***b = a, because it is using the address of the previous value 24, which is 'a'. ---- ====Week 6: Functions==== Functions are sub-programs that allows for repackaging of code. Functions require a prototype, function call, and the function definition itself. Syntax: Function Prototype: (parameter types) //Identifiers are not needed with the parameters Function Call: (input identifiers) //The function name and the number of input identifiers must match the prototype Function Header: (parameter types and identifiers) //Everything but the parameter identifiers must match up with the Prototype and Call __Function example:__ #include int myfunction(int,int);//Function prototype(aka declaration) void main() { int x,y; myfunction(x,y);//Function call } int myfunction(int top, int bottom)//Function definition { top++; int result = top + bottom; return(result); } In my previous C for Engineers course, we covered functions but did not get to pointers. The only way we could return more than one value from function to function was if we used an array to store multiple values and then returned the array. We learned this week that we could use pointers and pass by address. Which now that I think about it, whether we use an array or a pointer, we are still passing by address. Say for example you wanted to save the modified value of the variable top, but still return the variable result. You could make top be a pointer. Using the previous example: #include int myfunction(int *,int);//Function prototype(aka declaration) void main() { int x,y; myfunction(&x,y);//Function call } int myfunction(int *top, int bottom)//Function definition { top++; int result = top + bottom; return(result); } Side Topic: We used the << and >> to manipulate the binary representation of numbers. It is important to remember that data can be lost with the logical shifts! Other Operators: & - bitwise AND | - bitwise OR ~ - bitwise NOT ^ - bitwise XOR << logical left shift >> logical right shift Below is a truth table for the basic logic gates. I was confusing the logic gates so I felt the need to further research the gates. ^ A ^ B ^ AND ^ NAND ^ OR ^ NOR ^ EXOR ^ EXNOR ^ ^ 0 ^ 0 ^ 0 ^ 1 ^ 0 ^ 1 ^ 0 ^ 1 ^ ^ 0 ^ 1 ^ 0 ^ 1 ^ 1 ^ 0 ^ 1 ^ 0 ^ ^ 1 ^ 0 ^ 0 ^ 1 ^ 1 ^ 0 ^ 1 ^ 0 ^ ^ 1 ^ 1 ^ 1 ^ 0 ^ 1 ^ 0 ^ 0 ^ 1 ^ Random Note: There are 8 bits in 1 byte **6th Week Lab** - __Pointer/Arrays__ The following program was created in class to demonstrate the difference between the Ascii symbol representation and the Ascii integer representation. Ascii symbols and their equivalent integer values have patterns that can be handy when printing text. //Converts between Ascii symbol and equivalent integer value # include int main() { char x =0; char y =0; printf("Enter a number (0-255); "); scanf("%hhu", &x); getchar();//function that grabs single input char of 1 byte from input stream //So it grabs the enter character and get's rid of it, y=getchar() is equivalent printf("Enter a character: "); scanf("%c", &y);//Allows changes to be saved printf("x is numerically %hhu and characterally %c\n", x,x);//%hhu is int specifier, %c is char specifier printf("y is numerically %hhu and characterally %c\n", y,y); printf("x+48 characterally is %c\n",(x+48));//48 begins askii 0, so number entered will be printed printf("y+32 characterally is %c\n",(y+32));//32 added and subtracted will switch character to uppercase and lowercase return(0); } Important concepts from lab: char*a; //Memory is initially allocated for a char pointer *a = 49; //The value stored in a is 49 b = printf("%c", *a); //Prints the value stored in a, 49 b = printf("%c", a); //Prints the address of a If an array pointer is declared, MEMORY MUST BE ALLOCATED! char *numbers[12]; //Memory must be allocated for array pointers *numbers[0] = 49; //The value stored at index one, 49 b = printf("%d", *numbers[0]); //Prints the value at index one of numbers array, 49 b = printf("%d", numbers[0]); //Prints the address of index one of numbers array Difference between two lines of code: char (*numbers)[12] //Pointer to char value and have an array of pointers char*(numbers[12]) //Points to the array ---- ====Week 7: Knowledge Assessment ==== In lecture we reviewed for and then took the knowledge Assessment. We covered the use of an alias such as typedef. When the keyword typedef is used and followed by the variable type and the variable name, only the variable name is needed later. Here is an example: typedef unsigned long long int ulli; ulli x,y; //typedef hides the unsigned long long int variable type which makes code neater I learned an important lesson this week as I was finishing up my Knowledge Assessment when the system crashed, ALWAYS SAVE!!!!!!!!!!! In lab we worked on a program which can call a header file that was created and using this header file add numbers. The created header file contains several functions such as and(), or(), and not() which will represent the logic gates needed to do the arithmetic. The and, or, and not logic gates can be used to create all other logic gates. ---- ====Week 8: Object Oriented Programming ==== An array is a homogeneous composite type while a structure is a heterogeneous composite type. Basically, a struct can contain mixed and the same data types. The main difference between classes and structures is that classes can have functions and executable code while structures can not have executable code within. Example: //Declare struct struct thing{ int a; char b[20]; float c; int d; short int e; }; struct thing stuff;//Declare an instance of thing //Access the variables in struct thing stuff.a = 12; stuff.b[3] ='A'; It is important to remember the semicolon at the end of the struct declaration. However you can have a pointer to a function in a struct. Such as: struct thing{ int (*sum)(int, int, int); }; int all(int, int, int); sum = &all; sum(1,2,3); We learned about creating a library of function declarations in a .c file, then along with the header file (containing the function prototypes), we can include the header file and use the functions in our programs. **Object Orientated Programming** We have crossed over from procedural to Object Orientated programming this week!! The important thing to remember is that 99% of valid C code is valid in C++. When compiling C++ code, the command is now g++ and the C++ file extension is .cc, .C, .cpp, or .c++. In C++, it is preferred that the header files start with a c and the .h is omitted. The new concept of C++ is classes and objects. A class is the thing being defined or definition while an object is an instance of the thing being defined, the class. Such as: int a; //class = int and a = object In the following code, the C++ file, the header file, and the program are all written in the same file. The animal class has methods such as listen and tell, which are it's objects. Members are methods or variables. #include //Header File////////////////////////////////////////////////////////////////////// class animal { public://resources shared publicly, public is an attribute //Now accessor methods void listen(); void tell(char *); //Accessor methods-general use //Overloaded functions-same function name but different parameters animal();//constructor-"class name"-let u run code upon initiation animal(char *); private://resources only accessible witin the class char sound[80]; }; //////////////////////////////////////////////////////////////////////////////////// //C++ File////////////////////////////////////////////////////////////////////////// void animal :: listen()//listen is a member of animal, using the :: { printf("%s\n", sound); } void animal :: tell(char *sound)//tell is member of class animal { int i = 0; while(*(sound+i)!= '\0') { //structure pointer-> this->sound[i] = sound[i]; //when u want to specify the local class variable, pointer to itself (animal class) //this -> this sound and not the sound from class i++; } this -> sound[i]='\0'; } animal::animal() { printf("A\n"); } animal::animal(char *sound) { printf("B\n"); tell(sound); } //////////////////////////////////////////////////////////////////////////////////////// //Program/////////////////////////////////////////////////////////////////////////////// int main() { animal cat;//new instance of animal class called cat animal *dog= new animal("woof");//creates new variable dog pointer to animal, string of woof into the dog object cat.tell("meow"); cat.listen(); dog -> listen(); return(0); } /////////////////////////////////////////////////////////////////////////////////////////// The output is: A B meow woof So how does everything work? The program begins by creating an instance of animal called cat. By doing so, the constructor that does not take any parameters is called and an A is printed. The next line of code creates an instance of animal, the dog pointer. Whenever you initiate a new pointer object, you must have the new keyword. By initiating the dog class, the constructor that takes parameters is called. The letter B is printed and the tell function is called, passing the char* sound variable, which has the string "woof". In the tell function, the *(sound+i) is dereferencing the values in the sound pointer and an equivalent command would be sound[i]. The purpose of the while statement is that it is going through each letter of the sound local variable, and it is setting the sound member variable, using this, equal the to the local variable. So the member variable now equals the string "woof" and the null character is placed after the string. Now back to the next line in the program. We are accessing the tell function in the cat class and sending in the string meow, which the function places the string meow into the sound member variable. Next line, the listen function executes and prints the value of the sound member variable in cat, which is meow. The listen function is now executed in the dog pointer class, and the sound member variable, which was initialized during the constructor statement, is now printed. The program is finished. It is important to remember that when we created the two new instances of the animal class, dog and cat, they can be thought of as two separate entities or containers that began with exactly the same contents. However, the variables in each were modified throughout the code. You will notice that the constructors do not have a return value. If you choose to include the constructors, as soon as you instantiate a type, the constructor function is automatically ran. As seen in the above code, there are two types of accessors, based on the data types . is used to access data types other than pointers -> is used to access pointers to classes and structures ---- ====Week 9: C++ ==== We created more programs in C++ this week. An important note which I forget to mention last week is that the last brace on a class is followed by a semicolon. Also, when including your own files use "", such as "myheader" When using standard libraries, use <>, such as The #ifndef and #define preprocessor statements basically say, if they could talk, "I already have one copy of the file so disregard the second copy". Only the first person to have the file get's it, which prevents possible errors in the future. We created a simple area program which would calculate the area and perimeter of a square, rectangle, and triangle. The first time around creating this program, I thought I would just create three header files for each of the shapes and include the function prototypes. Well I forget that the shapes class was left blank and I had error galore. Not sure if it even is possible to include function prototypes in each object header file,outside of the class, because the function prototypes must be declared in the class. Anyway, so I ended up creating one shapes class header file, a C++ file containing the function definitions, and the program and SUCCESS!! Note: I learned the hard way that the following statement only assigns base to equal 10, not what I meant by the statement. Must do in separate lines. int side, height, base = 10; The shapes.h header file class shapes { public: int side, base, height; int area; int perimeter; shapes(); int sarea(); int sperimeter(); int rarea(); int rperimeter(); int tarea(); int tperimeter(); }; * The program int main() { shapes square; shapes rectangle; shapes triangle; printf("The area of the square is %d\n"", square.sarea()); printf("The perimeter of the square is %d\n"", square.sperimeter()); printf("The area of the rectangle is %d\n"", rectangle.rarea()); printf("The perimeter of the rectangle is %d\n", rectangle.rperimeter()); printf("The area of the equilateral triangle is %d\n", triangle.tarea()); printf("The perimeter of the equilateral triangle is %d\n", triangle.tperimeter()); return(0); } This program demonstrates the important power of C++ with having multiple objects, a square, rectangle, and triangle. While this simple program could have been achieved in one file, some projects become sufficiently large, where organization with different header files and classes is important. The program outputs: The area of the square is 25 The perimeter of the square is 20 The area of the rectangle is 25 The perimeter of the rectangle is 20 The area of the equilateral triangle is 12 The perimeter of the equilateral triangle is 15 ---- ====Week 10: Computer Simulator and Big Numbers ==== The idea behind our logic program is to simulate the processes of a computer. Well, we need to know how a computer works before we can make a program that is going to simulate a computer. So we learned the basics of how computers work. Basic explanation of computer processes: Input -> Process -> Output This Process step involves the CPU, which has the main tasks of Fetch -> Decode -> Execute -> Get Next Instruction In the Fetch process, the brains of the computer (CPU) is entered, Information is obtained(Information = data + meaning), and instruction is included with the information. Now sometimes you may want to travel to a small memory bank, known as a cache, because it is much faster. The CPU contains both caches and registers, which store small amounts of data. There is RAM also, which is volatile memory, meaning that as soon as power is lost the memory is gone. So when you turn on your computer, the CPU starts with non-volatile memory with instructions to get things started (usually the BIOS). The Decode process is basically getting outputs form inputs. Such an example would be a 2-4 decoder, meaning 2 inputs and 4 outputs using logic gates. The Execute process basically involves the control flow of input to output. Using an and gate, one input can always be in the preferred state so depending on the input of the other state, the output will depend on the one input. The clock in a computer drives the speed of the following steps. So far in our logic program, we have created logic gates, functions necessary to perform arithmetic processes, and just recently a latch. The purpose of a latch is to remember the states of function and data members. Otherwise, the state of a function is lost once it is exited. NOTE: When you do not specify the access specifiers, the public specifier is automatically assumed. The other project we are currently working on is the BigNumProgram. The program will allow us to create a big number by allowing us to change the sign, length, and base of the number. We spent the latter part of the week creating an increment function. Next, using increment/decrement functions, we will be able to add and subtract our big numbers. ---- ====Week 11: Debugging==== Up to now, we have used two techniques in fixing problamatic code: - Work through code (look at it) - strategic printf statements to see if areas of code are executing properly However, sometimes it can be very difficult to find errors in more complex levels of code. So, our third option is to use the GNU Debugger Program A debugger allows programmers to interupt execution of code and examine every nook and nanny of their code. In order to use the debugger, we need to compile the code with a -g. The -Wall command is not necessary for debugging, but the command turns all warnings into errors. Next, we need to run the executable code using the gdb command. So the commands for debugging a program would look like: gcc -Wall -o amazing amazing.c -g gdb ./amazing Now that we are in the debugger, there are sorts of commands available. The most used commands: * list - shows current location in code * break - to set a breakpoint * step - will take you to the next instruction * next - will take you to the next line of code, will execute but skip over functions * run - will run the program to the end or take you to the next breakpoint, if there is one * print - print anything to screen, such as values in variables or addresses * display - can select certain items, such as variables, and have them displayed each time the loop is executed * set - assign a value to a variable * exit - to leave When using the step command, make sure to only step into code that you have written. The next step in our logic program was to implement a clock or gate. This gate is created by placing nand gates at the inputs of the RSnand latch. Combining the two gates in the configuration below is called an JK Flip Flop. {{ :opus:fall2013:jwebb4:img_0130_1_.jpg?300 |}} In the JK Flip Flop, everytime the state changes from unpreferred to preferred, the gate switches every other time. In doing so, a 3 bit counter is created! ---- ====Week 12: Inheritance==== Inheritance is a very useful technique in programming. It allows for code transparency and it allows code to be reused. In using inheritance, the attributes of the parent class can be passed down to a child or derived class. So say we have the following class called fruitClass, class parentClass{ public: int apples(); int pears(); protected: int oranges(); private: int grapes(); }; The protected attribute is used in inheritance If we wanted another class that could inherit the attributes from the fruit class, the syntax follows, class foodClass: public fruitsClass{ //The methods from the parent class have been inherited, we can specify more methods public: int meat(); int vegetables(); }; The public attribute specifies the level of access control from the parent class. So all public and protected attributes are copied to the food class and classified as public. PRIVATE ATTRIBUTES CANNOT BE INHERITED FROM PARENT TO CHILD CLASS!! Just remember, "only parents can access their own private parts". Other access control attribute possibilities, class foodClass: protected fruitsClass{//all public and protected attributes are copied to the food class and classified as protected }; class foodClass: private fruitsClass{//all public and protected attributes are copied to the food class and classified as private }; Who can access what specifiers: Private Attributes -> parent class Protected & Public Attributes -> derived class Public Attribute -> World So that's inheritance! Currently in our Bignum program: I just finished creating my decrement method, so now using my increment and decrement methods, I can add and subtract numbers by looping these methods. I am currently working on a method that allows me to use methods with various bases of numbers. My current plan is to do some number manipulation and use the ascii table to convert between the different bases. Below are the files from my BigNum program: Header file #ifndef _BIGNUM_H #define _BIGNUM_H class BigNum { public: BigNum(); BigNum(unsigned char);//Up to 255 size in length unsigned char getBase(); unsigned char getLength(); unsigned char getSign(); void setBase(unsigned char); void setLength(unsigned char); void setSign(unsigned char); void zero(); void print(); void increment(); void decrement(); private: unsigned char base; unsigned char length; unsigned char sign; unsigned char *data;//bignum stored }; #endif It is a common practice to assign private variables which can only be accessed and modified within the class, and to create public methods which are used to retrieve information from the variables. File containing constructors and increment/decrement methods. #include #include #include "bignum.h" BigNum::BigNum()//If we give it nothing { length = 8; base = 10; sign = 0; data = (unsigned char*)malloc(sizeof(char)*length); //new is the C++ malloc, takes care of allocating memory for objects //new region of memory 8X char, so 8 times character array this -> zero(); } BigNum::BigNum(unsigned char length) { this -> length = length; base = 10; sign = 0; data = (unsigned char*)malloc(sizeof(char)*this -> length); this -> zero(); } void BigNum::increment()//Adds one each time function called { int i=0; char carry=1; char result; char sum; for(i=0;i 9) && ((sum%10)==0)) { carry = 1; sum = 0; } else carry = 0; *(data+(length-1)-i) = sum; } } void BigNum::decrement()//Subtracts one each time function called { int i=0; char carry=1; char result; char difference; for(i=0;i<(length-1);i++) { result = *(data+(length-1)-i); difference = result - carry; if (result<=0&& carry ==1) { carry = 1; difference = 9; } else carry = 0; *(data+(length-1)-i) = difference; } } Method which sets the BigNum initially to zero #include "bignum.h" void BigNum::zero() { for(int i=0; i Method which sets the sign of BigNum #include "bignum.h" unsigned char BigNum::getSign() { return(sign); } void BigNum::setSign(unsigned char sign) { this -> sign = sign; } We will use method to set the base of the bignnum #include "bignum.h" unsigned char BigNum::getBase() { return(base);//access private method data } void BigNum::setBase(unsigned char base) { this -> base = base;//class base similar to method base } Sets the length of our BigNum to the length of the char data type defined within the class unsigned char BigNum::getLength() { return(length); } void BigNum::setLength(unsigned char length) { this -> length = length; } We even created our own method to print our BigNum void BigNum::print() { int i; for(i=0; i MakeFiles: We also learned about MakeFiles and how they can automate tasks. They are written in a scripting language using the Make program. Instead of inserting all of the executable files and running the main program in the bash (which can be tedious for huge projects!), the keyword "make" is typed and magic happens, all of those previously needed bash commands are executed. Of course, it is not that easy... the MakeFiles can be complicated with a scripting style syntax. However, the great thing is that MakeFiles can be reused as long as you have some idea of how the MakeFiles work! Also, MakeFiles can incorporate compiler optimization, which simplifies the code and makes it more efficient by getting rid of unnecessary lines of code. ---- ====Week 13: Polymorphism==== C - we can't have functions with the same name C++ - we can have function overloading (two functions with same name, but different parameters) and then there is ... Polymorphism, which allows us to have two functions with the same name and parameters. Polymorphism allows for 2 different functions with the same signature. The virtual keyword is used and it allows for function overriding and "late binding". In the "late binding" process, there are a bunch of candidates and one will be called and determined at run time. Polymorphism example: class account { public: virtual int withdraw(); }; class checking: public account { public: virtual int withdraw();//will not be the same }; The End Of Course Experience was handed out this week. ---- ====Week 14: No classes==== THANKSGIVING BREAK! I am almost finished with the End Of Course Experience! ---- ====Week 15: EOCE==== I present to you, Whiteout! {{ :opus:fall2013:jwebb4:whiteout.png? |}}