C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:



Similar documents
Data Structures using OOP C++ Lecture 1

C++ INTERVIEW QUESTIONS

13 Classes & Objects with Constructors/Destructors

Comp151. Definitions & Declarations

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

C++FA 5.1 PRACTICE MID-TERM EXAM

6. Control Structures

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

Appendix K Introduction to Microsoft Visual C++ 6.0

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Object Oriented Software Design II

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Answers to Review Questions Chapter 7

Computer Programming C++ Classes and Objects 15 th Lecture

EP241 Computer Programming

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

SQLITE C/C++ TUTORIAL

Illustration 1: Diagram of program function and data flow

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

recursion, O(n), linked lists 6/14

JAVA - INNER CLASSES

An Overview of Java. overview-1

Constructor, Destructor, Accessibility and Virtual Functions

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Object Oriented Software Design II

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Tutorial on C Language Programming

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

DATA STRUCTURE - STACK

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Class 16: Function Parameters and Polymorphism

Lecture 2 Notes: Flow of Control

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Chapter 9 Text Files User Defined Data Types User Defined Header Files

1 Abstract Data Types Information Hiding

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

DATA STRUCTURE - QUEUE

>

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Copyright 2013 wolfssl Inc. All rights reserved. 2

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Introduction to Data Structures

sqlpp11 - An SQL Library Worthy of Modern C++

Object Oriented Software Design II

Basics of C++ and object orientation in OpenFOAM

Compiler Construction

Functional Programming in C++11

Stacks. Linear data structures

C Programming Review & Productivity Tools

Moving from C++ to VBA

Chapter 5 Functions. Introducing Functions

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Common Beginner C++ Programming Mistakes

CSI33 Data Structures

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Testing, Debugging, and Verification

Common Data Structures

Lecture 11 Array of Linked Lists

N3458: Simple Database Integration in C++11

An Incomplete C++ Primer. University of Wyoming MA 5310

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

/* File: blkcopy.c. size_t n


Lecture 12 Doubly Linked Lists (with Recursion)

Chapter 9 Delegates and Events

COMBINATIONAL CIRCUITS

C++ Essentials. Sharam Hekmat PragSoft Corporation

Programming languages C

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

Konzepte objektorientierter Programmierung

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

CS 241 Data Organization Coding Standards

arrays C Programming Language - Arrays

OBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES CONSTRUCTORS AND DESTRUCTORS

COSC 181 Foundations of Computer Programming. Class 6

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Stack Allocation. Run-Time Data Structures. Static Structures

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Solving Problems Recursively

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Transcription:

C++ DATA STRUCTURES http://www.tutorialspoint.com/cplusplus/cpp_data_structures.htm Copyright tutorialspoint.com C/C++ arrays allow you to define variables that combine several data items of the same kind but structure is another user defined data type which allows you to combine data items of different kinds. Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book: Title Author Subject Book ID Defining a Structure: To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member, for your program. The format of the struct statement is this: struct [structure tag]... [one or more structure variables]; The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure: book; Accessing Structure Members: To access any member of a structure, we use the member access operator.. The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use struct keyword to define variables of structure type. Following is the example to explain usage of structure: ;

int main( ) Book1; // book 1 specification // book 2 specification // Print Book1 info cout << "Book 1 title : " << Book1.title <<endl; cout << "Book 1 author : " << Book1.author <<endl; cout << "Book 1 subject : " << Book1.subject <<endl; cout << "Book 1 id : " << Book1.book_id <<endl; // Print Book2 info cout << "Book 2 title : " << Book2.title <<endl; cout << "Book 2 author : " << Book2.author <<endl; cout << "Book 2 subject : " << Book2.subject <<endl; cout << "Book 2 id : " << Book2.book_id <<endl; Book 1 title : Learn C++ Programming Book 1 author : Chand Miyan Book 1 subject : C++ Programming Book 1 id : 6495407 Book 2 title : Telecom Billing Book 2 author : Yakit Singha Book 2 subject : Telecom Book 2 id : 6495700 Structures as Function Arguments: You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. You would access structure variables in the similar way as you have accessed in the above example: void printbook( book ); ; int main( ) Book1;

// book 1 specification // book 2 specification // Print Book1 info printbook( Book1 ); // Print Book2 info printbook( Book2 ); void printbook( book ) cout << "Book title : " << book.title <<endl; cout << "Book author : " << book.author <<endl; cout << "Book subject : " << book.subject <<endl; cout << "Book id : " << book.book_id <<endl; Book title : Learn C++ Programming Book author : Chand Miyan Book subject : C++ Programming Book id : 6495407 Book title : Telecom Billing Book author : Yakit Singha Book subject : Telecom Book id : 6495700 Pointers to Structures: You can define pointers to structures in very similar way as you define pointer to any other variable as follows: * struct_pointer; Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the & operator before the structure's name as follows: struct_pointer = &Book1; To access the members of a structure using a pointer to that structure, you must use the -> operator as follows: struct_pointer->title; Let us re-write above example using structure pointer, hope this will be easy for you to understand the concept: void printbook( * book );

; int main( ) Book1; // Book 1 specification // Book 2 specification // Print Book1 info, passing address of structure printbook( &Book1 ); // Print Book1 info, passing address of structure printbook( &Book2 ); // This function accept pointer to structure as parameter. void printbook( * book ) cout << "Book title : " << book->title <<endl; cout << "Book author : " << book->author <<endl; cout << "Book subject : " << book->subject <<endl; cout << "Book id : " << book->book_id <<endl; Book title : Learn C++ Programming Book author : Chand Miyan Book subject : C++ Programming Book id : 6495407 Book title : Telecom Billing Book author : Yakit Singha Book subject : Telecom Book id : 6495700 The typedef Keyword There is an easier way to define structs or you could "alias" types you create. For example: typedef struct Books; Now, you can use Books directly to define variables of Books type without using struct keyword.

Following is the example: Books Book1, Book2; You can use typedef keyword for non-structs as well as follows: typedef long int * pint32; pint32 x, y, z; x, y and z are all pointers to long ints Loading [MathJax]/jax/output/HTML-CSS/jax.js