LESSON 06.

Slides:



Advertisements
Similar presentations
Structure.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
Dale/Weems/Headington
True or false A variable of type char can hold the value 301. ( F )
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Overview of Previous Lesson(s) Over View  C++  KeywordsReserved words  IdentifiersProgrammers defined variables  Variables A named storage location.
Basic Elements of C++ Chapter 2.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
C++ Programming: Basic Elements of C++.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
Simple Data Types Built-In and User Defined Chapter 10.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.
Programming Fundamentals Enumerations and Functions.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
Overview of Previous Lesson(s) Over View  I nteractive development environment is a software application that provides comprehensive facilities to computer.
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Control Statements
Computer Programming BCT 1113
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Fundamentals
Basic Elements of C++.
Programming Structures.
Pointers and Pointer-Based Strings
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Chapter 3: Understanding C# Language Fundamentals
Chapter 2: Basic Elements of C++
Basic Elements of C++ Chapter 2.
Starting JavaProgramming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
FUNCTIONS& FUNCTIONS OVERLOADING
Chapter 7 Additional Control Structures
Review for Final Exam.
Variables, Identifiers, Assignments, Input/Output
CS150 Introduction to Computer Science 1
Review for Final Exam.
Chapter 2: Introduction to C++.
Overloading functions
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Pointers and Pointer-Based Strings
What Actions Do We Have Part 1
C++ Programming Basics
C Language B. DHIVYA 17PCA140 II MCA.
Structures Chapter 4.
Presentation transcript:

LESSON 06

Overview of Previous Lesson(s)

Over View Interactive development environment is a software application that provides comprehensive facilities to computer programmers for software development.  Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft Corporation based on .Net framework

Over View.. C++ Keywords Reserved words Identifiers Programmers defined variables Variables A named storage location in the computer’s memory for holding a piece of data. Data Types When computer programs store data in variables, each variable must be assigned a specific data type. Some common data types include integers, floating point numbers, characters, strings, and arrays.

Over View... Looping structures Decision structures For loop While Do While Decision structures If If / else Switch

Over View... Example expression 2 + 2 * 2 – 2 Operator Precedence Operator precedence orders the operators in a priority sequence. In an expression with more than one operator, evaluate in this order: - (unary negation), in order, left to right * / %, in order, left to right + -, in order, left to right Example expression 2 + 2 * 2 – 2 evaluate third evaluate second evaluate first

Over View… Type Conversion and Casting Implicit type conversion: When an expression involving variables of different types then for each operation to be performed, the compiler has to arrange to convert the type of one of the operands to match that of the other. Any pair of operands of different types, the compiler decides which operand to convert to the other considering types to be in the following rank from high to low:

Over View… Explicit Type Conversion: A forced conversion from one type to another is referred as explicit type conversion. Auto keyword is used as the type of a variable in a definition statement and have its type deduced from the initial value. auto n = 16; // Type is int auto pi = 3.14159; // Type is double auto x = 3.5f; // Type is float auto found = false; // Type is bool

Over View… Bitwise operators: L Values and R Values: An lvalue refers to an address in memory in which something is stored on an ongoing basis. An rvalue is the result of an expression that is stored transiently.

Over View… Scope

Over View… Namespaces provide a way to separate the names used in one part of a program from those used in another. namespace keyword is used to declare a namespace namespace myStuff { // Code that I want to have in the namespace myStuff... } This defines a namespace with the name myStuff .

TODAY’S LESSON

Contents Structures Enumerations Functions

Structures A structure is a collection of simple variables. The variables in a structure can be of different types: Int, float, double, long.. etc The data items in a structure are called the members of the structure. A structure is a collection of data, while a class is a collection of both data and functions

A Simple Structure The structure is a kind of blueprint specifying what information is necessary for a single part. Following structure represents an item in a Machines company’s parts inventory. // parts.cpp // uses parts inventory to demonstrate structures #include <iostream> using namespace std;

Part Structure struct part //declare a structure { int modelnumber; //ID number of Machine model int partnumber; //ID number of Machine part float cost; //cost of part {;

Main Program int main() { part part1; //define a structure variable part1.modelnumber = 6244; //give values to structure members part1.partnumber = 373; part1.cost = 217.55F; //display structure members cout << “Model “ << part1.modelnumber; cout << “, part “ << part1.partnumber; cout << “, costs $” << part1.cost << endl; return 0; }

Part Structure.. The program’s output looks like this: Model 6244, part 373, costs $217.55 The PART program has three main aspects: Defining the structure. Defining a structure variable. Accessing the members of the structure.

Structure Syntax

Use of Structure Definition The structure definition serves only as a blueprint for the creation of variables of type part. It does not itself create any structure variables. So no space in memory is occupied by the structure definition. This is unlike the definition of a simple variable, which does set aside memory. A structure definition is merely a specification for how structure variables will look when they are defined.

Use of Structure Definition

Structure Variable part part1; //define a structure variable This definition reserves space in memory for part1. How much space? Enough to hold all the members of part1. Memory space in our example (assuming a 32-bit system) 4 bytes for each of the two ints 4 bytes for the float.

Memory Occupied

Accessing Structure Variable Once a structure variable has been defined, its members can be accessed using the dot operator. part1.modelnumber = 6244; Access operator

Accessing Structure Variable

Enumeration A different approach to defining your own data type other than structure is enumeration. To cope the need for variables that have a limited set of possible values. Ex, It can be usefully referred to by labels the days of the week or months of the year. enum Week{Mon, Tues, Wed, Thurs, Fri, Sat, Sun} thisWeek;

Enumeration.. An enum declaration defines the set of all names that will be permissible values of the type. These permissible values are called enumerators. The enum type Week has seven enumerators: Sun, Mon, Tue, and so on, up to Sat.

Enumeration.. // dayenum.cpp // demonstrates enum types #include <iostream> using namespace std; //specify enum type enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int main() { days_of_week day1, day2; //define variables of type days_of_week

Enumeration.. day1 = Mon; //give values to day2 = Thu; //variables int diff = day2 - day1; //can do integer arithmetic cout << “Days between = “ << diff << endl; if(day1 < day2) //can do comparisons cout << “day1 comes before day2\n”; return 0; }

Enumeration.. Program Output Days between = 3 day1 comes before day2

Functions A function groups a number of program statements into a unit and gives it a name. This unit can then be invoked from other parts of the program. The function’s code is stored in only one place in memory, even though the function is executed many times in the course of the program.

Functions..

A Simple Function Our first example demonstrates a simple function whose purpose is to print a line of 45 asterisks. // table.cpp // demonstrates simple function #include <iostream> using namespace std; void starline(); //function declaration // (prototype)

A Simple Function int main() { starline(); //call to function cout << “Data type Range” << endl; starline(); //call to function cout << “char -128 to 127” << endl << “short -32,768 to 32,767” << endl << “int System dependent” << endl << “long -2,147,483,648 to 2,147,483,647” << endl; return 0; }

A Simple Function // starline() // function definition void starline() //function declarator { for(int j=0; j<45; j++) //function body cout << ‘*’; cout << endl; }

Program Output

Function Syntax

Function Components

Thank You