0% found this document useful (0 votes)
83 views18 pages

C++ Structures: Access, Init, Copy

This document discusses operations on structures in C++. It covers accessing members of structures using the "." operator, initializing structures during declaration, and copying the values of one structure object to another. Examples are provided for a library management system using Book and Patron structures.

Uploaded by

siriuslot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topics covered

  • Structures in C++,
  • C++ Object Access,
  • Structure Copying,
  • IIT Bombay,
  • Book Structure,
  • Library System Operations,
  • C++ Object Management,
  • C++ Programming Techniques,
  • Structure Initialization,
  • Member Functions
0% found this document useful (0 votes)
83 views18 pages

C++ Structures: Access, Init, Copy

This document discusses operations on structures in C++. It covers accessing members of structures using the "." operator, initializing structures during declaration, and copying the values of one structure object to another. Examples are provided for a library management system using Book and Patron structures.

Uploaded by

siriuslot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Topics covered

  • Structures in C++,
  • C++ Object Access,
  • Structure Copying,
  • IIT Bombay,
  • Book Structure,
  • Library System Operations,
  • C++ Object Management,
  • C++ Programming Techniques,
  • Structure Initialization,
  • Member Functions

IIT Bombay

Computer Programming
Dr. Deepak B Phatak
Dr. Supratik Chakraborty
Department of Computer Science and Engineering
IIT Bombay
Session: Simple operations on structures

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Quick Recap of Relevant Topics


IIT Bombay

Brief introduction to object-oriented programming


Defining structures in C++

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Overview of This Lecture


IIT Bombay

Accessing members of structures


Initializing and copying structures

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Acknowledgment
IIT Bombay

Some examples in this lecture are from


An Introduction to Programming Through C++
by Abhiram G. Ranade
McGraw Hill Education 2014
All such examples indicated in slides with the citation
AGRBook

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recall: Library Information Management System


[Ref. AGRBook]

IIT Bombay

We want to design a book check out/return/claim management


system of a small library
How does the system work?

Every patron has a numerical id


Every book has an accession number
Check out: A patron can check out upto 3 books at any time
Claim: If X has not already checked out 3 books, she can claim a book
checked out by Y
When Y returns the book, it is held for X and cannot be lent to others
Return: A patron can return a book checked out by her at any time
No late charges!
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recall: Structures in C++


IIT Bombay

Structures group a set of variables/arrays of possibly different


data types together
struct Book {
char title[50];
char authors[500];
double price;
int accNum;
bool checkOutStatus;
int claimantId;
};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

struct Patron {
char name[50];
char address[100];
int uniqueId;
int numBooksChkOut;
int claimdBookAccNum;
};
6

Recall: Structures in C++


IIT Bombay

Structures group a set of variables/arrays of possibly different


data types together
Member
of
struct Patron {
struct Book {
structure
char name[50];
char title[50];
Book
char address[100];
char authors[500];
int uniqueId;
double price;
int numBooksChkdOut;
int accNum;
Member
int claimdBookAccNum;
bool checkOutStatus; of
};
int claimantId;
structure
};
Patron
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Recall: Structures in C++


IIT Bombay

Variables and arrays of structure types can be declared

Book libraryShelf[1000];
Book myChoice, yourChoice;
Patron libraryPatrons[200];
Patron currentPatron, prevPatron;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

Accessing Members of Structures


IIT Bombay

How do we access the member


named price of the object
myChoice of (structure) type
Book ?
C++ provides the . operator for
this:
myChoice.price
accesses the member named
price of the object myChoice
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

struct Book {
char title[50];
char authors[500];
double price;
int accNum;
bool checkOutStatus;
int claimantId;
};
Book myChoice;
9

Accessing Members of Structures


IIT Bombay

myChoice.price
can be used in a program like any
other double variable

struct Book {
char title[50];
char authors[500];
double price;
int accNum;
bool checkOutStatus;
int claimantId;
};

Example program statements using


myChoice.price
cin >> myChoice.price;
myChoice.price += 20;
Book myChoice;
cout << Rs. << myChoice.price;
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

10

Accessing Members of Structures


IIT Bombay

currPatron.name
struct Patron {
can be used in a program like any
char name[50];
other character array
char address[100];
Example program statements using
int uniqueId;
currPatron.name
int numBooksChkdOut;
if (currPatron.name*0+ == S) ,
int claimdBookAccNum;
};
cout << Patron name: ;
cout << currPatron.name << endl;
Patron currPatron;
}
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

11

Initializing Structures
IIT Bombay

Recall declaring and initializing variables of simple data


types
int index = 0;
char command = x;
Can we do similar initialization for structures?

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

12

Initializing Structures
IIT Bombay

struct Patron {
char name[50];
char address[100];
int uniqueId;
int numBooksChkdOut;
int claimdBookAccNum;
};

Patron currPatron =
,Shashi Dev, IIT Bombay, India, 2345, 0, -1};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

13

Initializing Structures
IIT Bombay

struct Patron {
currPatron objects members:
char name[50];
name: Shashi Dev
char address[100];
address: IIT Bombay, India
int uniqueId;
uniqueId: 2345
int numBooksChkdOut;
numBooksChkdOut: 0
int claimdBookAccNum;
claimdBookAccNum: -1
};

Patron currPatron =
,Shashi Dev, IIT Bombay, India, 2345, 0, -1};
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

14

Copying Structures
IIT Bombay

Recall copying one variable to another for simple data types

int i, j;
i = 27;
j = i;
Can we similarly copy one object of a structure type to
another object of the same structure type?
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

15

Copying structures
IIT Bombay

Patron currPatron, prevPatron;


currPatron = ,Shashi Dev, IIT Bombay, India, 2345, 0, -1};
prevPatron = currPatron;

Each member of the object currPatron is copied to the


corresponding member of the object prevPatron
after executing prevPatron = currPatron;

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

16

Copying structures
IIT Bombay

Patron currPatron, prevPatron;


currPatron = ,Shashi Dev, IIT Bombay, India, 2345, 0, -1}
prevPatron = currPatron;
currPatron before copying
name: Shashi Dev
address: IIT Bombay, India
uniqueId: 2345
numBooksChkdOut: 0
claimdBookAccNum: -1
Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

prevPatron after copying


name: Shashi Dev
address: IIT Bombay, India
uniqueId: 2345
numBooksChkdOut: 0
claimdBookAccNum: -1
17

Summary
IIT Bombay

The . operator to access members of structures


Initializing structures
Copying structures

Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay

18

You might also like