By Hector M Lugo-Cordero September 17, 2008

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Operator Overloading. Introduction Operator overloading –Enabling C++’s operators to work with class objects –Using traditional operators with user-defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - Operator Overloading Outline 8.1 Introduction 8.2 Fundamentals of Operator Overloading 8.3.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
CMSC 2021 Stream I/O Operators and Friend Functions.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
 Review structures  Program to demonstrate a structure containing a pointer.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 Department of Computer Engineering 1.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
ITEC 320 C++ Examples.
Operator Overloading Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
1 Streams In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Command Line Arguments.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
CSE 332: C++ IO We’ve Looked at Basic Input and Output Already How to move data into and out of a program –Using argc and argv to pass command line args.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Algorithms and Data Structures
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
C:\Temp\Templates 4 5 Use This Main Program 6.
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
IIT Bombay Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends.
C++ ReviewEECE 3521 EECS 352 Data Structures and Algorithms José M. Vidal Office: 3A47.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
1 C++ Classes and Data Structures Course link…..
Lecture 3: Getting Started & Input / Output (I/O)
C++ Iterative Constructs
MT262A Review.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Operator Overloading Part 2
Command Line Arguments
Programming Fundamentals
Multi-dimensional Array
solve the following problem...
C++ Arrays.
Dynamic Memory Allocation Reference Variables
Announcements Exam 1 Grades Posted on Blackboard.
User Defined Types – The Struct
Intro to Programming Week # 4 Control Structure Lecture # 8
Function Overloading C++ allows us to define functions that have the same name but different sets of parameters This capability can be used to define similar.
Counting Loops.
Starting Out with C++: From Control Structures through Objects
اصول کامپیوتر ۱ مبانی کامپیوتر و برنامه‌سازی
Code::Block vs Visual C++
الوحدة الرابعة البرمجة وصياغة حل المسائل البرمجة وأهميتها أهداف الدرس الأول مفهوم البرمجة. الفرق بين المبرمج ومستخدم البرنامج. الحاجة إلى البرامج.
CS148 Introduction to Programming II
By Hector M Lugo-Cordero September 17, 2008
The for Loop Syntax: Same as: for (expression1;condition; expression2)
Introduction to Programming
If Statements.
Object Oriented Programming
Strings and Pointer Arrays
By Hector M Lugo-Cordero September 3, 2008
Functions and Recursion
Lab4 problems More about templates Some STL
Pointers and dynamic objects
Functions Divide and Conquer
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Presentation transcript:

By Hector M Lugo-Cordero September 17, 2008 Structs By Hector M Lugo-Cordero September 17, 2008 Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Outline Creation Usage Enhancing The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Creation Syntax struct <name>{ Fields [Methods] }; Just like objects (to be discussed) structs allow operator overloading in C++ (not in C) The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Creation (cont.) Example: struct Pair{ double first; double second; Pair(){ first = 0; second = 0; } Pair(double x, double y){ first = x; second = y; bool equal(Pair other){ return (first == other.first) && (second == other.second); }; The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Usage int main(int argc, char** argv) { Pair myPair(5, 2); //sometimes struct Pair myPair cout << “(“ << myPair.first << “, “ << myPair.second << “)” << endl; Pair test; test.first = 5; Test.second = 2; if(myPair.equal(test)){ cout << “Enter a new pair: ” << endl; cin >> test.first; cin >> test.second; //input is read x y } return 0; IS THERE AN EASIER WAY? :S YES :P The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Enhancing Operator overloading bool operator==(Pair& other){ return (first == other.first) && (second == other.second); } friend ostream& operator<<(otstream& out, Pair data){ out << “(“ << data.first << “, “ << data.second << “)”; return out; The Department of Electrical and Computer Engineering

The Department of Electrical and Computer Engineering Questions? ? The Department of Electrical and Computer Engineering