User Defined Types – The Struct

Slides:



Advertisements
Similar presentations
Exposure C++ Chapter XVI C++ Data Structures, the Record.
Advertisements

Chapter 7 Completing a Program
User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Class 15 - Overhead 11Object Oriented Programming Using C #define t_circle 1 #define t_rectangle 2 struct circle_shape {short type; double x,y; double.
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Object-Oriented Programming and Classes. OOP / Slide 2 Basic, built-in, pre-defined types : char, int, double, … Variables + operations on them int a,
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
1 Lecture 24 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
More Storage Structures A Data Type Defined by You Characteristics of a variable of a specific ‘data type’ has specific values or range of values that.
Programming Languages
 Review structures  Program to demonstrate a structure containing a pointer.
Structured Data Types array array union union struct struct class class.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
CS1201: Programming Language 2 Structure By: Nouf Almunyif.
Programmer Defined Structures (Records)
PS3-Slides typedef struct ProcessTag{ int id; int size; int time; int importance; int priority; } PrintJob; Priority is - say - a function of size, time.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
The Basics of Arrays Problem: How can the rancher easily catalog all of his cattle?
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Computer Programming II
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
C++ Lesson 1.
Programmer Defined Types and Classes
Structured Data (Lecture 07)
Programming Structures.
C++ Arrays.
Reserved Words.
Structures.
Object-Oriented Programming (OOP) Lecture No. 32
Introduction to Structured Data Types and Classes
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
By Hector M Lugo-Cordero September 17, 2008
Data type List Definition:
Counting Loops.
Sequential input and output Operations in file
Structured Data Types array union struct class.
Standard Input/Output Stream
Object Oriented Programming
Function Overloading.
Boolean Variables & Values
Review of C++ Language Basics
Fundamental Programming
Strings …again.
Data Structures and ADTs
Structures Structured Data types Data abstraction structs ---
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CSE Module 1 A Programming Primer
Structures Chapter 4.
Exercise 6 – Compound Types und Kontrollfluss
Presentation transcript:

User Defined Types – The Struct

struct Intro to Object-Oriented Programming (OOP) Allows us to abstract at a higher level to build entities more complex than short, long, int, float, double, char, and bool With it programmers can create their own types to define what should make up a student, a class, a department, a university, etc.

struct syntax // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

syntax example // placed in a header file struct type_name { member_type1 member_name1; member_type2 member_name2; . . member_typeN member_nameN; }; // point.h struct point {     float m_Xcoord;     float m_Ycoord; };

struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...

struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...

struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...

struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...

struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...

struct in use int main() { point p1, p2; // 2 points with 2 floats in each p1.m_Xcoord = 4; p1.m_Ycoord = 6; cout << "enter p2’s x: "; cin >> p2.m_Xcoord; cout << "and the y: "; cin >> p2.m_Ycoord; cout << "the x coordinate of p1 is “ << p1.m_Xcoord; ...

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ...

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ...

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left point m_Right

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord point m_Right float m_Xcoord float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord 5 point m_Right float m_Xcoord float m_Ycoord

structs within structs struct point { float m_Xcoord; float m_Ycoord; }; struct line point m_Left; point m_Right; int main() line my_line; my_line.m_Left.m_Xcoord = 5; my_line.m_Left.m_Ycoord = 8; ... line my_line point m_Left float m_Xcoord float m_Ycoord 5 8 point m_Right float m_Xcoord float m_Ycoord

Another Example struct car_part { string m_description; long m_partNumber; float m_wholesalePrice; float m_retailPrice; string m_color; etc };

Forward Declarations struct point; struct line { point m_Left; ... }; struct point

Forward Declarations struct point; struct line { point m_Left; ... }; struct point

Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; string m_name; }

Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; string m_name; }

Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; string m_name; }

Pro Tips struct complex_number { float m_RealPart; float m_ImaginaryPart; };

End of Session