Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design ...
-
Upload
curtis-murphy -
Category
Documents
-
view
217 -
download
0
Transcript of Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design ...
![Page 1: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/1.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
DCO10105 Object-Oriented Programming and Design
Lecture 1: Introduction
What this course is about: C++ programming Object-Oriented programming concepts Good Programming practice Program design
-- By Rossella Lau
![Page 2: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/2.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
About C++
Created by Bjarne Stroustrup at Bell Lab about 1985
Maintains C (early 1980) with simpler usages
O-O language
Powerful, flexible
With a Standard Library and a Standard Template Library
Reference: An introduction to C++ for newbies: http://www.cprogramming.com/begin.html
![Page 3: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/3.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
ANSI/ISO Standard C++
C++ programs are not always portable
September 1998, IS14882 has been approved as an ANS
most of today’s compilers comply with this standard GNU C++ compilers
Visual C++
DevC++ The tool we will use in this course
![Page 4: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/4.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
O-O programming concepts
Usually three basic parts:
Class construction data encapsulation
Inheritance parent-child relationship – base class and sub-class sub-class inherits everything from the parent class software reuse, encapsulation
Polymorphism A sub-class can pretend its base classes A class allows for applying different data types through template An expression denotes different operations through dynamic binding Shorter/Less programming
![Page 5: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/5.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Good programming practice
Documentation Comments Naming identifiers White space: indentation, blank lines, spaces
Coding convention Usually there are rules, in addition to a programming language’s
syntax, to be followed in order to make people in the same organization understand each other better
• Coding style
• Statement usages
Reference: Guide lines for programming styles in this course
![Page 6: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/6.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Good practice I: Naming Convention
Naming in a programming language is always Program id, method id, variables, constants
To name an identifier, one should observe the rules in C++; or the id cannot get past the compiler, otherwise
To follow a convention means even if an id’s name can pass the compiler, it should conform to some additional rules
In this course, the Java naming convention should be followed
![Page 7: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/7.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Program Design
Class design with UML (Unified Modeling Language) Diagram
Object-Oriented Design (OOD) Encapsulation: combine data and operations in a unit Inheritance: create new objects from existing objects Polymorphism: same expression denotes different operations
Program design using structured programming approach Top-down approach with step-wise refinement
Design methods with C++ features: const, &(reference)
![Page 8: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/8.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
#include <iostream>using namespace std;int main() { float area; int r; float const PI = 3.14; cerr << "Please enter radius in whole number:\n"; cin >> r; area = PI * r * r; cout << "The radius you provided was " << r << " feet and the area is about " << area << " sq feet" << endl; return EXIT_SUCCESS;}
A simple C++ program: cirArea.cpp
Header file specificationLocation of header files
C++ entrance, function prototypeBeginning of function body
Function body, C++ statementsEnd of function body
Data Declaration
I/O objects
Output operators
Input operators
Keyword to define constant
Assignment with expressionstring
Statement terminatorsystem-defined id
![Page 9: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/9.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
C++ program style
For non-class programs: Header file specification
Coming with the standard library (SL) or the standard template library (STL)
Whenever a function from the SL or the STL is used, its respective header files should be specified through this “Preprocessor directives”#include <headerName>
Location of header files E.g., using namespace std; std is ANSI/ISO standard where objects of iostream are located
A collection of functions (or methods) main() is a necessary entrance point in a C++ program
![Page 10: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/10.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Preprocessor directives
Whenever a function from the SL or the STL is used, its respective header files should be specified through “Preprocessor directives”#include <headerName>
It is not a C++ statement
It is processed through the “preprocessor” before the compiler has taken place
![Page 11: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/11.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
The general process cycle of a C++ program
Malik’s slide: 2:42
![Page 12: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/12.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Usual style of a function
For each function:
Function prototype (header)typeOfFunction functionID (parameterList)
Function body: { C++ statements } Data declaration statements Executable statements Syntax of basic statements are the same as in Java;
Reference: Malik’s slide 2:5-30, 36-38, 43-47
![Page 13: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/13.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Quick revision and sample statements
Malik 2, Exercises: 7-9, 10.a-f
![Page 14: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/14.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Basic C++ data types
Integral char, short, int, long, bool unsigned char, unsigned short, unsigned int, unsigned long
Floating Point float, double, long couble
Enumeration user-defined data types
Note that string is not a basic data type in C++ but a class in the C++ STL
![Page 15: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/15.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Basic C++ Input statements
E.g., cin >> r; cin is a predefined (in iostream) object which refers to
input from keyboard >> the input operator or extraction operator r is the variable to store the values input from cin
• If r is a basic C++ data type variable, data conversion is not necessary as in Java
Multiple extraction operators on a line E.g., cin >> length >> width;
![Page 16: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/16.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Basic C++ Output statement E.g., cout << r;
cout is a predefined object which refers to output to screen There is another predefined output object cerr which also
direct output to screen; it is a good practice to direct user prompt and error messages to cerr and normal output to cout
<< is the output operator or insertion opertor Variable or literal value can be easily printed
Multiple insertion operators on a line E.g., cout << “The length is “ << length << endl;
![Page 17: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/17.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Output with new line
endl is a predeined id and its value is ‘\n’
Usually, endl is used when the last insertion operand is an
identifier; ‘\n’ is placed at the end of a literal string if the string is the
last insertion operand, e.g.,cout << length << “is input from the user\n”;
![Page 18: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/18.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Sample coding on exercises
Malik: 2 Exercise 18a, , Programming Exercises: 8,11
![Page 19: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/19.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Some major differences from Java
An independent executable module
Not necessary to be a class inside a program
Using template much more than inheritance and dynamic binding
An object can be referenced in three forms: a real object, a pointer, and a reference.
Does not have a “standard web site” for on-line documentation
Some on-line sites can be found through the Helpful links under the course page
![Page 20: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/20.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Summary
This course focus on C++ programming with advanced concepts in O-O design
C++ basic syntax is the same as Java except for program style and, of course, usage of functions in its own libraries
Before a C++ compiler is taken place, pre-process must be performed first
C++ input statement is easier than Java as it does not need numeric data conversion
![Page 21: Rossella Lau Lecture 1, DCO10105, Semester B,2004-5 DCO10105 Object-Oriented Programming and Design Lecture 1: Introduction What this course is about:](https://reader036.fdocuments.us/reader036/viewer/2022062517/56649edd5503460f94bedbf2/html5/thumbnails/21.jpg)
Rossella Lau Lecture 1, DCO10105, Semester B,2004-5
Reference
Malik: 1.9, 2, 13.3
An introduction to C++ for newbies: http://www.cprogramming.com/begin.html
-- END --