Rossella Lau Lecture 1, DCO10105, Semester B,2005-6 DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:

Slides:



Advertisements
Similar presentations
Introduction to Programming in C++ John Galletly.
Advertisements

CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Chapter 2: Basic Elements of C++
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
Three types of computer languages
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Chapter 2: Basic Elements of C++
C++ fundamentals.
Basic Elements of C++ Chapter 2.
Chapter 01: Introduction to Computer Programming
COMPUTER SCIENCE I C++ INTRODUCTION
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
1 C++ Syntax and Semantics, and the Program Development Process.
Week 1 Algorithmization and Programming Languages.
Course Title: Introduction to C++ Course Instructor: ADEEL ANJUM Chapter No: 01 1 BY ADEEL ANJUM (MCS, CCNA,WEB DEVELOPER)
C++ Programming: Basic Elements of C++.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Fundamental Programming: Fundamental Programming Introduction to C++
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Chapter 15 - C++ As A "Better C"
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
1.13 The Key Software Trend: Object Technology
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
C++ Programming Basics
Presentation transcript:

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, 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:

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, O-O programming concepts Usually three basic parts:  Class construction  data encapsulation  Inheritance  software reuse, encapsulation  Polymorphism  shorter/less programming

Rossella Lau Lecture 1, DCO10105, Semester B, Class Construction  To create a template for object instantiation  Inside the class, it defines the data and operations (method or function)  Outside the class, it does not need to know the details of the data and operations  Data Encapsulation

Rossella Lau Lecture 1, DCO10105, Semester B, Inheritance  Parent-child relationship – base class and sub-class  Sub-class inherits everything from the parent class  Software reuse, encapsulation

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, 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)

Rossella Lau Lecture 1, DCO10105, Semester B, #include 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 specification Location of header files C++ entrance, function header Beginning of function body Function body, C++ statements End of function body Data Declaration I/O objects Output operators Input operator Keyword to define constant Assignment with expression string Statement terminator system-defined id

Rossella Lau Lecture 1, DCO10105, Semester B, 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  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

Rossella Lau Lecture 1, DCO10105, Semester B, Preprocessor directives  #include  It is not a C++ statement  It is processed through the “preprocessor” before the compiler has taken place Reference: Malik’s slides: 2:42-44

Rossella Lau Lecture 1, DCO10105, Semester B, The general process cycle of a C++ program Malik’s slide: 2:45

Rossella Lau Lecture 1, DCO10105, Semester B, 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:6-36, 39, 41

Rossella Lau Lecture 1, DCO10105, Semester B, Quick revision and sample statements  Malik’s Exercises: 2:7 Do a walk-through to find the value assigned to e. Assume that all variables are properly declared. a = 3; b = 4; c = (a % b) * 6; d = c / b; e = (a + b + c + d) / 4;

Rossella Lau Lecture 1, DCO10105, Semester B, Quick revision and sample statements  Malik’s Exercises: 2:8 Which of the following variable declarations are correct? If a variable declaration is not correct, give the reason(s) and o provide the correct variable declarations. n = 12; // Line 1 char letter = ; // Line 2 int one = 5, two; // Line 3 double x, y, z;d = c / b; // Line 4

Rossella Lau Lecture 1, DCO10105, Semester B, Quick revision and sample statements  Malik’s Exercises: 2:9 Which of the following are valid C++ assignment statements? Assume that i, x, and percent are double variables. a. n = 12; b. x + 2 = x; c. x = 2.5 * x; d. percent = 10%;

Rossella Lau Lecture 1, DCO10105, Semester B, Quick revision and sample statements  Malik’s Exercises: 2:10a-f Write C++ statements that accomplish the following. a. Declares int variables x and y. b. Initializes and int variable x to 10 and a char variable ch to ‘ B ’. c. Updates the value of an int variable x by adding 5 to it. d. Sets the value of a double variable z to e. Copies the content of an int variable y into an int variable z. f. Swaps the contents of the int variables x and y.

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, 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;

Rossella Lau Lecture 1, DCO10105, Semester B, 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 operator  Variable or literal value can be easily printed  Multiple insertion operators on a line; e.g.,  cout << “The length is “ << length << endl;

Rossella Lau Lecture 1, DCO10105, Semester B, 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”;

Rossella Lau Lecture 1, DCO10105, Semester B, Sample error removal on exercise  Malik’s Exercise 2:18a #include const int prime =11,213; const rate = 15.6 int main() { int i, x, y, w; x = 7; y = 3; x = x + w; prime = x + prime; cout << prime << endl; wages = rate * 36.75; cout << “Wages = “ << wages << endl; return 0; }

Rossella Lau Lecture 1, DCO10105, Semester B, Sample coding on exercises  Malik’s Programming Exercises: 2: 8  Write a program that prompts the user to input five decimal numbers. The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.  Malik’s Programming Exercises: 2: 11  Write a C++ program that prompts the user to input the elapsed time fr an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. (For example, if the elapsed time is 9630 seconds, then the output is 2:40:30.)

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, 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

Rossella Lau Lecture 1, DCO10105, Semester B, Reference  Malik: 1.9, 2, 13.3  An introduction to C++ for newbies: -- END --