Lec2 1
Lec2 1
1. Structures
[email protected]
STRUCTURES
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...
27 1 1999
today.day = 27;
date
today.month = 1;
pointer = &today;
today.year = 1999;
pointer->year = 1999;
[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
[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.
[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]
}