In organizing the concepts of C++ which are being introduced we can view each of them separately, but studying them it is essential to keep in mind that they are designed to function together in the construction of programs which are easier to debug, which allow for cleaner overall syntax, and which encourage creation of reusable code. We shall explore: Data Types (build in and user created) Pointers and References (and their subtle deference) Manual Memory Allocation and the 'new' and delete keywords Class Declaration Class Definition Private data Public data Object instantiation and access, Class typedef Class Constructors Class Destructor Function or Method Overloading Operator Overloading External extension of definitions Copy Constructors at the end we'll try to actually make the example class, which is an extended array. 1) Data Types: DATA DATA DATA, everything is DATA As we know, C and C++ have built in data type which each variable and object the language needs to be defined as. Both C and C++ are typed language. We have char, int, double, float, pointer, and so on, a complete list of which is available around the net. A pointer stores an address of another C++ object. Different hardware and software platforms also have different sizes that it allocates for these data types, creating an inconsistency which, as an aside, the Free Software community has tried to address with the creation of the glib library, part of the GTK project. What is less apparent is that with regard to a computer, everything is data. We have the data that we create and manipulated with instructions, but the instructions themselves are a form data which we can package up and save for later use, and feed to the CPU at will. When the CPU runs out instructions the resulting actions are further instructions that can be packaged and saved as data. In a word, everything is data, and how we package that data is what separates one programming language from the next. This concept is covered extensively in NYLXS “Introduction Programming with Perl” class and since this is an advanced topic, we won't get much further into this. But we will review the basics of data types and then look at the new feature that C++ gives which C didn't have in such a generous way, the ability to easily create new data types easily and in a reusable fashion. An integer in the C family on the 32 bit Intel clone architecture is defined as a marked space in memory of 32 bits in size to represent both positive and negitive numbers. On the new 64 bit architecture that many of you might have, I don't know if this still holds true since the word size of a those machines is 64 bits or 8 bytes. when you use the declaration int a = 3456; The computer your program sets aside 32 bits, 4 bytes, of space in ram and puts the binary representation of that value in that space. The leftmost bit is usually the signed bit determining whether the number represented within is either positive or negative. A signed integer can therefor have a maximum value of 2,147,483,647 positive or negative. beyond that you must use a long int, which on 32 bit architecture actually won't help you, or to use external libraries with other data types defined. By default, C allows certain syntax with a data type. It will automatically translate it, for example, into a char that will print its representation for functions such as printf or in C++ the cout object: printf (“%d\n”, myint); cout << myint; It can be combined with operators it can be used with the assignment operator to fill or initialize its space with data. int myans,myx = 6,myy =12; It can be combined with arithmetic operators and have results assigned accordingly. myans = myy + myx; Two of them can be compared. while (myy < myx){ ... ... } They can be auto incremented myy++; ++myy; and so on.... One data type is actually a serial arraignment of data, that is an array. int myarray[100]; This defines an array of 100 elements indexed from zero to ninety-nine. myy = myarray[4]; assigns the fifth element of our array to our integer variable myy. One thing you can not do with an array data type is use an assignment operator on the entire array object. myarray[] = myarray2[]; //THIS IS AN ERROR C++ allows up to define our own data types that have all the properties of the built in ones. It uses the class mechanize, operator overloading, and the “new” keyword to accomplish this. 2) Pointers and References – Where did I PUT THAT! When we create data for our program, we ask the program to insert memory into RAM and to retrieve or assign the data from that memory location for use. Internally the program keeps track of the symbols and the memory locations. In fact is you run the program “nm” on a C or C++ binary it will tell you all the symbols that it has in that binary. ruben@www2:~/cplus> nm file3|less 0804a210 A __bss_start 08048a84 t call_gmon_start 0804a29c b completed.1 0804a0bc d __CTOR_END__ 0804a0b4 d __CTOR_LIST__ U __cxa_atexit@@GLIBC_2.1.3 0804a204 D __data_start 0804a204 W data_start 08048ed0 t __do_global_ctors_aux 08048ab0 t __do_global_dtors_aux 0804a208 D __dso_handle ...... But we can also create memory locations that are assignable, and store a representation of that memory location directly into a variable that only stores the memory location as data, not the data itself. In c and C++ this is called pointers and we can use the following syntax to create them. int *pt = &myint; This declares the pointer to an int variable called pt which stores the address for myint. The syntax int * in a declaration (and ONLY in a declaration) says make a pointer to an int. The & syntax in front of a variable myint says don't return the value of the variable, but return the address of the data stored in the variable itself. There are functions that return only pointer data. Those functions make it possible to access memory without the declaration of variables at all. There are also declarations that can be made in C and C++ which can create variables without variable names either. int (*pt)[10]; This declares a pointer (pt) to an array of 10 integers. char (*p)[10][100] This is a pointer which addresses an array of 10 pointers (implied) to arrays of 100 chars each. Commonly this is know as a point to an array of 10 strings. The symbolic variable name for an array often gets automatically cast as a point type. Most string functions in C return a char pointer for example char * strtok(char *s1, const char *s2); This would return an address of a char, which in theory would represent an array of chars. In use it would look like this char * spt; char wd1[100] = 'hello world', wd2[100] = ' '; spt = strtok(wd1,wd2); printf(“%s\n”, spt); Manual Memory Allocation and the 'new' and delete keywords C++ makes it very convent to create dynamically allocated memory which is accessed by pointers. We might call these anonymous pointers because they do not point to any variables, just defined memory. We do this with the key word “new”. int *pt = new int(124); This creates a new int pointer called pt and assigns to the memory pointed to by pt with the integer value 124. delete pt; deletes the anonymous pointer pt. int *pt = new int[100]; This declaration creates a new int pointer to an array of 100 integers, with no data assigned yet to that block of memory. delete [] pt; deletes the entire array pointed to by pt and then undefines pt. Ruben -- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 "Yeah - I write Free Software...so SUE ME" "The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society." "> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one." © Copyright for the Digital Millennium