ECE 1211 OBJECTORIENTED PROGRAMMING FOR ENGINEERS - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

ECE 1211 OBJECTORIENTED PROGRAMMING FOR ENGINEERS

Description:

Visit the course website regularly since most important notice will be put there ... declaration of object cin, cout, cerr and clog that we used for I/O operation ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 56
Provided by: akhmad2
Category:

less

Transcript and Presenter's Notes

Title: ECE 1211 OBJECTORIENTED PROGRAMMING FOR ENGINEERS


1
ECE 1211OBJECT-ORIENTED PROGRAMMING FOR
ENGINEERS
made easy
  • Semester I 2006/2007

2
(No Transcript)
3
Course Instructor
  • Instructor MOHD NAZMI BIN YUSOFF
  • Office E2-3-10.6
  • Tel/Fax 03-6196-4427
  • Course website http//staff.iiu.edu.my/mnazmi/
  • Visit the course website regularly since most
    important notice will be put there

4
  • MOHD NAZMI BIN YUSOFF
  • 2005 Ph.D System Electronic (System Control)
    Eng.
  • Tokyo University of Technology
  • 2001 M.Eng System Electronic (Electronic
    Communication) Eng. Tokyo University of
    Technology
  • 1999 B.Eng Electronic Communication
    Information Eng.
  • Tokyo Denki University

5
General rules and regulations
  • Signing for others behalf
  • ?Erasure of his/her attendance for that lecture
  • Failure to attend quiz without prior notice
    (except MC and emergency)
  • ?No makeup quiz given for WHATEVER REASON
  • Assignment must be submitted before the deadline.
    No late submission will be entertained. ? Copied
    and copy assignments deserve ZERO mark

6
Course Objective
  • To introduce the concept of Object-Oriented
    Programming (OOP) and Design (OOD).
  • To develop programming skills based on the
    Object-Oriented paradigm.
  • To apply the Object-Oriented Programming concept
    in engineering area.

7
Learning Outcomes
  • After completion of this course the students will
    be able to
  • Explain the concept of Object-Oriented
    Programming.
  • Outline and apply the concepts of encapsulation,
    inheritance and polymorphism.
  • Design, code, test and debug simple program in
    Object-Oriented Programming language
  • Develop a simple project in collaboration with
    other students in a group

IMPORTANT TO UNDERSTAND THE CONCEPT and
REINFORCE WITH EXTENSIVE PRACTICES
8
Grading scheme
  • Assignments and Quizzes 25
  • Projects
    35
  • Mid-term Examination
    20
  • Final Examination
    20
  • TOTAL
    100

9
Introduction to C
10
Objectives (1)
  • Understand differences between C and C
  • Skill to write simple program in C with
    standard I/O
  • Understand namespace concept and usage
  • Familiar with C standards

11
What is a program?
  • A sequence of instructions (program statements)
    to solve a problem
  • Program statements (actions)
  • Specific order of executions (algorithm)
  • ?solve a problem

12
C
  • Supports only procedural programming concept
  • Data and operations are two independent
    entity/unit
  • (Computer) Program is viewed as a sequence of
    actions (program statements)
  • Difficult to maintain integrity of data

13
C
  • Developed to overcome problems found in C
  • Contains almost everything in C and other
    features to support efficient software (large and
    complex programs) development

14
Additional features in C
  • Supports object-oriented programming concept
    (to-be detailed through out the course).
    Particularly useful to develop large software
  • Strict type checking to reduce the
    chances/probability of bug (error)
  • Enhanced pointers, functions
  • On-the-fly variable declaration
  • Variables can be declared anywhere in the program
    (not necessarily at the beginning of it)

15
C and C
  • C is a superset of C

C
C
16
Introduction to C
  • We can write single line comment by using //
  • Latest C compiler also support this feature
  • Does not require the .h extension for standard
    libraries ( required for user defined libraries)
  • Library collection of files that contain
    declarations and definitions of programming
    entities ( function, data types, constants, etc.)

17
C Input/Output
  • C uses scanf() and printf() to perform input and
    output
  • C treats input/output operations as
  • Keyboard as cin object of type istream
  • Output terminal as cout object of type ostream
  • Operators gtgt and ltlt to send streams to cout or
    read from cin.

18
  • / Welcome to C /
  • include ltiostreamgt // .h extension is not
    required for standard lib.
  • using namespace std // std namespace
  • int main() //function header
  • string name //variable declaration name of type
    string
  • //prompt user to enter name
  • cout ltlt " Please Enter your nick name "
  • //read user name
  • cin gtgt name
  • //print out welcome message
  • cout ltlt " Welcome to C ltlt" ltlt name ltlt "gtgt" ltlt
    endl

