Structures Chapter 4.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
C Programming Lecture 23 Enumeration Types Structures.
Structure.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
True or false A variable of type char can hold the value 301. ( F )
1 Types The type of a variable represents a set of values that may be assigned to the variable. For example, an integer variable is one that may take the.
1 Lab Session-III CSIT121 Fall 2000 Setting Your Own Paths Setting Project Information Browse Information Generation Data types revisited Enumerated data.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
1 Structures. Structure (struct) Definition A Structure is a container, it can hold a bunch of things. –These things can be of any type. Structures are.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Chapters 1-5 Review C++ Class. Chapter 1 – the big picture Objects Class Inheritance Reusability Polymorphism and Overloading.
Array, Structure and Union
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
Data Types Declarations Expressions Data storage C++ Basics.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 CSC241: Object Oriented Programming Lecture No 02.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Enumeration.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
CS 1430: Programming in C++.
1 Lecture10: Structures, Unions and Enumerations 11/26/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Enumerated Types Mr. Dave Clausen La Cañada High School.
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
COMP102 Lab 111 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Programming Fundamentals Enumerations and Functions.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Enumeration Types Managing Ordered Sets. Enumeration types are designed to increase the readability of programs Used to define a set of named constants.
5 Day Forecast Mon Tues Wed Thu Fri.
LESSON 06.
Enumerated Data Types Data type created by programmer
Pointers, Enum, and Structures
Programming Structures.
(Structures,Unions, Enumerations )
Visit for more Learning Resources
DATA HANDLING.
Buy book Online -
C Arrays Systems Programming.
Data Types Chapter 8.
Visit for more Learning Resources
Structures and Union.
Chapter 9: Records (structs)
Chapter 9: Records (structs)
Chapter 10: Records (structs)
Chapter 9: Records (structs)
MON TUE WED THU
Review for Final Exam.
Windows Programming Lecture 04
Structure As Function Arguments
Chapter 1: Introduction to Data Structures(8M)
Programming Assignment #1 12-Month Calendar—
2008 Calendar.
Objectives In this chapter, you will: - Learn about records (structs) - Examine operations on a struct - Manipulate data using a struct - Learn about the.
Review for Final Exam.
Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Structures and Union.
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
Chapter 9: Records (structs)
2008 Calendar.
CS100A Lect. 12, 8 Oct More About Arrays
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

Structures Chapter 4

Structures Variables of simple data types such as float , char, and int represent one item of information A structure is collection of simple variables. Variables can be of different types Data items in structure called members of the structure A structure is collection of data while a class is collection of both data and functions Members of structure are public by default while in class members are private by default

Syntax of Structure Syntax Example Struct structure-name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } struct-variable; Struct part { int modelnumber; int partnumber; float cost; } part1;

Example ////////////////////////////////////// struct part { int modelnumber; int partnumber; float cost; }; int main() part part1; part1.modelnumber = 6244; part1.partnumber = 373; part1.cost = 217.55; cout<<“Model: ”<< part1.modelnumber; cout<<“, Part: ”<< part1.partnumber; cout<<“, cost $: ”<< part1.cost; return 0; }

structures The structure definition is only blue print for creation of variables Defining a structure variable reserves space in memory part part1; part part2; is similar when we are defining a variable e.g. int var1 Members of structures are accessed by its variable following dot operator (member access operator) part1.modelnumber = 6244

Cont’d Another way to initialize structure members are part part1 = {6244,373,217.55}; One structure variable can be assigned to another part2 = part1; The value of each member of part1 is assigned to corresponding member of part2 One structure variable can assigned to another only when they are of the same structure type

Structure within structures Structures can be nested within other structures Example struct Distance { int feet; float inches; }; ////////////////////////////////// struct Room Distance length; Distance Width; } int main() { Room dining; dinning.length.feet = 13; dinning.length.inches =6.5; dinning.width.feet = 10; dinning.width.inches =0.0; Float l = dinning.length.feet + dinning.length.inches/12; Float w= dinning.width.feet + dinning.width.inches/12; Cout<<“Dining room area is ”<< l*w; return 0; }

Enumeration Another way to define own data type is enumeration Enumerated types work when you know in advance a finite list of values that a data type can take on e.g. enum days_of_weeks { Sun, Mon, Tue, Wed, Thu, Fri, Sat}; An enum defines a set of all names that will be permissible values of type. These permissible values are called enumeration Also enumeration is list of values and each value has its specific name

Cont’d Variable can be defined like in structure days_of_week day1, day2; These variables can be given any of the values listed in the enum declaration. E.g. day1 = Mon; , day2 = Thu; day1 = halloween; // illegal Enumerations are treated internally as integers First name in list is given value 0, next name is 1 and so on.

Example enum days_of_week{Sun, Mon, Tue, Wed, Thu, Fri, Sat}; int main() { days_of_week day1, day2; day1 = Mon; day2 = Thu; int diff = day2 – day1; // can do integer arithmetic cout<<“days between = ”;<<diff<<endl; if(day1<day2) cout<<“day1 comes before day2 ”; return 0; }