ECE 1211: Object Oriented Programming for Engineers - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

ECE 1211: Object Oriented Programming for Engineers

Description:

To introduce the concept of Object Oriented Programming and design ... declaration of object cin, cout, cerr and clog that we used for I/O operation ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 55
Provided by: afidalin
Category:

less

Transcript and Presenter's Notes

Title: ECE 1211: Object Oriented Programming for Engineers


1
ECE 1211 Object Oriented Programming for
Engineers
2
Todays lecture
  • Course Outline
  • Course Schedule
  • Course Policy

3
Course Objectives
  • To introduce the concept of Object Oriented
    Programming and design
  • To develop programming skills based on the Object
    Oriented paradigm using C
  • To apply the Object Oriented Programming concept
    in engineering area

4
Learning Outcomes
  • Able to explain the concept of Object Oriented
    Programming
  • Able to outline and apply the concepts of
    encapsulation, inheritance and polymorphism
  • Able to design, code, test and debug simple
    program in Object Oriented Programming language
  • Able to develop a simple project in a group

5
I dont expect you to be like this!
6
Assessment
  • Assignments (15) and Quizzes (10) 25
  • Project
    35
  • Mid-term Examination 20
  • Final Examination
    20
  • TOTAL
    100

7
Course Policy
  • Please abide to all rules and regulations
  • Attendance is compulsory
  • 10 - warning letter, 20 barred from
    examination
  • All information and materials
  • http//staff.iiu.edu.my/afidalina
  • Assignment must be submitted before the deadline.
    No late submission will be entertained. Both
    SOURCE and DESTINATION deserve ZERO mark
  • I will not consider any absence from Mid
    Term/Project etc without any PRIOR notice
  • I could hardly accept weird/illogical reasons
  • Because that will reflect your attitude and
    commitment
  • Class Representative
  • Text book

8
ICE BREAKING!
9
Quiz 1
10
Lets start our 1st real lecture
Introduction to C Part 1
11
Introduction to C
12
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

13
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

14
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

15
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

16
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)

17
C and C
  • C is a superset of C

C
C
18
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.)

19
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.

20
  • / Welcome to C /
  • include ltiostreamgt
  • include ltstringgt
  • //.h extension is not required for standard lib.
  • using namespace std
  • // std namespace
  • int main()
  • 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

21
  • //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 )
  • return 0

22
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)

23
  • // 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
  • return 0

24
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

25
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
  • return 0

26
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)

27
  • // 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
  • cout ltlt aa ltlt endl
  • const_castltintgt(aa) 7
  • cout ltlt aa ltlt endl

28
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

29
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)

30
  • // 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
  • return 0

31
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

32
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
  • Refer to references for more detailed explanation

33
  • //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

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

35
  • //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

36
  • //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

37
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

38
Pointers, References and Dynamic Memory
Allocation and Array
39
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

40
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

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

42
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

43
  • // 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

0x200
x
0x220
44
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

45
  • /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

46
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

47
  • //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

48
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

49
  • // 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

50
  • //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)
  • for(int i0 iltSIZE i)

51
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

52
  • //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

53
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

54
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