Lecture 3. The syntax for accessing a struct member is

Similar documents
Enumerated Types. Mr. Dave Clausen La Cañada High School

12 CREATING NEW TYPES

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

by: Lizawati, Norhidayah & Muhammad Noorazlan Shah Computer Engineering, FKEKK, UTeM

Structures and Pointers

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

PASCAL. PASCAL, like BASIC, is a computer language. However, PASCAL, unlike BASIC, is a Blocked Structured Language (BASIC is known as unstructured).

car object properties data red travels at 50 mph actions functions containing programming statements Program Program variables functions

Objectives. Describe ways to create constants const readonly enum

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

BLM2031 Structured Programming. Zeyneb KURT

Data Types and Variables in C language

22c:111 Programming Language Concepts. Fall Types I

Lecture10: Structures, Unions and Enumerations 11/26/2012

Object-Oriented Principles and Practice / C++

Lecture Notes on Programming Languages

LECTURE 11 STRUCTURED DATA

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Chapter 11: Structured Data

Introduction to Computers and Programming

Francesco Nidito. Programmazione Avanzata AA 2007/08

Programming in C. What is C?... What is C?

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Arrays III and Enumerated Types

Chapter 7: Structuring the Data. Lecture7 1

Preview from Notesale.co.uk Page 6 of 52

Chapter 21: Introduction to C Programming Language

The syntax of structure declaration is. struct structure_name { type element 1; type element 2; type element n;

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Introduction to C Final Review Chapters 1-6 & 13

Basic Elements of C. Staff Incharge: S.Sasirekha

Chapter 11: Structured Data

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

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

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

IV Unit Second Part STRUCTURES

Private Swimming Lessons

Department of Computer Science and Engineering. Programming for Problem Solving. I Year B.Tech. (I - Sem) Assistant Professor (M.

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

CS349/SE382 A1 C Programming Tutorial

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Industrial Programming

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CHAPTER : 9 FLOW OF CONTROL

Arrays. Systems Programming Concepts

For each of the following variables named x, specify whether they are static, stack-dynamic, or heapdynamic:

Chapter-8 DATA TYPES. Introduction. Variable:

UNIVERSITY OF SWAZILAND SUPPLEMENTARY EXAMINATION, JULY 2013

AIMMS Function Reference - Date Time Related Identifiers

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Input And Output of C++

CSCE 314 Programming Languages

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Introduction To C#.NET

Maintaining the Central Management System Database

Chapter-14 STRUCTURES

Data Types. Chapter 8

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Programming in Haskell Aug-Nov 2015

UNIT-IV. Structure is a user-defined data type in C language which allows us to combine data of different types together.

Arrays (Lists) # or, = ("first string", "2nd string", 123);

More non-primitive types Lesson 06

Syntax to define a Structure: struct structurename { datatype membername1; datatype membername2;... } ; For Example:

Values, types and variables

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Semantic actions for declarations and expressions

Topic 5: Enumerated Types and Switch Statements

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

Scheduling. Scheduling Tasks At Creation Time CHAPTER

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Announcements. 1. Forms to return today after class:

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Variables. Data Types.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

CS 485 Advanced Object Oriented Design. Enum. Spring 2017

Motivation. Each distinct type needs its own I/O library INTEGER FLOAT CHARACTER BOOLEAN. Using pure numbers, we can write nonsense:

ALQUDS University Department of Computer Engineering

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Programming Fundamentals (CS-302 )

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

JavaScript. Like PHP, JavaScript is a modern programming language that is derived from the syntax at C.

UNIT-V. Structures. The general syntax of structure is given below: Struct <tagname> { datatype membername1; datatype membername2; };

Complex data structures. Cedric Saule

Structures Unions and Enumerated Datatypes 224

Lecture 12. Data Types and Strings

1.00 Lecture 9. Arrays

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Introduction Primitive Data Types Character String Types User-Defined Ordinal Types Array Types. Record Types. Pointer and Reference Types

DigiPen Institute of Technology

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Michele Van Dyne Museum 204B CSCI 136: Fundamentals of Computer Science II, Spring

Fundamental of Programming (C)

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSCI312 Principles of Programming Languages!

Systems Optimization Spring 2003 Mid Term Exam

Chapter 2: Objects and Primitive Data

Transcription:

Lecture 3 Structures: Structures are typically used to group several data items together to form a single entity. It is a collection of variables used to group variables into a single record. Thus a structure (the keyword struct is used in C++) is used. Keyword struct is a datatype, like the following C++ data-types ( int, float, char, etc... ). This is unlike the array, which all the variables must be the same type. The data items in a structure are called the members of the structure. For example: struct product int weight; double price; ; The syntax for accessing a struct member is 1

The dot (.) is an operator, called the member access operator. This example uses parts inventory to demonstrate structures. struct Data int x; int y; ; Data D; D.x = 21; D.y = 23; cout<<d.x<<endl<<d.y; Example Write program to input and print student s data (name,number and address) using struct void main () 2

struct student int no; char name; char address; ; student st; cout<<"enter the student number:\n"; cin>>st.no; cout<<"enter the student name:\n"; cin>>st.name; cout<<"enter the student address:\n"; cin>>st.address; cout<<"student number ="<<st.no<<"\n"; cout<<"student name ="<<st.name<<"\n"; cout<<"student address ="<<st.address<<"\n"; 3

Functions and Structures: A structure can be passed to a function as a single variable. The scope of a structure declaration should be an external storage class whenever a function in the main program is using a structure data types. The field or member data should be same throughout the program either in the main or in a function. For example struct Data int x; int y; void print() cout<<x<<" "<<y<<"\n"; ; Data D; cout<<"plz enter two numbers\n"; cin>>d.x>>d.y; D.print(); 4

Initialize structure You can initialize a structure at the time that it is declared. To give a structure variable a value, follow it by an equal sign and a list of the member values enclosed in braces. For example, struct Data int x; int y; void print() cout<<x<<" "<<y<<"\n"; ; Data D1 = 11,22; Data D2 = D1; D1.print(); D2.print(); 5

Enumerated Data Types: The enumerated data type is a programmer-defined type that is limited to a fixed list of values. A specifier gives the type a name and specifies the permissible values definitions then create variables of this type. Internally, the compiler treats enumerated variables as integers. Structures should not be confused with enumerated data type. Structures are a powerful and flexible way of grouping a diverse collection of data into a single entity. Where enum is a keyword for defining the enumeration data type and the braces are essential. The members of the enumeration data type are the indivisual identifiers. Once the enumeration data type is defined, it can be declared in the following ways: Storage_class enum user_defined_name var.1, var.2, var.n where the storage class is optional. For example: 1) Enum sample Mon, tue, wed, thu, fri, sat, sun ; Enum sample day1, day2, day3; 2) Enum drinks Cola, tea, koffi,rani Enum drinks d1, d2, d3; 3) Enum games 6

