1 / 13

CS1061: C Programming Lecture 22: A Brief Look at C++

CS1061: C Programming Lecture 22: A Brief Look at C++. A. O’Riordan, 2004. Object Oriented Programming. C++ is an object-oriented programming language (OOPL) based on C.

ouida
Download Presentation

CS1061: C Programming Lecture 22: A Brief Look at C++

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. CS1061: C ProgrammingLecture 22: A Brief Look at C++ A. O’Riordan, 2004

  2. Object Oriented Programming • C++ is an object-oriented programming language (OOPL) based on C. • Programmers define entities called objects and define communication between the objects in the form of (member) function invocation. • Objects may consist of both program logic and data. • Programming consists of defining classes of these objects and their interrelationships, i.e. behaviour.

  3. C++ = C + Objects • C++ is based on C. C++ gets its name from the C increment operator ++. • C++ was an attempt to invent a programming language that supported "object oriented" abstractions, yet remained compatible with the widely used C language. • Thus programmers can reap the benefits of object orientation without having to throw away their existing C programs. • C++ is nearly-but not exactly-a superset of ISO C.

  4. The C++ Language • C++ was developed by Bjarne Stoustrup at AT&T in the 1980s, but only became an ISO standard in 1998. • C++ is a problematic language. For example the pointer implementation is considered too low-level. Also there is no automatic garbage collection, i.e. you cannot automatically reclaim used memory when it becomes available. It has complex semantics, and is hard to learn. • Modern languages such as Java and C# aim to combat and remedy many of these disadvantages. • http://java.sun.com/ • http://msdn.microsoft.com/vcsharp/

  5. C versus C++ Many constructs in C and C++ are the same (or almost exactly the same) • Assignment Statement • Arithmetic • Looping – for, while, do..while • Decision Statements – if, switch • Arrays and Pointers • Logic and Relational Operaters Other aspects of the language are quite different • Data Definition – struct versus class • Function Invocation – messages in C++ • Input/Output

  6. Input/Output Input/output is also significantly different in C++ and is based on streams. Here is the "HelloWorld" program. #include<iostream> using namespace std; int main(){ // Display HelloWorld on the screen cout << "HelloWorld" << endl; return(0); }

  7. Example with Function Call #include <iostream> using namespace std; void display_char(char); int main(){ char my_c; cin >> my_c; display_char(my_c); return 0; } void display_char(char c){ cout << "Character is " << c << endl; return; }

  8. String example Here is a simple example – need to include sting header file. #include <string> using namespace std; int main(){ string salutation = "Hello\n"; cout << salutation; return 0; }

  9. Reference Parameters An alternative is pointers is available in C++. This is to use reference parameters: #include<iostream> void add_VAT(float); int nain(){ add_VAT(priceItem); cout << "Price plus VAT is " << priceItem << endl; return 0; } void add_VAT(float &refPriceItem){ refPriceItem*= (1 + VATRate); }

  10. Class Concept • Objects consist of both data and functionality. • In C++ this equates to data members and member functions. • Classes in C++ are implemented as an extension of the struct mechanism in C. • A fundamental construct in object oriented programming is that of inheritance. It is common when classifying things in the real world to order them into hierarchical structures. We can likewise arrange C++ classes into hierarchies.

  11. Permissible operations Data Private Public Encapsulation In C++ we encapsulate the data and functions in a class. The elements of the class are called members. Here is an example of a bank account class. class DepositAccount{ public: DepositAccount(int accnum); void makeWithdrawal(int amt); void makeLodgement(int amt); void printStatement(); private: int accountNumber; float balance; };

  12. Member Functions • An example member function implementation is given here: void DepositAccount::makeWithdrawal(int amt){ if (amt > balance) cout << "You do not have enough in account" << endl; else balance -= amt; } • And called as follows: DepositAccount my_da; my_da.makeWithdrawal(100);

  13. More on C++ For treatment of the C++ language there are numerous excellent books. Here are two: • The C++ primer, 3rd edition, Stanley B. Lippman, Addison-Wesley, 1998. • The C++ Programming Language, 3rd edition, Bjarne Stroustrup, Addison-Wesley, 1997.

More Related