19
  • //PROG1_1 Program demonstrates C I/O.
  • include ltiostreamgt
  • using namespace std //cout and cin are defined
    in std
  • int main()
  • float length, width, area
  • coutltlt"Enter length and width gt "
    //output
  • cingtgtlengthgtgtwidth
    //input
  • arealengthwidth
  • coutltlt"Area "ltltarea
    //output
  • return 0
  • /Demonstration of I/O in C/
  • includeltstdio.hgt
  • int main()
  • float length, width, area /variable
    declarations/
  • printf("Enter length and width\n") //prompt
    user to input
  • scanf("ff",length, width ) //read
    keyboard input
  • area widthlength
  • printf("Area f\n",area )
  • system("pause")
  • return 0

20
Data types
  • Data types in C
  • void (nothing)
  • char A, 1, ? etc.
  • int 1, 124, 999, etc.
  • Variations short, long, unsigned
  • float 123.44, 0.23, etc.
  • double 123.44, 0.00000001
  • bool true (logic 1), false (logic 0)

21
  • // Examples of boolean usage
  • includeltiostreamgt
  • using namespace std
  • int main()
  • bool x,y,z
  • cout ltlt"Simulate XOR gate \n"
  • cout ltlt"Enter two logic values \n"
  • cin gtgt x gtgt y
  • cout ltlt "The XOR of " ltlt x ltlt"," ltlt y ltlt ""
    ltlt (x y) ltlt endl
  • system("pause")
  • return 0

22
Data type conversion
  • Using various data types in the same program is
    common
  • Need to perform operation involve mixed data
    types
  • Need to convert between data types
  • Automatic conversion
  • Values of different data types (in an expression)
    are first converted to the largest one then the
    expression is evaluated
  • A r.h.s value is converted to the data type of
    l.h.s

23
It may become a source of logic error
  • // Demonstrate automatic conversion between data
    types
  • includeltiostreamgt
  • using namespace std
  • int main()
  • int a2
  • short a222
  • double b3.14156, x
  • x ab
  • a b
  • cout ltlt a ltlt '\t' ltlt b ltlt '\t' ltlt x ltlt endl
  • system("pause")

24
Explicit conversion
  • C-style cast
  • (new type) var
  • (float) x
  • C style cast
  • Standard data type conversion
  • static_castltnew typegt ( var )
  • To remove const-ness of a function argument
  • const_cast
  • User-defined (class) type conversion
  • reinterpret_castltnew typegt (object)

25
  • // Demonstrate Explicit conversion between data
    types
  • includeltiostreamgt
  • using namespace std
  • int main()
  • int a10
  • double b, d
  • b (double) a //C-style casting
  • d static_castltdoublegt (a) //C style
    casting
  • cout ltlt a ltlt '\t' ltlt b ltlt'\t' ltlt d ltlt endl
  • const int aa a //declaring const reference
  • const_castltintgt(aa) 7
  • cout ltlt aa ltlt endl
  • system("pause")

26
Variables scope
  • Scope is the program portion/part where a
    variable can be referenced
  • Global scope
  • A variable can be referenced anywhere in the
    program
  • Local scope
  • A variable can be referenced only in the portion
    where it is declared

27
Global variable in C
  • C allows the same identifier to be used as
    global and local variables names simultaneously
    (at the same time)
  • Distinguished by (scope resolution operator)

28
  • // Scope of variables
  • includeltiostreamgt
  • using namespace std
  • int x111 //declare global variable x
  • int main()
  • int x 222 //declare local variable x
  • int x 333 //declare yet another local
    variable having block scope
  • cout ltlt "X in the block is " ltlt x ltlt endl
  • cout ltlt "X in the main is " ltlt x ltlt endl
  • cout ltlt "Global value of x is " ltlt x ltlt
    endl
  • system("pause")
  • return 0

29
I/O formatting
  • In C programmers have to specify the format of
    any input/output operation
  • int x
  • printf(the value of x is d\n, x )
  • C type formatting is automatic for build in
    data types
  • int x
  • cout ltlt the value of x is ltlt x ltlt endl

30
Manipulating I/O
  • Manipulate base of numbers
  • Oct, hex, dec
  • Manipulate character controls
  • Newline ch., null ch., etc
  • Manipulate format control
  • Requires ltiomanipgt header file
  • setw(int) field width for a single output field
  • setprecision(int) floating point precision

