SlideShare a Scribd company logo
1 of 11
Course:
Object Oriented Programming
STRUCTURES
 Collection of variables referenced with one name
 A structure declaration is a blueprint (i.e., does not
allocate memory); often put in header files
struct tag
{
variable declarations;
};
 Creates a new type specifier (i.e., a new data type)
 Variable declarations may include any type except the
struct-type being created
 Each element or variable is called a member or field
Structs vs Database Tables
Structure Variable and Pointer
struct date
{
int year;
int mon;
int day;
};
 date when;
date* datePtr;
 Space is not allocated by struct
declarations, only by variable
definitions of that struct
Member Access Operators
Accessing the individual data contained in a struct
 If the left hand side (LHS) operand is a struct object, use the
. (the period, pronounced “dot”)
 If the LHS operand is a pointer to a struct, use the -> (formed
by the minus and greater than symbols, pronounced “arrow”)
 Members are legal anywhere a variable of that type is legal
(i.e., they can be either l-values or r-values)
 Members (i.e., names) are in a unique scope
Examples
 when.year = 1998;
 int mon = when.mon;
 datePtr->day = 14;
 int year = datePtr->year;
Struct Facts
Assignment, pass, and return copies a “patch of bits”
 Initialization
date today = { 1998, 2, 14 };
 Assignment (bit-by-bit copy)
when = today;
 Member by member assignment (if you only want some
members)
when.year = today.year;
when.mon = today.mon;
when.day = today.day;
 struct can pass as argument to function
passed by value (bit-by-bit copy)
passed by reference(address)
 struct can return as value from function
Manipulating structs
 Finding the address: &today;
 Allocating structs dynamically:
date* dateVar;
dateVar = new date;
 De allocating dynamic memory or variables:
delete dateVar;
Structure Function Arguments
 Passing structs by value and by address
Structure Return Values
date getDate(void)
{
date D;
cout << “enter a year: ”;
cin >> D.year;
cout << “enter a month: ”;
cin >> D.mon;
cout << “enter a day: ”;
cin >> D.day;
return D;
}
date now;
now = getDate( );
Nested structs(struct within structs)
 structs may contain
structs
range R;
R.Start.year = 1776;
 Structs cannot
contain themselves
struct range
{
date Start;
date End;
};
struct date
{
int year, mon, day;
date end; // error
};
STRUCTURES AND CLASSES
• Structures are usually used to hold data only,
and classes are used to hold both data and
functions.
• In C++ structures can in fact hold both data
and functions
• Major difference between class and a
structure is that in a class members are private
by default while in a structure they are public
by default.

More Related Content

Similar to Week2a.pptx

Similar to Week2a.pptx (20)

Structures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptxStructures_Final_KLE (2).pptx
Structures_Final_KLE (2).pptx
 
Lk module4 structures
Lk module4 structuresLk module4 structures
Lk module4 structures
 
Structure in c language
Structure in c languageStructure in c language
Structure in c language
 
Structure & union
Structure & unionStructure & union
Structure & union
 
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA REC UNIT-4 PREPARED BY M V BRAHMANANDA RE
C UNIT-4 PREPARED BY M V BRAHMANANDA RE
 
Structures
StructuresStructures
Structures
 
Structure
StructureStructure
Structure
 
Pointers and Structures
Pointers and StructuresPointers and Structures
Pointers and Structures
 
CHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptxCHAPTER -4-class and structure.pptx
CHAPTER -4-class and structure.pptx
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Str
StrStr
Str
 
Chapter15 structure
Chapter15 structureChapter15 structure
Chapter15 structure
 
Cs1123 12 structures
Cs1123 12 structuresCs1123 12 structures
Cs1123 12 structures
 
Lecture 19 - Struct and Union
Lecture 19 - Struct and UnionLecture 19 - Struct and Union
Lecture 19 - Struct and Union
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 
C programing -Structure
C programing -StructureC programing -Structure
C programing -Structure
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Structure In C
Structure In CStructure In C
Structure In C
 
Ch7 structures
Ch7 structuresCh7 structures
Ch7 structures
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 

Recently uploaded

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Week2a.pptx

  • 2. STRUCTURES  Collection of variables referenced with one name  A structure declaration is a blueprint (i.e., does not allocate memory); often put in header files struct tag { variable declarations; };  Creates a new type specifier (i.e., a new data type)  Variable declarations may include any type except the struct-type being created  Each element or variable is called a member or field
  • 4. Structure Variable and Pointer struct date { int year; int mon; int day; };  date when; date* datePtr;  Space is not allocated by struct declarations, only by variable definitions of that struct
  • 5. Member Access Operators Accessing the individual data contained in a struct  If the left hand side (LHS) operand is a struct object, use the . (the period, pronounced “dot”)  If the LHS operand is a pointer to a struct, use the -> (formed by the minus and greater than symbols, pronounced “arrow”)  Members are legal anywhere a variable of that type is legal (i.e., they can be either l-values or r-values)  Members (i.e., names) are in a unique scope Examples  when.year = 1998;  int mon = when.mon;  datePtr->day = 14;  int year = datePtr->year;
  • 6. Struct Facts Assignment, pass, and return copies a “patch of bits”  Initialization date today = { 1998, 2, 14 };  Assignment (bit-by-bit copy) when = today;  Member by member assignment (if you only want some members) when.year = today.year; when.mon = today.mon; when.day = today.day;  struct can pass as argument to function passed by value (bit-by-bit copy) passed by reference(address)  struct can return as value from function
  • 7. Manipulating structs  Finding the address: &today;  Allocating structs dynamically: date* dateVar; dateVar = new date;  De allocating dynamic memory or variables: delete dateVar;
  • 8. Structure Function Arguments  Passing structs by value and by address
  • 9. Structure Return Values date getDate(void) { date D; cout << “enter a year: ”; cin >> D.year; cout << “enter a month: ”; cin >> D.mon; cout << “enter a day: ”; cin >> D.day; return D; } date now; now = getDate( );
  • 10. Nested structs(struct within structs)  structs may contain structs range R; R.Start.year = 1776;  Structs cannot contain themselves struct range { date Start; date End; }; struct date { int year, mon, day; date end; // error };
  • 11. STRUCTURES AND CLASSES • Structures are usually used to hold data only, and classes are used to hold both data and functions. • In C++ structures can in fact hold both data and functions • Major difference between class and a structure is that in a class members are private by default while in a structure they are public by default.