برنامه سازي پيشرفته 1 4. using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public.

Slides:



Advertisements
Similar presentations
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Advertisements

Operator overloading redefine the operations of operators
Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
static void Main() { int i = 0; if (i == 0) { int a = 5; int b = 15; if (a == 5) { int c = 3; int d = 99; }
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
Object-Oriented Programming. Agenda Classes & Objects Attributes Methods Objects in memory Visibility Properties (get & set) Constructors.
Overloading Operators Overloading operators Unary operators Binary operators Member, non-member operators Friend functions and classes Function templates.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
EC-241 Object-Oriented Programming
Operator Overloading Fundamentals
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
C++ Classes & Data Abstraction
در جامعه، افرادي که موقعيت و مسؤليت خود را مي‌دانند از راحتي و امنيت بيشتري برخوردارند هر کلاس از جامعه، مجموعه‌اي از امکانات و تواناييهاي مجاز برخوردار.
C++ Training Datascope Lawrence D’Antonio Lecture 6 An Overview of C++: What is Polymorphism? - Coercion.
برنامه سازي پيشرفته 5 Inheritance. وراثت Inheritance allows a software developer to derive a new class from an existing one The existing class is called.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Inheritance #1 First questions Similar to Python? What about visibility and encapsulation? – can an object of the child class access private members.
Crossword Puzzle Solver Michael Keefe. Solver structure.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Case Study - Fractions Timothy Budd Oregon State University.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Operator Overloads Part2. Issue Provide member +(int) operator Rational + int OK int + Rational Error.
ساختارهاي تقسيم كار پروژه
Operator Overloading Week 5.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
Introduction to Programming Lecture 6: Making Decisions.
اسامي شناسه ها (Identifier names) اسامي متغيرها ، توابع ، برچسب ها (labels) وبقيه اشياء تعريف شده توسط كاربر در C ، شناسه ( identifier ) ناميده مي شود.
Const Member Functions Which are read-only? //fraction.h... class Fraction { public: void readin(); void print();
شرط و تصميم اصول كامپيوتر 1. 2 الگوريتم اقليدس E1: [find remainder] Divide m by n and let r be the remainder. Clearly, 0
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
کلاس های متغيرها در C 1- کلاس متغيرهای اتوماتيک auto int x = 10; /* x is global auto int x = 10; /* x is global void main( ) { void main( ) { int x = 25;
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
الف) تابع y = f(x) = X >= 0x -2 < x < 0 x3x3 X
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Static Members.
Find the common denominator. Make equivalent fractions.
OOP Powerpoint slides from A+ Computer Science
Examples of Classes & Objects
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
More About Objects and Methods CS140: Introduction to Computing 1 Savitch Chapter 6 10/16/13.
Chapter 2.
Overloading and Constructors
null, true, and false are also reserved.
Accessor and Mutator Functions
نمايش اعداد در کامپيوتر چهار عمل اصلي
Classes & Objects: Examples
More on Classes and Objects
Operator Overloads Part1
© A+ Computer Science - OOP © A+ Computer Science -
Problem Solving Session
Module 2 Variables, Assignment, and Data Types
C# Programming: From Problem Analysis to Program Design
Operator Overloads Part1
CIS 199 Final Review.
Object-Oriented Programming and class Design
Chapter 11 Classes.
Presentation transcript:

برنامه سازي پيشرفته 1 4

using System; //A class represents a reference type in C# class Fraction { public int numerator; public int denominator; public void Print( ) {Console.WriteLine( "{0}/{1}", numerator, denominator );} } public class ReferenceTest { public static void Main( ) { Fraction f = new Fraction( ); f.numerator = 5; f.denominator = 10; f.Print( ); Fraction f2 = f;//f2 is a reference to f and not a copy!!! f.Print( ); //modify instance f2. Note that f is also effected. f2.numerator = 1; f.Print( ); f2.Print( ); } 2 2 f f2 وقتي از عملگر انتساب براي كلاس استفاده مي‌شود. f2 مرجعي براي f است و نه كپي f

تعريف مجدد عملگرها عملگرها بايد بصورت static تعريف شوند. عملگرها به يك نوع تعلق دارند، نه به يك نمونه. فرم تابع تعريف مجدد عملگر : public static return-type operator T (param p [,param p1]) 3

using System; public class Fraction { //data members private intm_numerator; private intm_denominator; //Properties public int Numerator { get { return m_numerator; } set { m_numerator = value; } } public int Denominator { get { return m_denominator; } set { m_denominator = value; } } //Constructors public Fraction( ) { m_numerator = 0; m_denominator = 0; } public Fraction( int iNumerator, int iDenominator ) { m_numerator = iNumerator; m_denominator = iDenominator; } 4 4

//Operator Overloading public static Fraction operator+(Fraction f1, Fraction f2) { Fraction Result = new Fraction( ); if( f1.Denominator != f2.Denominator ) { Result.Denominator = f1.Denominator * f2.Denominator; Result.Numerator = (f1.Numerator * f2.Denominator) + (f2.Numerator * f1.Denominator); } else { Result.Denominator = f1.Denominator; Result.Numerator = f1.Numerator + f2.Numerator; } return Result; } public static Fraction operator-(Fraction f1, Fraction f2) { Fraction Result = new Fraction( ); if( f1.Denominator != f2.Denominator ) { Result.Denominator = f1.Denominator * f2.Denominator; Result.Numerator = (f1.Numerator * f2.Denominator) - (f2.Numerator * f1.Denominator); } else { Result.Denominator = f1.Denominator; Result.Numerator = f1.Numerator - f2.Numerator; } return Result; } }//end of class Fraction 5 5

public class OperatorTest { public static void Main( ) { Fraction f1 = new Fraction( 1, 5 ); Fraction f2 = new Fraction( 2, 5 ); Fraction f3 = f1 + f2; Console.WriteLine("f1 + f2 = {0}/{1}", f3.Numerator, f3.Denominator ); f3 = f3 - f2; Console.WriteLine("f3 - f2 = {0}/{1}", f3.Numerator, f3.Denominator ); Console.ReadLine(); } 6 6

تعريف مجدد عملگرها تبديل نوع تبديل نوع صريح (explicit) نوع مشخص مي‌شود. تبديل نوع غير صريح يا ضمني (implicit) نوع مشخص نمي‌شود. Fraction f=new Fraction(1, 5); double d = f; //implicit cast double dd = (double)f; //explicit cast 7

فرم تابع تعريف مجدد عملگر : public static [implicit | explicit] operator return-type (Type T) 8

public static explicit operator double (Fraction f) { double dResult = ((double)f.Numerator / (double)f.Denominator); return dResult; } public static bool operator == (Fraction f1, Fraction f2) { if (f1.Numerator * f2.Denominator == f1.Denominator * f2.Numerator) return true; else return false; } public static bool operator != (Fraction f1, Fraction f2) { return !(f1 == f2); } 9 9