31
  • //PROG1_2 Program demonstrates C formatting.
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • void print()
  • coutltlthexltltsetw(5)ltlt11ltltendl
  • int main()
  • coutltltsetw(20)ltlt"C FORMATTING"ltltendl
  • coutltlt"Integer numbers\n"
  • coutltlt173ltltendl
  • print()
  • coutltltsetiosflags(iosleftiosuppercase)
  • coutltltsetw(6)ltlt15ltltdecltlt15ltltendl
  • coutltlt"\nFloating point numbers"ltltendl

32
Namespace
  • Large program/software may consist of separate
    files
  • An entity where we can declare variables, define
    functions, etc. to avoid clashes or conflict
  • E.g., namespace std contains the declaration of
    object cin, cout, cerr and clog that we used for
    I/O operation

33
  • //PROG1_4 Program demonstrates using directives.
  • include ltiostreamgt
  • using namespace std //predefined namespace
  • namespace Rectangle //user-defined
    namespace
  • float length
  • float width
  • void area() coutltlt"Area "ltlt(lengthwidth)
  • using namespace Rectangle //Specifies
    user-defined namespace
  • int main()
  • coutltlt"Enter length gt "
  • cingtgtlength
  • coutltlt"Enter width gt "
  • cingtgtwidth
  • area()
  • return 0

34
  • //PROG1_5 Program demonstrates code without the
    using directive.
  • include ltiostreamgt
  • namespace Rectangle
  • float length
  • float width
  • void area() stdcoutltlt"Area
    "ltlt(lengthwidth)
  • int main()
  • stdcoutltlt"Enter length gt "
  • stdcingtgtRectanglelength
  • stdcoutltlt"Enter width gt "
  • stdcingtgtRectanglewidth
  • Rectanglearea()
  • return 0

35
Summary
  • Are you familiar with C syntaxes now?
  • C provides more facilities for programmers
  • New data type
  • Ease of I/O operation
  • Scope of variables
  • Global or local
  • Global variables can be accessed by using
  • More facilities to manipulate I/O
  • Requires ltiomanipgt library

36
Assignment
  • Write a program that do the following
  • Read from user
  • Full name
  • Date of birth
  • Country of origin
  • CGPA
  • Print out to the screen
  • Hints
  • You can declare string variables to store name
  • Use cin.get( string str, int len ) or
    cin.getline( string str, int len ) to read full
    name

37
Assignment
  • Submit by Friday April 27, 2006 1700 (strictly)
    through email to
  • To assignment.unggul_at_gmail.com
  • Subject ASSIGNMENT1
  • Cc your own email
  • Make sure the subject is correct
  • Successfully sending email DOES NOT guarantee
    delivery
  • Successful submission will get a auto-reply
    message
  • Identified copy shall get zero mark

38
Template
  • / Assignment X
  • Name Akhmad Unggul Priantoro
  • Matric 0550001
  • /
  • includeltabcgt
  • using namespace xyz
  • . Your codes come here

39
Pointers, References and Dynamic Memory
Allocation and Array
40
Objectives (2)
  • To understand the differences between C and C
    pointers
  • To familiarize reference data type
  • To be able to allocate and release (deallocate)
    memory dynamically
  • new operator to allocate memory
  • delete operator to deallocate memory
  • To appreciate the benefit of dynamic memory

41
Why pointers?
  • Many says pointer is difficult to master
  • It is useful to write program efficiently
  • by dynamic memory allocation
  • Modification through functions
  • To support some data structures such as linked
    list, binary tree

42
Pointers
  • Pointer is variable that contains memory address
    of any of the following
  • Variable
  • Pointer
  • Function

43
Pointer declaration
  • Pointer to variable
  • Type var
  • double x
  • void y
  • Pointer to another pointer
  • double z
  • double x z
  • double y x
  • Operators
  • is called indirection (dereferencing) operator
  • is called address-of operator

44
  • // Pointer declarations
  • includeltiostreamgt
  • using namespace std
  • int main()
  • double z 3.1415
  • double y z //declare a pointer to
    variable
  • double x y //declare a pointer to
    pointer. note the
  • //double v 10 //error 10 is an int
    literal, not variable
  • //int ptr z // z is not an integer
  • //double ptr2 z //pointer must be
    initialized with address
  • cout ltlt z ltlt"\t" ltlt y ltlt "\t" ltlt x ltlt endl
  • system("pause")
  • return 0

