1 / 24

This presentation includes custom animations.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode.

tatum
Download Presentation

This presentation includes custom animations.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

  2. C++ This slide show introduces C++ & compares it to C. Vocabulary: attribute bool cin cout encapsulation iostream method namespace object oriented private public string w_char Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  3. C appeared in 1972. It was developed by Dennis Ritchie at Bell Labs. In 1979, Bjarn Stroustrup at Bell Labs became interested in adding classes to C and overcoming some of the common problems with C. His additions were adopted in 1983 as C++. C++ was first standardized in 1998.

  4. C++ is a superset of C. C++ C

  5. The name: BCPl (Basic Combined Programming Language) …evolved into a language nicknamed “B” Dennis Ritchie adopted parts of B into his new language. Because it was a successor to B, he called it C. Bjarne Stoustrup extended the C language to invent C++ He coined the name C++ to imitate the increment operator (++) adapted from Dale. Weems. “Programming and Problem Solving with C++)

  6. C++ supports (but does not require) object-oriented programming The main objects are in the std namespace and the line using namespace std; appears after the last #include statement

  7. C++ includes two additional data types C Fundamental Data Types void char int float double C++ Fundamental Data Types void char int float double bool wchar_t

  8. Differences: Standard I/O C++ #include <iostream> #include <iomanip> cout cin C #include <stdio.h> printf scanf printf(“The answer is %d\n”, Result); cout << “The answer is “ << Result <<endl; cin >> Rate; scanf(“\n%f”, &Rate) cout <<setw(8) << setprecision(4) << my_float; printf(“%8.4f“, my_float);

  9. C++ provides a robust string header that includes a string data type. C Code #include <stdio.h> #include <string.h> int main () { char FirstName[25]; strcpy(FirstName, “Oscar”); return 0; } C++ Code #include <iostream> #include <string> using namespace std; int main () { string FirstName; FirstName = “Oscar”; return 0; }

  10. C vs C++ Comparison of the Hello World program /* my first program in C */ #include <stdio.h> int main () { printf("Hello World!“); return 0; } // my first program in C++ #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; }

  11. C and C++ use the same arithmetic operators: + - * / % C and C++ use the same unary operators: ++ -- - C and C++ use the same logical operators: ! && || C and C++ use the same comparison operators: == < <= > >= != C and C++ use the same assignment operators: = *= /= %= += -=

  12. C vs C++ keyboard input #include <iostream> #include <iomanip.h> #include <stdio.h> scanf(“\n %d”, &x); cin >> x;

  13. In C, the programmer can define a new data type based on the fundamental types. The new data type is called a structure and is declared using the struct keyword. Inside the structure, attributes are defined based on existing data types. struct TestScore { char Pnumber[8]; int TestNum; float score; }; Variables can now be declared as type TestScore. data types variable names int Age; float Salary; TestScore StudentIn;

  14. C++ expands the structure to include both the data (attributes) and functions (methods). A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. http://cplusplus.com The concept that all the attributes and behaviors of an entity are bound together in a single class is called Encapsulation class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ;

  15. When a language supports the use of classes/objects, it is referred to as object-oriented. Procedure oriented programming the traditional programming that is based on algorithms or a logical step-by-step process for solving a problem. http://www.pcai.com/web/glossary/pcai_p_s_glossary.html Object oriented programming a class of programming languages and techniques based on the concept of an “object” which is a data structure encapsulated with a set of routines which operate on the data. C++ and Java are object oriented programs http://www.powerhousemuseum.com/hsc/lexicon/glossary.htm

  16. Classes are diagrammed as shown below. class name Cat attributes itsAge GetAge(void); SetAge(int age); Meow(void); methods

  17. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ; class object int Age; float Salary; TestScore StudentIn; Cat Puff;

  18. Each method must be defined. Methods are defined in the same manner as a subfunction. The name of the method is justified by the prepending the class name + :: class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ; void Cat::SetAge(int age) { itsAge = age; } method prototype method definition

  19. When calling a method, or referring to an attribute of the class, prepend the OBJECT name (not the class name) and a period to the method or attribute. class Cat { public: int GetAge(void); void SetAge(int age); void Meow(void); private: int itsAge; } ; int main() { Cat Frisky; int Years; int YearCtr; printf("\nPlease enter Frisky's age "); fflush(stdin); scanf("\n%d", &Years); Frisky.SetAge(Years); cout << "Frisky is a cat who is "; cout << Frisky.GetAge() << " years old.\n"; for(YearCtr = 1; YearCtr <= Years; ++YearCtr) { Frisky.Meow(); } printf("\n\n\n\n"); return 0; }

  20. Differences: String class C #include <string.h> …for C-strings C++ #include <cstring> …for C-strings #include <string> …for string class A C-string is an array of chars terminated with NULL A class string is an object and has methods that can be used to manipulate the string. By default, a literal such as “Ohio University” is a C-string.

  21. Declaring strings C-strings are declared and manipulated as in C string class objects are declared as type string string FirstName; string Greeting = “Hello”; A string declared as type string can... use the assignment operator FirstName = “Jane”; use the + operator Greeting = Greeting + “ “ + Firstname (note, at least one of the operands must be a string class object) use string methods FirstName.substr etc use comparison operators == != < <= >= > (watch out for upper/lower case)

  22. C++ cin >> >> (extraction operator) extracts desired data value from the input stream skips any leading whitespace characters if data value is a char >> extracts exactly 1 character if data value is an int >> extracts characters until first inappropriate character if data value is a float >> extracts characters until first inappropriate character if data value is a string >> extracts characters until first whitespace Examples: (given: i & j are ints, x is float, ch is char, str is a string) Statement Data Result cin >> I; 32 cin >> i >> j; 4 60 cin >> i >> ch >> x; 25 A 16.9 cin >> i >> ch >> x; 25A16.9 cin >> i >> j >> x; 12 8 cin >> i >> x; 46 32.4 15 cin >> str; Jane cin >> str; Jane Doe

  23. C++ • Drawbacks to the >> operator when inputting strings • If string isn’t large enough, the >> operator will continue to store characters into memory beyond the end of the array. • The >> operator cannot be used to input strings with embedded whitespace.

  24. Solution cin.get() (assume ch is a char and str is a string and n is an int) get() does not skip leading whitespace cin.get(ch ); reads a single character (even a whitespace) cin.get(str, n); reads up to n-1 chars & appends ‘\0’ stops at newline, or n-1 chars newline left in stream cin.get(str, n , delimiter) reads up to n-1 chars & appends ‘\0’ stops at delimiter, or n-1 chars delimiter left in stream getline(cin, str) reads entire line up to ‘ \n’ extracts newline from stream and stores

More Related