• Like
Parm
Upcoming SlideShare
Loading in...5
×
Uploaded on

inrductin about discovery of c++

inrductin about discovery of c++

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
113
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
1
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. C++ Overview• History and major features of C++• Input and output• Preprocessor directives• Comments January 26, 2012 CS410 – Software Engineering 1 Lecture #2: Hello, C++ World!
  • 2. History of C++1972: C language developed at Bell Labs •Dennis Ritchie wrote C for Unix OS •Needed C for work with Unixlate 70s: C becomes popular for OS development by many vendors •Many variants of the language developed •ANSI standard C in 1987-89
  • 3. History of C++ (continued)early 80s: Bjarne Stroustrup adds OO features to C creating C++90s: continued evolution of the language and its applications •preferred language for OS and low level programming •popular language for application development •low level control and high level power
  • 4. History of C++• 1980: “C-with-classes” developed by Bjarne Stroustrup• 1983: C-with-classes redesigned and called C++• 1985: C++ compilers made available• 1989: ANSI/ISO C++ standardization starts• 1998: ANSI/ISO C++ standard approved January 26, 2012 CS410 – Software Engineering 4 Lecture #2: Hello, C++ World!
  • 5. Conceptually what is C++Alternatives: •is it C, with lots more options and features? •is it an OO programming language with C asits core? •is it a development environment?On most systems it is a development environment, language, and library, used for both procedural and object oriented programming, that can be customized and extended as desired
  • 6. Versions of C++ANSI C++Microsoft C++ (MS Visual C++ 6.0)Other vendors: Borland, Symantec, Turbo, …Many older versions (almost annual) including different version of C tooMany vendor specific versionsMany platform specific versionsFor this class: Unix / Linux based versions •g++
  • 7. Characteristics of C++ as a Computer LanguageProceduralObject OrientedExtensible...
  • 8. Other OO LanguagesSmalltalk •pure OO language developed at PARCJava •built on C/C++ •objects and data typesEifel and others
  • 9. What you can do with C++Apps (standalone, Web apps, components)Active desktop (Dynamic HTML, incl Web)Create graphical appsData access (e-mail, files, ODBC)Integrate components w/ other languages
  • 10. Major Features of C++• Almost upward compatible with C •Not all valid C programs are valid C++ programs. • Why? • Because of reserved words in C++ such as ‘class’• Extends C with object-oriented features• Compile-time checking: strongly typed• Classes with multiple inheritance• No garbage collection, but semi-automatic storage reclamation January 26, 2012 CS410 – Software Engineering 10 Lecture #2: Hello, C++ World!
  • 11. Disadvantages of C++Tends to be one of the less portable languagesComplicated? •40 operators, intricate precedence, pointers, etc. •can control everything •many exceptions and special cases •tremendous libraries both standard, vendor specific, and available for purchase, but all are intricateAspects above can result in high maintenance
  • 12. Advantages of C++Available on most machinesCan get good performanceCan get small sizeCan manage memory effectivelyCan control everythingGood supply of programmersSuitable for almost any type of program (from systems programs to applications)
  • 13. Sample C++ Program#include <iostream> if (enteredPasswd == deptChairPasswd)#include <string> {using namespace std; cout << "Thank you!" << endl; passwdOK = true;int main() }{ else string deptChairName, deptChairPasswd, { enteredPasswd; cout << "Hey! Are you really " << bool passwdOK; deptChairName << "?n"; int attempts = 0; cout << "Try again!n"; passwdOK = false; deptChairName = GetDeptChairName(); } deptChairPasswd = GetDeptChairPasswd(); } while (!passwdOK && attempts < 3); cout << "How are you today, " << if (passwdOK) deptChairName << "?n"; SetNewParameters(); do else { { cout << "Please enter your password: "; Shout(“Warning! Illegal access!”); cin >> enteredPasswd; CallCampusPolice(); attempts++; } return 0; } January 26, 2012 CS410 – Software Engineering 13 Lecture #2: Hello, C++ World!
  • 14. Input and Output• Some standard functions for input and output are provided by the iostream library.• The iostream library is part of the standard library.• Input from the terminal (standard input) is tied to the iostream object cin.• Output to the terminal (standard output) is tied to the iostream object cout.• Error and warning messages can be sent to the user via the iostream object cerr (standard error). January 26, 2012 CS410 – Software Engineering 14 Lecture #2: Hello, C++ World!
  • 15. Input and Output• Use the output operator (<<) to direct a value to standard output.• Successive use of << allows concatenation.• Examples: •cout << “Hi there!n”; •cout << “I have “ << 3 + 5 << “ classes today.”; •cout << “goodbye!” << endl; (new line & flush) January 26, 2012 CS410 – Software Engineering 15 Lecture #2: Hello, C++ World!
  • 16. Input and Output• Use the input operator (>>) to read a value from standard input.• Standard input is read word by word (words are separated by spaces, tabs, or newlines).• Successive use of >> allows reading multiple words into separate variables.• Examples: •cin >> name; •cin >> nameA >> nameB; January 26, 2012 CS410 – Software Engineering 16 Lecture #2: Hello, C++ World!
  • 17. Input and OutputHow can we read an unknown number of inputvalues?int main(){ string word; while (cin >> word) cout << “word read is: “ << word << “n”; cout << “OK, that’s all.n”; return 0;} January 26, 2012 CS410 – Software Engineering 17 Lecture #2: Hello, C++ World!
  • 18. Input and OutputThe previous program will work well if we use a fileinstread of the console (keyboard) as standard input.In Linux, we can do this using the “<“ symbol.Let us say that we have created a text file “input.txt”(e.g., by using gedit) in the current directory.It contains the following text:“This is just a stupid test.”Let us further say that we stored our program in a filenamed “test.C” in the same directory. January 26, 2012 CS410 – Software Engineering 18 Lecture #2: Hello, C++ World!
  • 19. Input and OutputWe can now compile our program using the g++compiler into an executable file named “test”:$ g++ test.C –o testThe generated code can be executed using thefollowing command:$ ./testIf we would like to use the content of our file “input.txt”as standard input for this program, we can type thefollowing command:$ ./test < input.txt January 26, 2012 CS410 – Software Engineering 19 Lecture #2: Hello, C++ World!
  • 20. Input and OutputWe will then see the following output in our terminal:Word read is: ThisWord read is: isWord read is: justWord read is: aWord read is: stupidWord read is: test.OK, that’s all. January 26, 2012 CS410 – Software Engineering 20 Lecture #2: Hello, C++ World!
  • 21. Input and OutputIf we want to redirect the standard output to a file, say“output.txt”, we can use the “>” symbol:$ ./test < input.txt > output.txtWe can read the contents of the generated file bysimply typing:$ less output.txtWe will then see that the file contains the output thatwe previously saw printed in the terminal window.(By the way, press “Q” to get the prompt back.) January 26, 2012 CS410 – Software Engineering 21 Lecture #2: Hello, C++ World!
  • 22. Input and OutputIf you use keyboard input for your program, it willnever terminate but instead wait for additional input.Use the getline command to read an entire line fromcin, and put it into a stringstream object that can beread word by word just like cin.Using stringstream objects requires the inclusion ofthe sstream header file:#include <sstream> January 26, 2012 CS410 – Software Engineering 22 Lecture #2: Hello, C++ World!
  • 23. Input and Output#include <iostream>#include <sstream>#include <string>using namespace std;int main(){ string userinput, word; getline(cin, userinput); stringstream mystream(userinput); while (mystream >> word) cout << "word read is: " << word << "n"; cout << "OK, that’s all.n"; return 0;} January 26, 2012 CS410 – Software Engineering 23 Lecture #2: Hello, C++ World!
  • 24. Input and OutputBy the way, you are not limited to strings when usingcin and cout, but you can use other types such asintegers.However, if your program expects to read an integerand receives a string, the read operation will fail.If your program always uses file input and output, it isbetter to use fstream objects instead of cin and cout. January 26, 2012 CS410 – Software Engineering 24 Lecture #2: Hello, C++ World!
  • 25. File Input and Output#include <iostream> if (!outfile)#include <fstream> {#include <string> cerr << “error: unable to openusing namespace std; output file”;int main() return –2;{ } ofstream outfile(“out_file.txt”); ifstream infile(“in_file.txt”); string word; while (infile >> word) if (!infile) outfile << word << “_”; { cerr << “error: unable to open return 0; input file”; } return –1; } January 26, 2012 CS410 – Software Engineering 25 Lecture #2: Hello, C++ World!
  • 26. Preprocessor DirectivesPreprocessor directives are specified by placing apound sign (#) in the very first column of a line in ourprogram.For example, header files are made part of ourprogram by the preprocessor include directive.The preprocessor replaces the #include directive withthe contents of the named file.There are two possible forms:#include <standard_file.h>#include “my_file.h” January 26, 2012 CS410 – Software Engineering 26 Lecture #2: Hello, C++ World!
  • 27. Preprocessor DirectivesIf the file name is enclosed in angle brackets (<, >),the file is presumed to be a standard header file.Therefore, the preprocessor will search for the file ina predefined set of locations.If the file name is enclosed by a pair of quotationmarks, the file is presumed to be a user-suppliedheader file.Therefore, the search for the file begins in thedirectory in which the including file is located (projectdirectory). January 26, 2012 CS410 – Software Engineering 27 Lecture #2: Hello, C++ World!
  • 28. Preprocessor DirectivesThe included file may itself contain an #includedirective (nesting).This can lead to the same header file being includedmultiple times in a single source file.Conditional directives guard against the multipleprocessing of a header file.Example:#ifndef SHOUT_H#define SHOUT_H /* shout.h definitions go here */#endif January 26, 2012 CS410 – Software Engineering 28 Lecture #2: Hello, C++ World!
  • 29. Preprocessor DirectivesThe #ifdef, #ifndef, and #endif directives are mostfrequently used to conditionally include program codedepending on whether a preprocessor constant isdefined.This can be useful, for example, for debugging: January 26, 2012 CS410 – Software Engineering 29 Lecture #2: Hello, C++ World!
  • 30. Preprocessor Directivesint main(){#ifdef DEBUG cout << “Beginning execution of main()n”;#endif string word; while (cin >> word) cout << “word read is: “ << word << “n”; cout << “OK, that’s all.”; return 0;} January 26, 2012 CS410 – Software Engineering 30 Lecture #2: Hello, C++ World!
  • 31. Comments• Comments are an important aid to human readers of our programs.• Comments need to be updated as the software develops.• Do not obscure your code by mixing it with too many comments.• Place a comment block above the code that it is explaining.• Comments do not increase the size of the executable file. January 26, 2012 CS410 – Software Engineering 31 Lecture #2: Hello, C++ World!
  • 32. CommentsIn C++, there are two different comment delimiters:• the comment pair (/*, */),• the double slash (//).The comment pair is identical to the one used in C:• The sequence /* indicates the beginning of a comment.• The compiler treats all text between a /* and the following */ as a comment.• A comment pair can be multiple lines long and can be placed wherever a tab, space, or newline is permitted.• Comment pairs do not nest. January 26, 2012 CS410 – Software Engineering 32 Lecture #2: Hello, C++ World!
  • 33. CommentsThe double slash serves to delimit a single-linecomment:• Everything on the program line to the right of the delimiter is treated as a comment and ignored by the compiler.A typical program contains both types of comments.In general, use comment pairs to explain thecapabilities of a class and the double slash to explaina single operation. January 26, 2012 CS410 – Software Engineering 33 Lecture #2: Hello, C++ World!