3.1415
0x100
z
0x100
y
0x200
0x200
x
0x220
45
Pointer declaration
  • double x //non-const variable
  • double ptr x // non-const pointer to
    non-const variable
  • const double ptr2 x2 //non-const pointer to
    const variable
  • const double const ptr3 x2 //const pointer
    to const variable
  • Double const ptr4 x //const pointer to
    non-const

46
  • /const pointer and const variable /
  • includeltiostreamgt
  • using namespace std
  • int main()
  • int x 10 //non-const variable
  • int y -100
  • int ptr1 x //pointer to integer
  • const int ptr2 x // pointer to const
    integer
  • const int const ptr3 x // const pointer
    to const integer
  • int const ptr4 x //const pointer
  • cout ltlt "x " ltlt x ltlt endl
  • cout ltlt "ptr1 " ltlt ptr1 ltlt " ptr2 " ltlt
    ptr2 ltlt " ptr3 " ltlt ptr3 ltlt endl
  • ptr1 100
  • cout ltlt "ptr1 " ltlt ptr1 ltlt " ptr2 " ltlt
    ptr2 ltlt " ptr3 " ltlt ptr3 ltlt endl
  • //ptr2 1000 //error. try to modify a read
    only location

47
Reference
  • It is an alternative name of other variable of
    the same type. (an alias?)
  • It must be initialized during declaration
  • type var var2
  • int y 10
  • int z y //declare reference variable z
  • //which is initialized to y

48
  • //PROG3_1 Program demonstrates a use of an
    independent
  • // reference variable.
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int i 13
  • int iref i //declaring a reference
    variable
  • coutltlt"The value is gt "ltltiref
  • i--
  • coutltlt"\nAfter decrementing gt "ltltirefltltendl
  • iref 99
  • coutltlt"The value is now gt "ltlti
  • return 0

49
Static and Dynamic Array
  • Static array
  • Memory must be allocated during the compilation
    of the program
  • Fixed during the execution of the program
  • If less space is used ?waste of memory
  • If more space is used ?may cause crash
  • Dynamic array
  • Memory can be allocated dynamically during run
    time (execution)
  • The size varies
  • Efficient usage of memory

50
  • // Program demonstrate static array and pointer
  • includeltiostreamgt
  • using namespace std
  • int main()
  • const int SIZE 50
  • int markSIZE 0
  • int input
  • int count 0
  • cout ltlt "Enter grade 0-100" ltlt endl
  • while((cingtgtinput))
  • if( (inputlt0) (inputgt100) )
  • cout ltlt"Out of range " ltlt endl
  • else
  • markcount input
  • count

51
  • //PROG3_4 Program demonstrates a
    single-dimensional
  • // dynamic array.
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • int main()
  • int ptr
  • int SIZE
  • coutltltNumber of data?"
  • cin gtgt SIZE
  • ptr new intSIZE //Allocates an array
    dynamically
  • if(!ptr) //Checks for a memory
    allocation error
  • coutltlt"Memory allocation error!"
  • exit(1)

52
Connecting pointer and array
  • Array name is the address of the array (address
    of the first element)
  • Pointer can point to array
  • int array10
  • int ptr
  • ptr array // not ptr array
  • ptr array0 //equivalent to the above

53
  • //Relate a pointer to an array
  • includeltiostreamgt
  • using namespace std
  • int main()
  • int array100
  • int array2 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • int ptr
  • ptr array
  • for( int a0 alt10 a )
  • cout ltlt arraya ltlt"\t" ltlt ptra ltlt"\t" ltlt
    (arraya) ltlt "\t" ltlt (ptra) ltlt endl
  • ptr array2
  • for( int a0 alt10 a )
  • cout ltlt array2a ltlt"\t" ltlt ptra ltlt"\t" ltlt
    (array2a) ltlt "\t" ltlt (ptra) ltlt endl
  • system("pause")
  • return 0

54
Notes on Pointer and Reference
  • Make sure that you do not try to access array
    beyond its boundary (size)
  • Array index starts from zero not one
  • Can not assign one array to another (a b )
  • Char array is a special array
  • Ended with a \0 null character
  • The whole array can be printed at once by using
    stream insertion operator
  • Use class string
  • We will revisit them again when we discuss
    function

55
Summary
  • Pointer is variable containing memory address
  • Reference is variable which is an alias of
    other variable
  • When a pointer points to an array pointer name
    and array name are compatible
  • Dynamic memory allocation is important to write
    efficient program
  • new operator to allocate memory
  • delete operator to de-allocate memory
Write a Comment
User Comments (0)
About PowerShow.com