L2_-_More_on_C++_(4_on_1) - Lecture 2 More on C++ 2--- 146...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 2 More on C++ 2--- 146 [CS1102C AY0708S1 Lecture 2] Lecture Overview ¡ Object Oriented Features in C++ ¢ Class and Object ¢ Inheritance ¢ Template Class ¡ Useful C++ Libraries ¢ String ¢ Stream ¢ Standard Template Library ¡ Assertion and Exception ¡ Namespace Object Oriented Languages Definition and Motivation 4--- 146 [CS1102C AY0708S1 Lecture 2] Object Oriented Languages ¡ All programming languages like C, C++, Java etc has an underlying programming model ¢ Also known as programming paradigms ¡ Programming Model tells you: ¢ How to organize the information and processes needed for a solution (program) ¢ Allows/facilitates a certain way of thinking about the solution ¢ Analogy : it is a kind of “world view” for the language ¡ Popular programming paradigms: ¢ Procedural : C, Pascal ¢ Object Oriented: Java, C++ ¢ etc 5--- 146 [CS1102C AY0708S1 Lecture 2] Bank Account : A simple illustration ¡ Let’s look at C implementation of a simple bank account ¡ A bank account contains: ¢ Account Number : integer ¢ Balance : double (should be >= 0) ¡ Basic operations: ¢ Withdrawal ¢ Deposit ¡ Using structure is the best approach in C 6--- 146 [CS1102C AY0708S1 Lecture 2] Bank Account : C implementation typedef struct { int acctNum; double balance; } BankAcct ; void initialize( BankAcct* baPtr , int anum) { baPtr->acctNum = anum; baPtr->balance = 0; } int withdraw( BankAcct* baPtr , double amount) { if (baPtr->balance < amount) return 0; //indicate failure baPtr->balance -= amount; return 1; //success } void deposit( BankAcct* baPtr , double amount) { ... Code not shown ... } Structure to hold information for bank account Functions to provide basic operations 7--- 146 [CS1102C AY0708S1 Lecture 2] Bank Account : C Implementation ¡ C treats the data (structure) and process (function) as separate entity: Data Process Passed into Modifies BankAcct structure depost(...) withdraw(...) 8--- 146 [CS1102C AY0708S1 Lecture 2] Procedural Languages ¡ C is a typical procedural language ¡ Characteristics of procedural languages: ¢ Data and Process are separated ¢ Data and Process are “passive” ¡ The caller initiate the execution ¢ User must make sure the data and process are used correctly ¡ No good way to prevent intentional / accidental wrong use 9--- 146 [CS1102C AY0708S1 Lecture 2] Procedural Languages BankAcct ba1; initialize(&ba1, 12345); deposit(&ba1, 1000.50); withdraw(&ba1, 500.00); withdraw(&ba1, 600.00); ... BankAcct ba1; deposit(&ba1, 1000.50); initialize(&ba1, 12345); ba1.acctNum = 54321; ba1.balance = 10000000.00; ... Correct use of BankAcct and its operations Wrong and malicious exploits of BankAcct Forgot to initialize Account Number should not change!...
View Full Document

Page1 / 37

L2_-_More_on_C++_(4_on_1) - Lecture 2 More on C++ 2--- 146...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online