Docsity

Understanding Structures, Abstract Data Types and Classes in Programming, Slides of Programming for Engineers

Partial preview of the text

Download Understanding Structures, Abstract Data Types and Classes in Programming and more Slides Programming for Engineers in PDF only on Docsity!

WEEK-

STRUCTURE, ADT and

CLASSES

Structure

• When we handle real world data, we don’t

usually deal with little atoms of information

by themselves—things like integers,

characters and such.

• Instead we deal with entities that are

collections of things, each thing having its

own attributes, just as the entity we call a

ā€˜book’ is a collection of things such as title,

author, call number, publisher, number of

pages, date of publication, etc.

  • A structure contains a number of data types

grouped together.

  • These data types may or may not be of the

same type.

Structure

• A Structure is a container, it can hold a bunch of

things.

– These things can be of any type.

• Structures are used to organize related data

(variables) in to a nice neat package.

Structure Members

• Each thing in a structure is called member.

• Each member has a name, a type and a value.

• Names follow the rules for variable names.

• Types can be any defined type.

Example Structure Definition

struct StudentRecord {

char name; // student name

double hw[3]; // homework grades

double test[2]; // test grades

double ave; // final average

Structure

• Structure-sometimes referred to as

aggregates are collections of related

variables under one name.

• Structure may contain variables of many

different data types.

• Keyword struct introduces a structure

definition.

Structures: Example...

struct Date

{

int day; int month; int year;

};

Date birthday = {12, 4, 1963};

Members of Different Types

struct Student { ... id; ... name; ... age; ... gender; }; student ali; ali.id = 1234;

strcpy(ali.name,ā€Mohammad Aliā€);

ali.age = 19; ali.gender = 'M';

The members of a struct need not be of the same type.

What should be the types of these members?

1234 ā€œMohammad Aliā€ 19 ā€žMā€Ÿ

Assignment to Structure Variable

  • the value of a structure variable can be assigned to

another structure variable of the same type, e.g :

Date newBabyBirthday = today;

Student topStudent; topStudent = fred;

  • assignment is the only operation permitted on a

structure variable as a whole.

Examples of Common Errors in

Accessing Structures

cout << '\n' << year; // Error!

// year is not a variable. It is only the name of // a member in a structure

cout << '\n' << Date.year; // Error!

// Date is not the name of a variable. It is the name of a type

Common Errors in Accessing

Structures

cout << '\n' << today;

//cout does not know how to handle the variable

today, as it is not one of the built-in types. You

have to give it individual bits of today that it can recognize and handle

cout << today.year << today.month;

//this is OK

Accessing struct s in struct s

cout << '\n'

<< (anOrder.orderDate).month;

cin >> (anOrder.orderDate).year;

cin >> anOrder.productNumber;

anOrder

123456 true 20 6 1996

note : this expression access the Date portion of the variable

Passing Structure Variables as Parameters

  • an individual structure member may be passed as a

parameter to a function, e.g. :

  • validMonth(birthday.month);
  • an entire structure variable may be passed , e.g. :
  • validDate(birthday);