Structure As Function Arguments

Slides:



Advertisements
Similar presentations
Structures Spring 2013Programming and Data Structure1.
Advertisements

1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
A program example is given below to input date and display on the screen by write a class ‘ date ‘ # include Class date { Private: Int y, m, d ; Publice.
CS Sept Your first C++ program… Boilerplate // Cannon, demo program #include using namespace std; int main() {// program goes here… return.
Chapter 11: Records (structs)
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Lecture 8 Sept 25, Till now ► I► I► I► Introduction to computers ► S► S► S► Simple Programs using basic concepts like variables and data types,
 Review structures  Program to demonstrate a structure containing a pointer.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Object Oriented Programming (OOP) Lecture No. 11.
Inheritance Examples. Example Of Multiple Inheritance class personnel { protected: char name[30]; char addr[30]; char [30]; char birth_date[30];
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Chapter 7 A Data Types – Structures Structures Structure: C++ construct that allows multiple variables to be grouped together Structure Declaration.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Struct Data Type in C++ What Are Structures?
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
Lecture 20 Polymorphism. Introduction General meaning ; the ability to take on different forms. Programming language term: –Allows an entity to take a.
STRUCTURES. INTRODUCTION A structure is same as that of records. It stores related information about an entity. Structure is basically a user defined.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7. 12: Structures Starting Out with C++ Early Objects Eighth.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Lecture 11 Multi-dimensional Arrays
MT262A Review.
CSC241: Object Oriented Programming
Introduction to Programming
Command Line Arguments
Learning Objectives Pointers Pointer in function call
Programming Structures.
This technique is Called “Divide and Conquer”.
Structures.
Data Types – Structures
DATA HANDLING.
Lecture 8 – 9 Arrays with in a class
Student Data Score First Name Last Name ID GPA DOB Phone ...
Data Types – Structures
ساختار ها در زبان C Structures in C.
Structures in C++.
Structure ការណែនាំអំពី Structure
FUNCTIONS& FUNCTIONS OVERLOADING
CS150 Introduction to Computer Science 1
Pointers & Functions.
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
Struct Data Type in C++ What Are Structures?
Dynamic Memory A whole heap of fun….
Introduction to Programming
EECE.2160 ECE Application Programming
Introduction to Programming
CHAPTER 2 Arrays and Vectors.
Glenn Stevenson CSIS 113A MSJC
EECE.2160 ECE Application Programming
Starting to think about objects...
CS150 Introduction to Computer Science 1
CHAPTER 2 Arrays and Vectors.
Pointers & Functions.
The Stack.
Structure (i.e. struct) An structure creates a user defined data type
CS 201(Introduction To Programming)
CS150 Introduction to Computer Science 1
Objects as Function Arguments
EECE.2160 ECE Application Programming
The values to be assigned to the structure members are surrounded by braces and separated by commas. The first value in the list is assigned to the first.
Structures Chapter 4.
Programming Fundamental
Programming Fundamental
Presentation transcript:

Structure As Function Arguments

struct point { int x; int y; }; point sum (point p1, point p2) point t; t.x=p1.x+p2.x; t.y=p1.y+p2.y; return t; } void main () point p1={2,2}; point p2={3,3}; point p3=sum(p1,p2); cout<<p3.x<<endl; cout<<p3.y<<endl;

Nested Structures

Nested Structures One Structure can be nested into another structure. Example struct Distance { int feet; float inches; }; struct Room char name[20]; Distance length; Distance width;

Accessing Nested Structure Members To access the nested structure members, we have to apply dot operator twice. Example Room Dining; strcpy(Dining.name,”dining room”); Dining.length.feet=13; Dining.length.inches=6.5; Dining.width.feet=20; Dining.width.inches=10;

Initializing Nested Structures In the previous example we can initialize the nested structure such that each structure of type Distance, which is embedded in Room, is initialized separately. Which involves surrounding the values with braces and separating them with commas. Example; Room dining ={“Dining room”, {13, 6.5},{20,10} };

Example struct Distance { int feet; float inches; }; struct Room char name[20]; Distance length; Distance width; void main( ) { Room dining= {“Dining room”, {13,3.2}, {10,0.0} }; cout<<dining.name<<endl; cout<<dining.length.feet<<endl; cout<< dining.length.inches<<endl; cout<< dining.width.feet<<endl; cout<<dining.width.inches<<endl; }

Activity Create a structure called Volume that uses three variables of type Distance to model the volume of a room. Initialize a variable of type Volume to specific dimensions, then calculate the volume it represents, and print out the result.

Array of Structures

Example struct student { int regNo; float gpa ; }; void main ( ) int n; student std[10]; for (n=0; n<10; n++) { cout<< “Enter student Reg#\n”; cin>>std[n].regNo; cout<<“Enter Gpa\n”; cin>>std[n].gpa; } cout<<std[n].regNo<<endl; cout<<std[n].gpa<<endl

Accessing Structure Members with pointers to structures

Example struct student { char name[64]; char course[128]; int age; int year; }; void main() student s1 = {"Ali", "CS201- Introduction to programming", 22, 2008}; student *sptr; sptr = &s1; cout << "Displaying the structure data members using pointers" << endl; cout << "Using the * operator" << endl; cout << endl; cout << "The name is " << (*sptr).name<<endl; cout << "The course is " <<(*sptr).course<<endl; cout << "The age is " <<(*sptr).age<<endl; cout << "The year is " << (*sptr).year << endl; }

Array of Structures with Pointers (Using *. Operator) struct student { int rollno; int marks; }; void main() { student s[3]; student *p= s; cout << "Enter Record" << endl; for(int i=0;i<3;i++) cin>>(*(p+i)).rollno; cin>>(*(p+i)).marks; } student t; for(int pass=0;pass<3;pass++) for(int j=0;j<2;j++) if (p[j].marks<p[j+1].marks) { t=p[j]; p[j]=p[j+1]; p[j+1]=t; } cout<<"Roll NO\tMarks"<<endl; for( i=0;i<3;i++) cout<<(*(p+i)).rollno<<"\t"; cout<<(*(p+i)).marks<<endl;

Array of Structures with Pointers (Using -> Operator) struct student { int rollno; int marks; }; void main() { student s[3]; student *p= s; cout << "Enter Record" << endl; for(int i=0;i<3;i++) cin>>(p+i)->rollno; cin>>(p+i)->marks; } student t; for(int pass=0;pass<3;pass++) for(int j=0;j<2;j++) if (p[j].marks<p[j+1].marks) { t=p[j]; p[j]=p[j+1]; p[j+1]=t; } cout<<"Roll NO\tMarks"<<endl; for( i=0;i<3;i++) cout<<(p+i)->rollno<<"\t"; cout<<(p+i)->marks<<endl;