Tennis, chess, football, swimming, walking ; Enum games student, staff; The EDT declaration can be written in a single declaration as: Enum sample Mon, tue, wed, thu, fri, sat, sun Day1, day2, day3; Which is exactly equivalent to: 1) Enum sample Mon, tue, wed, thu, fri, sat, sun day1; Enum sample Day2, day3; 2) Enum sample Mon, tue, wed, thu, fri, sat, sun ; Enum sample Day1; Enum sample Day2; Enum sample Day3; The enumeration constants can be assigned to the variable like day1=mon;. The enumeration constants are automatically assigned to integers starting from 0, 1, 2 etc. up to the last number in the enumeration. Example 1: Write C++ program to declare the EDT and to display the integer values on the screen. enum sample mon, tue, wed, thu, fri, sat, sun day1, day2, day3, day4, day5, day6, day7; day1=mon; day2=tue; day3=wed; 7

day4=thu; day5=fri; day6=sat; day7=sun; cout<<"monday = "<<day1 <<endl; cout<<"tuesday = "<<day2 <<endl; cout<<"wednesday = "<<day3 <<endl; cout<<"thursday = "<<day4 <<endl; cout<<"friday = "<<day5 <<endl; cout<<"saturday = "<<day6 <<endl; cout<<"sunday = "<<day7 <<endl; These integers are normally chosen automatically but they can also be specified by the programmer with negative or positive numbers, for example Enum sample mon, tue, wed=10, thu, fri, sat=-5, sun day1, day2, day3, day4, day5, day6, day7; The C++ compiler assigns the enumeration constants as Monday = 0 Tuesday = 1 Wednesday = 10 Thursday = 11 8

Friday = 12 Saturday = -5 Sunday =-4 Ex:-Write C++ program to declare the EDT and to display the difference between days. enum daysofweek mon, tue, wed, thu, fri, sat, sun day1, day2; day1=mon; day2=thu; int diff=day2-day1; cout<<"days between="<<diff<<endl; if (day1 < day2) cout<<"day1 comes before day2 \n"; 9