Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lecture 3 1. Structures 2. Abstract Data Types

2 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

3 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

4 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 2711999

5 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; record1 10 15

6 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; }

7 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

8 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.

9 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

10 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; };

11 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;}

12 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(); }


Download ppt "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."

Similar presentations


Ads by Google