0% found this document useful (0 votes)
8 views12 pages

Lec2 1

Uploaded by

sametaleb12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Lec2 1

Uploaded by

sametaleb12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

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

[email protected]
STRUCTURES

How to declare it?


struct date {
int day; Day Month Year
int month;
int year; } date
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.
[email protected]
Cont...

How to assign values to structures?

27 1 1999
today.day = 27;
date
today.month = 1;
pointer = &today;
today.year = 1999;
pointer->year = 1999;

Use ‘.’ operator to access the Use ‘->’ operator, when


members using pointers

[email protected]
Structures
Structures containing pointers: 10 15

struct node {
int number,
10
int *ptr;
}
record1
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;

[email protected]
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; }
[email protected]
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

[email protected]
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 conceptof a class is ideally suited to
define an ADT .
[email protected]
ADT
What is a CLASS?
1) Similar to a Struct
Data Members
2) Used to model Objects with different attributes
Member Functions
Difference :
1) Members of structure are by Default PUBLIC and can Class
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

[email protected]
ADT
Class classname Example :
#include <iostream.h>
{
Public : class Patient
declarations of public {
members public:
Patient();
Private : //optional
void SetDetails (int,char);
declarations of private void DisplayDetails();
members private:
int IdNumber;
};
char Name;
[email protected]
};
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;}

[email protected]
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();
[email protected]
}

You might also like