Comp151. Definitions & Declarations

Similar documents
Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Chapter 5 Functions. Introducing Functions

7.7 Case Study: Calculating Depreciation

Visual Studio 2008 Express Editions

Chapter 13 Storage classes

CSI33 Data Structures

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Member Functions of the istream Class

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

An Incomplete C++ Primer. University of Wyoming MA 5310

13 Classes & Objects with Constructors/Destructors

Appendix K Introduction to Microsoft Visual C++ 6.0

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Data Structures using OOP C++ Lecture 1

C++ INTERVIEW QUESTIONS

Compiler Construction

Informatica e Sistemi in Tempo Reale

Basics of I/O Streams and File I/O

C++ Language Tutorial

URI and UUID. Identifying things on the Web.

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Passing 1D arrays to functions.

C++ Crash Kurs. C++ Object-Oriented Programming

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Chapter One Introduction to Programming

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

Moving from C++ to VBA

EP241 Computer Programming

Syllabus OBJECT ORIENTED PROGRAMMING C++

Class 16: Function Parameters and Polymorphism

Answers to Review Questions Chapter 7

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Pemrograman Dasar. Basic Elements Of Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

The C Programming Language course syllabus associate level

Conditions & Boolean Expressions

Sequential Program Execution

Computer Programming C++ Classes and Objects 15 th Lecture

CSC230 Getting Starting in C. Tyler Bletsch

Chapter 5. Selection 5-1

An API for Reading the MySQL Binary Log

Note: Syntactically, a ; is needed at the end of a struct definition.

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

5 Arrays and Pointers

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Ch 7-1. Object-Oriented Programming and Classes

How to: Use Basic C++, Syntax and Operators

Illustration 1: Diagram of program function and data flow

Developing an ODBC C++ Client with MySQL Database

CORBA Programming with TAOX11. The C++11 CORBA Implementation

2.2: Bitwise Logical Operations

COMPUTER SCIENCE 1999 (Delhi Board)

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Parameter Passing. Standard mechanisms. Call by value-result Call by name, result

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Stack Allocation. Run-Time Data Structures. Static Structures

Function Overloading I

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

C++FA 5.1 PRACTICE MID-TERM EXAM

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

Chapter 9 Delegates and Events

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

C++FA 3.1 OPTIMIZING C++

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Unit Testing C++ Code CppUnit by Example Venkat Subramaniam

C++ program structure Variable Declarations

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Project 3: Image Enhancement - Spatial vs. Frequency Domain Filters. Steven Young: ECE 572

Chapter 9 Text Files User Defined Data Types User Defined Header Files

CSCI110: Examination information.

SQLITE C/C++ TUTORIAL

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Introduction to Java

Common Data Structures

Formatting Numbers with C++ Output Streams

csce4313 Programming Languages Scanner (pass/fail)

C++ Input/Output: Streams

arrays C Programming Language - Arrays

Moving from CS 61A Scheme to CS 61B Java

ECE 122. Engineering Problem Solving with Java

Appendix M: Introduction to Microsoft Visual C Express Edition

Transcription:

Comp151 Definitions & Declarations

Example: Definition /* reverse_printcpp */ #include <iostream> #include <string> using namespace std; int global_var = 23; // global variable definition void reverse_print(const char* s) // function definition { for (int j = strlen(s) - 1; j >= 0; --j) cout << s[j]; cout << endl; }

Example: Declaration /* use_reverse_printcpp */ #include <iostream> using namespace std; extern int global_var; // external variable declaration extern void reverse_print(const char* s); // external function declaration int main(int argc, const char* argv[]) { float local_var; local_var = 987654; // local variable definition } cout << ''global var = '' << global_var << endl; cout << ''local var = '' << local_var << endl; cout << ''input string backwards = ''; reverse_print(argv[1]);

Definition A definition introduces a variable's or a function's name and type A variable definition reserves a number of bytes of memory for the variable A function definition generates code for the function In both cases, definitions cause the compiler to allocate memory to store the variable or function code An object must be defined exactly once in a program* *Except inline function definitions (which we ll discuss in a moment)

Declaration The declaration of a variable (or function) announces that the variable (or function) exists and is defined somewhere else (in the same file, or in a different file) The connection is made when the object files are linked A variable declaration consists of the variable's name and its type preceded by the keyword extern A function declaration consists of the function prototype (without the function body) preceded by the keyword extern A (forward) class declaration consists of the class name (without the class body) preceded by the keyword class A declaration does not generate code, and does not reserve memory There can be any number of declarations for the same object name in a program (as long as they re consistent with each other) If a declaration is used in a file different from that with the definition of the object, the linker will insert the real memory address of the object instead of the symbolic name In C++, a variable must be defined or declared to the program before it is used

A word on class definitions A class definition defines a type Note that merely defining a type or class does not generate code (The code for any member function of the class is not generated until the compiler sees an individual member function definition) In this sense, class definitions are unlike variable and function definitions, which cause the compiler to allocate memory to store the variable or function code But class definitions are still like variable and function definitions in the sense that a class must be defined exactly once in a program

Advantages of Header Files In general, a header file provides a centralized location for: external object declarations function declarations & member function declarations type definitions class definitions (but not member function definitions, except inline) inline function definitions & inline member function definitions The advantages are: 1 By including the header files, all files of the same piece of software are guaranteed to contain the same declaration for a global object or function 2 Should a declaration require updating, only one change to the header file will need to be made

A question /* C++ code fragment uses Date class from separate compilation lecture */ Date d1(2, 17, 2006); Date d2; d2 = d1; d1set(2, 20, 2006); d2print(); // what gets printed here? /* Java code fragment looks nearly identical to above C++ code fragment */ Date d1 = new Date(2, 17, 2006); Date d2; d2 = d1; d1set(2, 20, 2006); d2print(); // what gets printed here?

Variables A variable is a symbolic name assigned to some memory storage The size of this storage depends on the type of the variable, compiler, and platform eg, on x86 under Windows, char is 1 byte long and int is 4 byte long 2000 2004 354 76 :x :y The difference between a variable and a literal constant is that a variable is addressable

Key distinction: lvalue vs rvalue [ interpretation of " x = x + 1 " ] x: x+1 A variable has dual roles, depending on where it appears in the program, it can represent lvalue: the location of the memory storage rvalue: the value in the storage They are so called because a variable represents an lvalue (or rvalue) if it is written to the left (or right) of an assignment statement Thus, the following are invalid statements in C++: 4 = 1; grade + 10 = new - grade;

Not all languages distinguish rvalues from lvalues the way that C++ does! /* C++ code fragment uses Date class from separate compilation lecture */ Date d1(2, 17, 2006); Date d2; d2 = d1; d1set(2, 20, 2006); d2print(); // prints 20060217 /* Java code fragment looks nearly identical to above C++ code fragment */ Date d1 = new Date(2, 17, 2006); Date d2; d2 = d1; d1set(2, 20, 2006); d2print(); // prints 20060220