Lecture 3 1. Structures 2. Abstract Data Types. STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
C++ Classes & Data Abstraction
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
1 Data Structures Topic #1 Welcome !. 2 Today’s Agenda Introduction...what to expect!?! Talk about our Goals and Objectives Textbook is highly recommended.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Data Structures.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ fundamentals.
1 CSC241: Object Oriented Programming Lecture No 07.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Structured Data Types array array union union struct struct class class.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Array, Structure and Union
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Structured Data Types struct class Structured Data Types array – homogeneous container collections of only one type struct – heterogeneous data type.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Structures and Unions in C Alan L. Cox
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
Programming Techniques Classes II Important Class Features Spring 2009.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
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.
CGS 3460 Thus Far n Whenever we declare a variable, we specified its data type. n The data type helped us identify the type of information that a variable.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
Procedural and Object-Oriented Programming
Chapter 4 Linked Lists.
More about OOP and ADTs Classes
Data Structures and Abstract Data Types
Pointers and References
Dynamic Memory Allocation Reference Variables
CSC 253 Lecture 8.
CSC 253 Lecture 8.
1. Structures 2. Abstract Data Types
More about OOP and ADTs Classes
Pointer to Structures Lesson xx
PZ09A - Activation records
Structured Data Types array union struct class.
Dynamic Memory A whole heap of fun….
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Object oriented programming (OOP) Lecture No. 7
C Programming Lecture-8 Pointers and Memory Management
Java Programming Language
Chapter 9: Pointers and String
The Stack.
C Programming Lecture-14 Unions
Data Structures and ADTs
Types of Computer Languages
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Object Oriented Programming (OOP) Lecture No. 12
Structures Declarations CSCI 230
Presentation transcript:

Lecture 3 1. Structures 2. Abstract Data Types

STRUCTURES WHY ARE STRUCTURES NEEDED? If the predefined types are not adequate to model the object, create a new data type to model it. An Ordered set of elements, not necessarily of the same type. The elements are called members or fields

How to declare it? struct date { int day; int month; int year; } This declares a data type date, which is a structure containing 3 integer fields. struct date today, *pointer; This declares a variable today that can contain 3 fields and a (pointer) variable that can contain the address of a structure of type date. STRUCTURES date DayMonthYear

Cont... today.day = 27; today.month = 1; today.year = 1999; pointer = &today; pointer->year = 1999; How to assign values to structures? Use ‘.’ operator to access the members Use ‘->’ operator, when using pointers date

Structures Structures containing pointers: struct node { int number, int *ptr; } Defines a structure type that contains an integer and an address. struct node record1, record2; int a,b,c: a = 10; b = 15; record1.number = a; record1.ptr = &b; record

Difference Between Structures and Unions? Members of the structure are allocated different memory locations Members of the Union share memory (ie) they all share the same memory address Syntax : Union unionname { members; }

Procedural Programming VS OOP 1) The focus of OOP is thus on OBJECTS rather than on subprograms 2) Objects carry out their own operations around with them instead of calling External functions

ABSTRACT DATA TYPES WHAT IS ADT? If we identify the 1) Data items 2) Operations that must be performed on them then we call it as ADT. Eg: lists, stacks, queues and trees. In C++ the concept of a class is ideally suited to define an ADT.

ADT What is a CLASS? 1) Similar to a Struct 2) Used to model Objects with different attributes Difference : 1) Members of structure are by Default PUBLIC and can be accessed by a non member function via ‘.’ operator. 2) Members of a class are by Default PRIVATE and cannot be accessed by non member functions unless explicitly declared as public Class Data Members Member Functions

ADT Class classname { Public : declarations of public members Private : //optional declarations of private members }; Example : #include class Patient { public: Patient(); void SetDetails (int,char); void DisplayDetails(); private: int IdNumber; char Name; };

ADT So the new data type is Patient. Operations allowed on the data type is: Patient::Patient() { cout << "Allocating memory" <<endl;} void Patient::SetDetails (int IdNumberin, char Namein) {IdNumber = IdNumberin; Name = Namein; } void Patient::DisplayDetails() {cout << IdNumber<<" "<<Name<<endl;}

ADT How do we use the Class? By creating an instance of the class void main() { Patient p1,p2; p1.Setdetails(1111,'x'); p2.Setdetails (2222,'y'); p1.Displaydetails(); p2.Displaydetails(); }