E N D
Introduction to C++ Chapter 30
Background • C was the dominant programming language of the 1980s. Also during this time, a new paradigm, the object oriented paradigm, started to grow. C++ was developed by Bjarne Stroustrup at Bell labs to build upon the stable, powerful base of C, and added objects and generic programming. C++ is a superset of C, meaning that any valid C program is also a valid C++ program. • “Stroustrup was more concerned with making C++ useful than with enforcing particular programming philosophies or styles.” Because of this, C++ does not force the use of an entirely object oriented approach, or a procedural approach. It allows programmers to decide for themselves how they want to write their programs, for better or worse.
Added Features • Object Oriented Programming • Classes • Encapsulation • Polymorphism • Inheritance • Single • Multiple • Generic Programming • Templates – allow you to write a single function that works for any data type.
Compiling C++ Programs • Your source file must end with a .C, .cc, .cpp, or .cxx extension. • Use g++ as a compiler instead of gcc. • All of the flags you used with gcc (-Wall -g -O, etc) will also work with g++.
Includes • C++ includes are located in /usr/include/c++/version_num where version_num is the version number of the g++ compiler. • C++ header files have no file extension. For example instead of #include <stdio.h> you would use #include <iostream>. • You can still use the C header files ie: string.h
Namespaces • If you’re using the C++ header files, you need to add a “using directive” to your code to specify which namespace you wish to use. • A namespace is much like a package in Java. It allows a vendor to package up their code. • Now let’s say you’re using some code for one of your programs from two different vendors. Each of the vendor’s packages includes a function called test( ). How do you differentiate one test( ) from the other?
Namespaces Continued • Namespaces allow you to easily specify which version of test( ) you wish to use. • You could type vendor1::test( ) to call the test function (vendor1 is the namespace name in this example). • To save some typing, you can use the “using directive” ie: using namespace vendor1; This will allow you to use just test( ) in your code, but be certain of which version you’re calling. • This can be placed in each function as needed, or you can place it before main( ) so that you don’t need to type it in each of your functions. • At any time after you’ve declared which namespace you’re using, you can still use the :: operator to access functions from another namespace, ie: vendor2::test( );
Output • cout provided by iostream is an ostream object in charge of printing output. cout is much like system.out.println( ) in Java, and doesn’t require a formatting string like C’s printf( ). • cout << “this is a string to be output”; • int pounds = 150;cout << “you weigh “ << pounds << “pounds”;//prints: you weigh 150 pounds//notice the concatenation operator
Input • cin provided by iostream is an istream object in charge of reading input from the user. There is no need to use a format string like with scanf( ), C++ automatically determines the type of data to be entered based on the variable you assign the input into. • int weight=0;cout << “Enter your weight in pounds”;cin >> weight;
Data Types • C++ has all of the basic data types supported by C including structures. It also has two new types to make your life easier. • bool – boolean, true or false • string • You can still use the C style string, an array of strings ending with the null ‘\0’ character. • To use C style string functions in C++, you must include the cstring header file • #include <cstring> • There is now a string object which acts like the String in Java. You need to include the string header file to use it. • #include <string>
The string Class • string temp = “this is a string declaration”; • The string class automatically resizes the string to fit the data stored in it. • You can still use array notation to access individual characters in a string. • You can set one string equal to another, ie: str1 = temp; This isn’t possible with C style strings. • You can concatenate strings with the + operator, ie: str2 = str1 + temp; • You can use cout to print a string. • You can use cin to read in a string from the user. • cin stops reading when it reads in white space. To read in a whole line at a time instead of a word, use the getline( ) function. Ie: getline(cin,temp);
Dynamic Allocation • C++ provides the “new” keyword which dynamically allocates variables just like in Java. • Using “new” returns a pointer to a block of data with the appropriate size for the type of variable you create. • int *temp = new int; • When you’re done with a variable initialized with new, you need to free that block of memory using delete. • delete temp; //frees the memory used by temp • You could technically reassign the temp pointer to a new block of memory using “new” without freeing the memory that it pointed at to start with. This would cause a memory leak. • int *temp = new int; // grab 4 bytes of memory for an inttemp = new int; //grab another 4 bytes. You now no longer have access to // the memory that was allocated in the first “new” call and can’t free it.delete temp; //only frees 4 bytes of memory rather than the 8 we’ve//allocated. • int * array = new int[10]; //dynamically create an array • delete [ ] array; //You need to tell delete that you’re deleting an array
An Example #include <iostream> #include <string> using namespace std; int main( ) { //reads in the names of up to 3000 animals string *animals[3000]; bool run = true; int i=0; for(i=0; i<3000 && run; i++){ cout << "Enter the name of an animal or exit to quit" << endl; animals[i]= new string; getline(cin, *animals[i]); if(*animals[i]=="exit"){ run = false; i--; }//end if }//end for return 0; }//end main
Helpful Links • http://research.att.com/~bs • http://cplus.about.com