|
|||
|
I'm considering working the C++ program in the following manner. I want
this to be a serious endeavor with commitment to learning. I hope to initiate the first phase over 3 months. A full blown exploration of the standard C++ language to include syntax, namespace, the standard libraries, compilation, datatypes, memory management, Class structure and Object design, scope, and a run down of the standard libraries, based largely of the classic text, C++ Primer Stanley Lippman and Josee Lajoie, The C++ Programming Language by Bjarne Stroustrup, as well as some C programming as needed for supplementation, from Kings, C Programming: A Modern Approach. Other books are welcome for additional materials as needed as C++ texts tend to be wonderfully short on clarity on specific language particulars. I'm expecting to put out lecture size workshops 2 times a week, maybe Mondays and Thursdays. They will be posted to the NYLUG mailing list, NYLXS mailing list (hangout), to comp.lang.C++. I'll try to cross post to all three mediums, and I'll be putting up edited versions of everything at http://www.nylxs.com/docs/workshops/ . Sunday nights I hope to meet with everyone on line on IRC on Freenode, #nylug during normal Sunday night NYLXS technite events. Finally, I hope that we can meet every two weeks in order to work together on code and exercises...although admittedly, getting that done once a month might be a more doable goal. Times and Places we will discuss and work out. We might be able to dove tail this with the NYLUG workshop if they don't view that as an invasion. As a roadmap, after the first three months, I hope to move into studying serious Unix based systems libraries, such as networking libraries, file system libraries, unix sockets, and all that wonderful stuff many of us know nothing about. Then I hope to spend a 6 months on the Linux Kernel and Device drivers. I know that the Kernel is largely C and not C++ but I think we can still pull this off, and after about 6 months of this, I hope to move on to X11 libraries, and application building. Perhaps we can touch GTK libraries, QT libraries after the core X11 set is explored. Its a lot of work and it takes a serous commitment. I tried to do this once before and it failed on its face. So I'm hoping we can start this up again with more success. In my mind, it is very easy to get involved in one of the advanced scripting languages in order to have fun, and even make a few bucks, but everything always comes back to C and C++ for the kind of advanced work and employment that separates the unemployed from the well employed in times of recession. Obviously, we will be using GCC, Make, Autoconf, and Tags and personally I'll be using VIM, and I'll discuss it much at the beginning. No editor wars. Use whatever editor you enjoy. I don't care. Just please don't distract the program with a parallel track of study. So who is still game? This kind of study could cost someone 10 grand at NYU and you might never learn as much. Ruben |
|
|
||||
|
||||
|
|
|
|||
|
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 |
|
|||
|
C++ Syntax Before exploring how we actually represent data in our C++ programs, I want to introduce a formal discussion of basic C++ syntax, which, like data types, doesn't get enough attention in most standardized texts. All programming languages require syntax rules in order for the compilers, to parse and create working machine code. These syntax rules require basic understanding of core components. These components include files, structure, statements, data and operators. Starting from the top, first you have files, and files usually have naming rules. C++ inherits from C nearly all the file structures, and actually requires a greater knowledge in more detail and at an earlier level of expertise. C++, because of its object oriented design depends heavely on library creation. In fact, even for beginners, most of your work happens on the library level. All C programs inherit from the Unix enviroment, which co-developed with C, the need for an initiation of the main function definition. All C programs start with main, and main will then absorb and use all other parts of the systems libraries and programming to produce your completed program. Main is located in your upper most programming file. Standard Programming Files: A standard programming file is when the top most programming will take place. In the C language, most of your code, espeically as a beginner takes place in this file. Most commonly these files have a suffix of either .cc oro .C. file.C for example is a standard C++ File name. A standard programming file will have several components: 1) Include Prepocessor Directives - These import header files and define the definitions of the symbols which your not spontaneously creating, that your program will use. And include directive might look like this: #include <iostream> Which tells the compiler to load up the defintions of all the functions and objects defined in the iostream library. Standard C++ libraries are included using the angle blacket notions as above.They are search for by your compiler in a set of standard locations which are defined by your compiler and programming enviroment (something I wouldn't mind understanding better on modern Linux and GNU enviroments). If you use the syntax #include "myheader" with double quotes, the compiler will look for these headers in the local directory. C libraries are accessable in C++ and can either have a standard C language notion #include <assert.h> #include <stdio.h> ***Note the .h suffix being included*** or use the C++ version #include <casset> #include <cstdio> 2) Macro and other Prepocessor Compiler Directives - Help set up conditions in which libraries and header files are brought into your program to help prevent duplication and to create different versions of a program as might be needed for differing architecture or conditions. The list of Preprocessor Directives are as follows: #define #endif #ifdef #ifndef #include (as discussed above) A Macro directive might look like this: #ifndef HEAD #define HEAD #include <iostream> #include <string> #endif Development of skills using these directives, which is a language in a language,is one of the skills that advanced C and C++ coders have that separate them from amateurs. This Macro is telling the compiler to include the libraries and symbols for iostream and string from the core C++ library if and ONLY IF, the symbol HEAD, in the compiler instructions, haven't been already defined. There are also constants that your program has which the compiler adds to your code which include __cplusplus __DATE__ __FILE__ __LINE__ __STDC__ __TIME__ __DATE__ and __TIME__ are the date and time the program is compiled. 3) Original Code and runtime directives starting with main. C++ has added a new programming directive called the "using" directive which is used to create namespace. Namespace gives a finer grain control of which symbols your code recognizes in a specified space. Its really important and in many ways was a long time coming to the C family of languages. Most importantly it prevents you from accidentally stepping on library symbols or words that you might not have been aware of or that programmers after you might not be aware of. It also allows to define the same symbol in multiple locations of your code without stepping on your own toes. So todays modern C++ main program files might look something look something like this: #ifndef TOP_H #include <iostream> #define TOP_H #endif #ifndef INTARRAY_H #include "intarray.h" #define INTARRAY_H #endif using namespace std; int main( int argc, const char* argv[] ) { //YOUR PROGRAMMIGN CODE } There is a catch to the namespace usage though. It might very well be that your library files, especially if you are creating them yourself, which you will in C++, have the using directive. If so, you will likely depend on them. Header Files: Header files normally have a .h suffix. file1.h would be an exampe of a header file for C or C++. These are the files that are being included in you #include preprocessor directive. |
|
|||
|
On Mon, 08 Feb 2010 08:10:57 +0000, Ruben Safir wrote:
> There is a catch to the namespace usage though. It might very well be > that your library files, especially if you are creating them yourself, > which you will in C++, have the using directive. If so, you will likely > depend on them. > > Header Files: > Header files normally have a .h suffix. file1.h would be an exampe of a > header file for C or C++. These are the files that are being included > in you > #include preprocessor directive. > > 3) Original Code and runtime directives starting with main. C++ has added a new programming directive called the "using" directive which is used to create namespace. Namespace gives a finer grain control of which symbols your code recognizes in a specified space. Its really important and in many ways was a long time coming to the C family of languages. Most importantly it prevents you from accidently stepping on library symbols or words that you might not have been aware of or that programmers after you might not be aware of. It also allows to define the same symbol in multiple locations of your code without stepping on your own toes. So todays modern C++ main program files might look something look something like this: #ifndef TOP_H #include <iostream> #define TOP_H #endif #ifndef INTARRAY_H #include "intarray.h" #define INTARRAY_H #endif using namespace std; int main( int argc, const char* argv[] ) { //YOUR PROGRAMMING CODE } There is a catch to the namespace usage though. It might very well be that your library files, especially if you are creating them yourself, which you will in C++, have the using directive. If so, you will likely depend on them. Header Files: Header files normally have a .h suffix. file1.h would be an exampe of a header file for C or C++. These are the files that are being included in you #include preprocessor directive. These files are often distributed with a program and you can examine them. They are useful for discovering the definitions of programing objects in libraries are used and often programmers will point them out to you as a form of documentation, which itself is a practice I'm not happy about because many programmers mistake them as a sunstitute for real documentation. Library Files: After researching this, it has occured to me that there is an abiguity about the structure of C and C++ Programming files. Professional programs generally have header files that are described above, but don't have a proper name for the coding files that associate with the headers and which produce object binary files and static or linked libraries. For a beginner this is all confusing and the lack of proper nomenclature makes this all the more harder to learn. I little bit of compiler theory is needed to understand the files structure and binary construction of your program. For now, I just want to point out that programming objects defined in your header file for use in your programming has to have source code to produce the actually machine code that is represented by the symbols in your header file. Those library source files will not have the main function. But the compiler can be asked to create what is called object files, which are partially processed C binary code for later inclusion in your program. When we look closer at the gcc compiler we will examine these object files and learn why they are so important. What is important to say, however, is that in C++, because of its object orientation and its emphasis on creating Application Programming Interfaces (API), most of the C++ coding you will do is taking place in these library C++ source files (which I will refere to as Library Code from here on out). There are two kinds of Library code files that you will work with, that which you create, and that which you borrow from your system for inclusion in your programs. User defined: User defined library files define the code to create working programming objects that are normally declared in your matching header files. These programming source code files look just like your main programming file except they don't have the main function. Your top most main programming file is dependent on these library code files. The code they produce has to be linked into your program by your compiler. Standard C++ or Packaged third party: These are the standard libraries, either in source or in object files, that define standard language needs and are usually found somewhere in /lib or /usr/lib on your system. Standard C++ File Creation: All our C++ programs has to be created in with a standard text editor. The code that the compiler works on, also known as translation units for the compiler at straight ASCII text. You can NOT use a word processor. My prefered text editor is VIM or GVIM, which is a derivitive of VI. VI is the standard text editor on Unix like systems and there are many tutorials for it around the internet. Other editors include EMACS, and then there are C++ working environments like Anjuta, which I strongly discourage. I discourage the Programming Integrated Programming enviroments because with GNU and Unix like systems, your OS is your integrated enviroment, and I believe one should learn to use the standard tools that are on your GNU/Linux system. A standard C++ file needs to have at least one function defined. We will look at functions (also called methods, more closely later, but a new programmer should get use to looking at them from the start, since everything in C++ is encapsulated in a function called main. Functions are defined by following structure "return type" "function name (the symbol)" ( Arguement list) { Statements that end in semi-colon; } Functions do not that semi-colons after the closing curly brace. The main function looks like this int main(int argc, char * argv[]){ return 0; } A realistic C++ main program file, including prepocessor directives would look as follows #include <iostream> using namespace std; int main(int argc, char * argv[]){ return 0; } The curly braces forms a block in which the coder can add really as many instructions as they choose to. These blocks of statements are seen in many C++ syntax structures including functions, if statements, for loops and other structures. While the above is a minimal C++ source file structure, generally most of the heavey lifting of your code takes place outside of main in user defined functions which you create, as well as objects. A more realistic first program skeletan might well look something like this: #include <iostream> using namespace std; void oxygen(){ cout << "oxygen()\n";} void hydrogen(){ cout << "hydrogen()\n";} void helium(){ cout << "helium()\n";} void neon(){ cout << "neon()\n";} int main(int argc, char * argv[]){ oxygen(); hydrogen(); helium(); neon(); return 0; } Here we can see the declaring and defining for 4 user defined functions that are outside of our program, and get instantated only when called. the for functions are called read, sort, compact and write. And notice that we are using the standard namespace called std. |
|
|||
|
On 31 jan, 02:46, Ruben Safir <ru...@mrbrklyn.com> wrote:
> I'm considering working the C++ program in the following manner. *I want > this to be a serious endeavor with commitment to learning. Given the number of errors you teach, it is not really a gift you are making. [snip] > I'm expecting to put out lecture size workshops 2 times a week, maybe > Mondays and Thursdays. *They will be posted to the NYLUG mailing list, > NYLXS mailing list (hangout), to comp.lang.C++. *I'll try to cross post > to all three mediums, Please, don't. This is as bad as spam. [snip] -- Michael |
|
|||
|
Statement Structure: All C and C++ statements (although not all syntax) ends with a semi-colon. You can even put two semi-colons on a single line, seperated by a semicolon, but in general this isn't recommend. Statements are constructed with Data, Operators and Keywords. C++ has an exented set of Keywords than C. Keywords: Keywords are any symbols that the Standard C++ recognizes as having instructional meaning, that is the tell the compiler to do something. The Key Words in C++ are as follows, and learning the exact meaning of all the keywords is essential to learning C++. These are inhereted from C: auto const double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while These are the extended set added to C++ asm dynamic_cast namespace reinterpret_cast try bool explicit new static_cast typeid catch false operator template typename class friend private this using const_cast inline public throw virtual delete mutable protected true wchar_t and most C++ Compilers also recognize the follow Keywords and bitand compl not_eq or_eq xor_eq and_eq bitor not or xor Keywords are completely reserved and can not be used as symbols by any user defined variables in your program. They are exclusive to the language and compilers. There are other important predefined symbols that C++ uses as well. These are not strictly exclusive to the Language, however, overloading them or using them as symbols for variables is a very bad idea. There is a lot of them, but some of them might include cin endl INT_MIN iomanip main npos std cout include INT_MAX iostream MAX_RAND NULL string not to mention the Macros like __DATE__ and __TIME__ Operators: Operators, are very much like functions or methods in that they define processes, taking in arguments and returning outputs (and having side affects). In the C Language, Operators are immutable. You can't change their meaning. In C++ many of them can be overloaded, that is that you can create, and change their meaning. A lot of C++ study involves discussing the overloading of Operators. All Operators, as they do in Mathematics, have precedence and associativity. For example, in arithmetic: 4 x 3 - 10 = 22 and not -28 or 2. That is because multiplication has a higher precedence that subtraction and the associativity is left to right. A complete list of C++ operators is considerable and as follows: ┌─────────────── ────────┬─────── ──────────────── ──────────────── ┬─────────────── ┐ │ Operator │ Type │ Associativity │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ :: │ binary scope resolution │ │ │ :: │ unary scope resolution │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ () │ parentheses │ │ │ [] │ array subscript │ │ │ . │ member selection via object │ │ │ -> │ member selection via pointer │ left to right │ │ ++ │ unary postincrement │ │ │ -- │ unary postdecrement │ │ │ typeid │ run-time type information │ │ │ dynamic_cast< type > │ run-time type-checked cast │ │ │ static_cast │ compile-time type-checked cast │ │ │ reinterpret_cast │ cast for non-standard conversions │ │ │ const_cast │ cast away const-ness │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ ++ │ unary preincrement │ │ │ -- │ unary predecrement │ │ │ + │ unary plus │ │ │ - │ unary minus │ │ │ ! │ unary logical negation │ │ │ ~ │ unary bitwise complement │ │ │ ( type ) │ C-style unary cast │ right to left │ │ sizeof │ determine size in bytes │ │ │ & │ address │ │ │ * │ dereference │ │ │ new │ dynamic memory allocation │ │ │ new[] │ dynamic array allocation │ │ │ delete │ dynamic memory deallocation │ │ │ delete[] │ dynamic array deallocation │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ .* │ pointer to member via object │ │ │ ->* │ pointer to member via pointer │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ * │ multiplication │ │ │ / │ division │ │ │ % │ modulus │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ + │ addition │ │ │ - │ subtraction │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ << │ bitwise left shift │ │ │ >> │ bitwise right shift │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ < │ relational less than │ │ │ <= │ relational less than or equal to │ left to right │ │ > │ relational greater than │ │ │ >= │ relational greater than or equal to │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ == │ relational is equal to │ │ │ != │ relational is not equal to │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ & │ bitwise AND │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ ^ │ bitwise exclusive OR │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ | │ bitwise inclusive OR │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ && │ logical AND │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ || │ logical OR │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ ?: │ ternary conditional │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ = │ assignment │ │ │ += │ addition assignment │ │ │ -= │ subtraction assignment │ │ │ *= │ multiplication assignment │ │ │ /= │ division assignment │ right to left │ │ %= │ modulus assignment │ │ │ &= │ bitwise AND assignment │ │ │ ^= │ bitwise exclusive OR assignment │ │ │ |= │ bitwise inclusive OR assignment │ │ │ >>= │ bitwise left shift assignment │ │ │ <<= │ bitwise right shift with assignment │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ , │ comma │ left to right │ └─────────────── ────────┴─────── ──────────────── ──────────────── ┴─────────────── ┘ We will walk through this complete list of operators later. |
|
|||
|
Statement Structure: All C and C++ statements (although not all syntax) ends with a semi-colon. You can even put two semi-colons on a single line, seperated by a semicolon, but in general this isn't recommend. Statements are constructed with Data, Operators and Keywords. C++ has an exented set of Keywords than C. Keywords: Keywords are any symbols that the Standard C++ recognizes as having instructional meaning, that is the tell the compiler to do something. The Key Words in C++ are as follows, and learning the exact meaning of all the keywords is essential to learning C++. These are inhereted from C: auto const double float int short struct unsigned break continue else for long signed switch void case default enum goto register sizeof typedef volatile char do extern if return static union while These are the extended set added to C++ asm dynamic_cast namespace reinterpret_cast try bool explicit new static_cast typeid catch false operator template typename class friend private this using const_cast inline public throw virtual delete mutable protected true wchar_t and most C++ Compilers also recognize the follow Keywords and bitand compl not_eq or_eq xor_eq and_eq bitor not or xor Keywords are completely reserved and can not be used as symbols by any user defined variables in your program. They are exclusive to the language and compilers. There are other important predefined symbols that C++ uses as well. These are not strictly exclusive to the Language, however, overloading them or using them as symbols for variables is a very bad idea. There is a lot of them, but some of them might include cin endl INT_MIN iomanip main npos std cout include INT_MAX iostream MAX_RAND NULL string not to mention the Macros like __DATE__ and __TIME__ Operators: Operators, are very much like functions or methods in that they define processes, taking in arguments and returning outputs (and having side affects). In the C Language, Operators are immutable. You can't change their meaning. In C++ many of them can be overloaded, that is that you can create, and change their meaning. A lot of C++ study involves discussing the overloading of Operators. All Operators, as they do in Mathematics, have precedence and associativity. For example, in arithmetic: 4 x 3 - 10 = 22 and not -28 or 2. That is because multiplication has a higher precedence that subtraction and the associativity is left to right. A complete list of C++ operators is considerable and as follows: ┌─────────────── ────────┬─────── ──────────────── ──────────────── ┬─────────────── ┐ │ Operator │ Type │ Associativity │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ :: │ binary scope resolution │ │ │ :: │ unary scope resolution │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ () │ parentheses │ │ │ [] │ array subscript │ │ │ . │ member selection via object │ │ │ -> │ member selection via pointer │ left to right │ │ ++ │ unary postincrement │ │ │ -- │ unary postdecrement │ │ │ typeid │ run-time type information │ │ │ dynamic_cast< type > │ run-time type-checked cast │ │ │ static_cast │ compile-time type-checked cast │ │ │ reinterpret_cast │ cast for non-standard conversions │ │ │ const_cast │ cast away const-ness │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ ++ │ unary preincrement │ │ │ -- │ unary predecrement │ │ │ + │ unary plus │ │ │ - │ unary minus │ │ │ ! │ unary logical negation │ │ │ ~ │ unary bitwise complement │ │ │ ( type ) │ C-style unary cast │ right to left │ │ sizeof │ determine size in bytes │ │ │ & │ address │ │ │ * │ dereference │ │ │ new │ dynamic memory allocation │ │ │ new[] │ dynamic array allocation │ │ │ delete │ dynamic memory deallocation │ │ │ delete[] │ dynamic array deallocation │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ .* │ pointer to member via object │ │ │ ->* │ pointer to member via pointer │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ * │ multiplication │ │ │ / │ division │ │ │ % │ modulus │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ + │ addition │ │ │ - │ subtraction │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ << │ bitwise left shift │ │ │ >> │ bitwise right shift │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ < │ relational less than │ │ │ <= │ relational less than or equal to │ left to right │ │ > │ relational greater than │ │ │ >= │ relational greater than or equal to │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ == │ relational is equal to │ │ │ != │ relational is not equal to │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ & │ bitwise AND │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ ^ │ bitwise exclusive OR │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ | │ bitwise inclusive OR │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ && │ logical AND │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ || │ logical OR │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ ?: │ ternary conditional │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┤ │ │ = │ assignment │ │ │ += │ addition assignment │ │ │ -= │ subtraction assignment │ │ │ *= │ multiplication assignment │ │ │ /= │ division assignment │ right to left │ │ %= │ modulus assignment │ │ │ &= │ bitwise AND assignment │ │ │ ^= │ bitwise exclusive OR assignment │ │ │ |= │ bitwise inclusive OR assignment │ │ │ >>= │ bitwise left shift assignment │ │ │ <<= │ bitwise right shift with assignment │ │ ├─────────────── ────────┼─────── ──────────────── ──────────────── ┼─────────────── ┤ │ , │ comma │ left to right │ └─────────────── ────────┴─────── ──────────────── ──────────────── ┴─────────────── ┘ We will walk through this complete list of operators later. |
|
|||
|
On Feb 2, 11:07*pm, Ruben Safir <ru...@mrbrklyn.com> wrote:
> Integer data types can be defined as extended types with different sizes > as follows: > > short int: * * * * * * * * * * * * * * * * * * * -32,768 - 32768 > unsigned short int: * * * * * * * * * * * * * * * * * *0 - 65535 > int * * * * * * * * * * * * * * * -2,147,483.648 - 2,147,483,648 > unsigned int * * * * * * * * * * * * * * * * * 0 - 4,294,967,295 > long int * * * * * * * * * * * * *-2,147,483.648 - 2,147,483,648 > unsigned long int * * * * * * * * * * * * * **0 - 4,294,967,295 > > These numbers are byte sizes (65535 is the largest number representable > in 4 bytes). Are you in the process of learning this stuff yourself, or is this a rough draft you expect us to help you to correct? Knowing how many bits are in things like a byte and how binary numbers are represents in computers is pretty basic programming knowledge. Are those numbers really the ones that are given in the book? From inspection and without working out what 2^31 or 2^32 actually is I can see that the signed ranges are all wrong for 16 and 32 bits. 65535 is the largest number representable in 2 bytes (16 bits, and 65535 is 2^16-1 so 16 bit number can store from 0 to 65535 which is 65536 numbers which 2^16) using an unsigned integral type. Which of the unsigned integer types is 2 bytes is implementation defined, and in fact there is no requirement that one even be present. > Computers has a special chip to work with factional numbers in decimal > notation (not an easy piece of engineering IMO) Presumably you mean binary notation here? And not all computers have an FPU. Floating numbers and fractional numbers aren't really the same thing at all either. > float * * * * * * * *1.17 x 10^-39 - 3.40x10^38 and 6 digit precision > double * * * * * * * 2.22 x10^-308 - 1.19x10^308 and 15 digit precision > long double * * * * *very hardware specific > > As I understand it, the numbers are actually stored in scientific > notation You should probably actually know and understand this if you plan on teaching it. > C++ adds one additional data type calls a Boolean type to store true or > false. *In C (and in C++), processes that need to test for a true or > false view 0 as false and anything else as true. *But there are issues > with that. *int's are 4 bytes and signed, chars can actually be singed > and unsigned as well....so C++ adds > > bool * * * * * * *true or false > > which is really like some kind of enum operator (which hasn't been > introduced). Ummm.... no comment I've not read the rest of it, but if this part is any guide it looks like it still needs a lot of work to get it into any sort of shape where it will be a good resource for other people to learn from. As it stands I'm sure you will learn a lot in getting this stuff correct and to a standard where other people can make use of it. K |
|
|||
|
On Thu, 11 Feb 2010 01:22:22 -0800, Kirit Sælensminde wrote:
> Are you in the process of learning this stuff yourself, or is this a > rough draft you expect us to help you to correct? Knowing how many bits > are in things like a byte and how binary numbers are represents in > computers is pretty basic programming knowledge. Not for beginners. For beginners it is a complete mystery, especially in adult education. > Are those numbers > really the ones that are given in the book? From inspection and without > working out what 2^31 or 2^32 actually is I can see that the signed > ranges are all wrong for 16 and 32 bits. That is from King, C Programming: A Modern Approach ISBN 0393969452 page 111, one of about 8 resources I'm using. |
|
|||
|
On Thu, 11 Feb 2010 01:22:22 -0800, Kirit Sælensminde wrote:
>> Computers has a special chip to work with factional numbers in decimal >> notation (not an easy piece of engineering IMO) > > Presumably you mean binary notation here? And not all computers have an > FPU. Floating numbers and fractional numbers aren't really the same > thing at all either. BTW - I couldn't find much with regard to understandable information on float point processors around. I know that in the old days hackers literally ripped them out of calculators and installed them on main boards. There is an IEEE Floating Point standard which I did some searching on but I couldn't find at least one resource I was looking for by David Goldberg. As I understand the representation, the exponent by the standard for single precision is 8 bits long and the fraction occupies 23 bits. But my general understanding of floating points representation isn't as clear as I'd like and if you have a reference for more background I'd be interested in reading it. Ruben |
|
|||
|
On Thu, 11 Feb 2010 01:22:22 -0800, Kirit Sælensminde wrote:
> As it stands I'm sure you will learn a lot in getting this stuff correct > and to a standard where other people can make use of it. Its really an informal workshop, but I am trying to edit it to be a more permanent reference and constructive criticism, and even nonconstructive criticism is read and reviewed. FWIW, I'm really unhappy with the level of education on Programming in the University level and otherwise. What my kids learn is really useless and the students I've had are not being given a broad enough understanding of programming and Comp Sci. I can tell you some stories, but I'm sure everyone has run into this. I'm hoping to just try to work with people to hammer out a better overall learning construction and to help a few people along the way. Ruben |
|
|||
|
On 11 fv, 17:30, Ruben Safir <ru...@mrbrklyn.com> wrote:
> On Thu, 11 Feb 2010 01:22:22 -0800, Kirit Slensminde wrote: > > Are you in the process of learning this stuff yourself, or is this a > > rough draft you expect us to help you to correct? Knowing how many bits > > are in things like a byte and how binary numbers are represents in > > computers is pretty basic programming knowledge. > > Not for beginners. *For beginners it is a complete mystery, especially in > adult education. > > > Are those numbers > > really the ones that are given in the book? From inspection and without > > working out what 2^31 or 2^32 actually is I can see that the signed > > ranges are all wrong for 16 and 32 bits. > > That is from King, C Programming: A Modern Approach ISBN 0393969452 > page 111, one of about 8 resources I'm using. From which source did you get ? <quote - your post 08 Feb 2010> C++ has added a new programming directive called the "using" directive which is used to create namespace.[..] </quote> -- Michael |
|
|||
|
Ruben Safir <ruben@mrbrklyn.com> wrote in
news:hl1bp8$mv0$5@reader2.panix.com: > As I understand the representation, the > exponent by the standard for single precision is 8 bits long and the > fraction occupies 23 bits. But my general understanding of floating > points representation isn't as clear as I'd like and if you have a > reference for more background I'd be interested in reading it. http://lmgtfy.com/?q=wikipedia+floating+point&l=1 |
|
|||
|
On Thu, 11 Feb 2010 08:44:39 -0800, Michael Doubez wrote:
> On 11 fév, 17:30, Ruben Safir <ru...@mrbrklyn.com> wrote: >> On Thu, 11 Feb 2010 01:22:22 -0800, Kirit Sælensminde wrote: >> > Are you in the process of learning this stuff yourself, or is this a >> > rough draft you expect us to help you to correct? Knowing how many >> > bits are in things like a byte and how binary numbers are represents >> > in computers is pretty basic programming knowledge. >> >> Not for beginners. *For beginners it is a complete mystery, especially >> in adult education. >> >> > Are those numbers >> > really the ones that are given in the book? From inspection and >> > without working out what 2^31 or 2^32 actually is I can see that the >> > signed ranges are all wrong for 16 and 32 bits. >> >> That is from King, C Programming: A Modern Approach ISBN 0393969452 >> page 111, one of about 8 resources I'm using. > > From which source did you get ? > <quote - your post 08 Feb 2010> > C++ has added a new programming directive called the "using" directive > which is used to create namespace.[..] </quote> Lippman, Lajoie: C++ Primer 437-440 ISBN 0201824701 Would you like a complete bibiography? I was thinking of writing one up anyway. Ruben |
|
|||
|
On Thu, 11 Feb 2010 12:40:41 -0600, Paavo Helde wrote:
> Ruben Safir <ruben@mrbrklyn.com> wrote in > news:hl1bp8$mv0$5@reader2.panix.com: > >> As I understand the representation, the exponent by the standard for >> single precision is 8 bits long and the fraction occupies 23 bits. But >> my general understanding of floating points representation isn't as >> clear as I'd like and if you have a reference for more background I'd >> be interested in reading it. > > > http://lmgtfy.com/?q=wikipedia+floating+point&l=1 Thanks. I was looking for something more credible at this point than wikipedia. Just my own opinion, but I really really finished with Wikipedia. Ruben |
|
|
![]() |
| Popular Tags in the Forum |
| announcement, lists, mailing, nylug, nylxs, workshop |
| Thread Tools | |
| Display Modes | |
|
|