Fundamentals of Programming Session 24

Similar documents
Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

Fundamentals of Programming Session 23

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Programming for Engineers Structures, Unions

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Fundamentals of Programming Session 20

Fundamentals of Programming Session 25

Fundamentals of Programming Session 12

Fundamentals of Programming Session 4

Fundamentals of Programming Session 19

Fundamentals of Programming Session 8

Introduction to C++ Systems Programming

Fundamentals of Programming Session 20

Fundamentals of Programming Session 15

Fundamentals of Programming Session 19

Introduction to Programming

Fundamentals of Programming Session 13

Introduction to Programming

Introduction to Programming

Chapter 10 C Structures, Unions, Bit Manipulations

Fundamentals of Programming Session 7

Fundamentals of Programming Session 9

Introduction to Programming session 24

Fundamentals of Programming Session 14

Chapter 15 - C++ As A "Better C"

Lecture 2 Tao Wang 1

Fundamentals of Programming. Lecture 3: Introduction to C Programming

Tokens, Expressions and Control Structures

Object Oriented Design

Full file at C How to Program, 6/e Multiple Choice Test Bank

Object Oriented Design

Fundamentals of Programming Session 24

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

LECTURE 02 INTRODUCTION TO C++

Variables. Data Types.

typedef int Array[10]; String name; Array ages;

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

Fundamentals of Programming Session 1

CSI33 Data Structures

Fast Introduction to Object Oriented Programming and C++

CSc Introduction to Computing

Dept. of Computer and Information Science (IDA) Linköpings universitet Sweden

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Short Notes of CS201

Fundamental of Programming (C)

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CS201 - Introduction to Programming Glossary By

Review of the C Programming Language for Principles of Operating Systems

Computer Programming : C++

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Fundamentals of Programming Session 1

Lab # 02. Basic Elements of C++ _ Part1

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Chapter 2: Using Data

Structs. Comp Sci 1570 Introduction to C++ Introduction. Aggregate data. Example. General syntax Object initialization Initialization and access

Relationship between Pointers and Arrays

Lexical Considerations

Creating a C++ Program

Data Representation and Storage

Cpt S 122 Data Structures. Introduction to C++ Part II

CSCE 110 PROGRAMMING FUNDAMENTALS

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Data Representation and Storage. Some definitions (in C)

Object oriented programming C++

Input And Output of C++

BITG 1233: Introduction to C++

Flow Control. CSC215 Lecture

Lexical Considerations

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

OBJECT ORIENTED PROGRAMMING USING C++

2 nd Week Lecture Notes

Lecture 7. Log into Linux New documents posted to course webpage

Chapter 8 STRUCTURES IN C

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

COMP322 - Introduction to C++

Fundamental of Programming (C)

VARIABLES AND CONSTANTS

1 Lexical Considerations

Review of the C Programming Language

6.096 Introduction to C++ January (IAP) 2009

Fundamentals of Programming Session 2

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

C Pointers. 7.2 Pointer Variable Definitions and Initialization

C++ Structures Programming Workshop 2 (CSCI 1061U)

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

C: How to Program. Week /Mar/05

4. Structure of a C++ program

REVIEW. The C++ Programming Language. CS 151 Review #2

III. Classes (Chap. 3)

Chapter 2 THE STRUCTURE OF C LANGUAGE

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Functions and Recursion

Transcription:

Fundamentals of Programming Session 24 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology

Outlines Structures Using Structures with Functions typedef C++ Inline Functions 2

Structure Definitions 3 A structure cannot contain an instance of itself. For example, a variable of type struct employee cannot be declared in the definition for struct employee. A pointer to struct employee, however, may be included. For example, struct employee2 { char firstname[ 20 ]; char lastname[ 20 ]; int age; char gender; double hourlysalary; struct employee2 person; /* ERROR */ struct employee2 *eptr; /* pointer */ }; struct employee2 contains an instance of itself (person), which is an error.

Structure Definitions The definition struct card acard, deck[ 52 ], *cardptr; declares acard to be a variable of type struct card, declares deck to be an array with 52 elements of type struct card and declares cardptr to be a pointer to struct card. The preceding definition could have been incorporated into the struct card structure definition as follows: 4 struct card { char *face; char *suit; } acard, deck[ 52 ], *cardptr; Structures may not be compared using operators == and!=, because structure members are not necessarily stored in consecutive bytes of memory.

Initializing Structures Structures can be initialized using initializer lists as with arrays. To initialize a structure, follow the variable name in the definition with an equals sign and a brace-enclosed, comma-separated list of initializers. For example, the declaration struct card acard = { "Three", "Hearts" }; creates variable acard to be of type struct card and initializes member face to "Three" and member suit to "Hearts". 5

Accessing Structure Members 6 The structure pointer operator consisting of a minus (-) sign and a greater than (>) sign with no intervening spaces accesses a structure member via a pointer to the structure. Assume that the pointer cardptr has been declared to point to struct card and that the address of structure acard has been assigned to cardptr. To print member suit of structure acard with pointer cardptr, use the statement printf( "%s", cardptr->suit ); /* displays Hearts */

Accessing Structure Members The expression cardptr->suit is equivalent to (*cardptr).suit, which dereferences the pointer and accesses the member suit using the structure member operator. The parentheses are needed here because the structure member operator (.) has a higher precedence than the pointer dereferencing operator (*). The program of Fig. 10.2 demonstrates the use of the structure member and structure pointer operators. 7

8 Accessing Structure Members

9 Accessing Structure Members

Using Structures with Functions Structures may be passed to functions by passing individual structure members, by passing an entire structure or by passing a pointer to a structure. When structures or individual structure members are passed to a function, they are passed by value. Therefore, the members of a caller s structure cannot be modified by the called function. To pass a structure by reference, pass the address of the structure variable. 10

typedef 11 The keyword typedef provides a mechanism for creating synonyms (or aliases) for previously defined data types. Names for structure types are often defined with typedef to create shorter type names. For example, the statement typedef struct card Card; defines the new type name Card as a synonym for type struct card. C programmers often use typedef to define a structure type, so a structure tag is not required.

typedef For example, the following definition typedef struct { char *face; char *suit; } Card; creates the structure type Card without the need for a separate typedef statement. 12

C++ C++ improves on many of C s features and provides object-oriented-programming (OOP) capabilities that increase software productivity, quality and reusability. This section revisits the addition program of Fig. 2.8 and illustrates several important features of the C++ language as well as some differences between C and C++. C file names have the.c (lowercase) extension. C++ file names can have one of several extensions, such as.cpp,.cxx or.c (uppercase). 13

Header Files 14 The C++ Standard Library is divided into many portions, each with its own header file. The header files contain the function prototypes for the related functions that form each portion of the library. The header files also contain definitions of various class types and functions, as well as constants needed by those functions. Figure 15.2 lists common C++ Standard Library header files.

15 Header Files

16 C++

Inline Functions 17 Implementing a program as a set of functions is good from a software engineering standpoint, but function calls involve execution-time overhead. C++ provides inline functions to help reduce function call overhead especially for small functions. The trade-off is that multiple copies of the function code are inserted in the program (often making the program larger) rather than there being a single copy of the function to which control is passed each time the function is called. The compiler can ignore the inline- qualifier and typically does so for all but the smallest functions.

18 Inline Functions

19 Inline Functions

Inline Functions Lines 4 6 are using statements that help us eliminate the need to repeat the std:: prefix. From this point forward, each C++ example contains one or more using statements. In place of lines 4 6, many programmers prefer to use using namespace std; C++ also provides type bool for representing boolean (true/false) values. The two possible values of a bool are the keywords true and false. 20