Object Oriented Programming

Slides:



Advertisements
Similar presentations
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Class Hierarchies. Inheritance is the capability of a class to use the properties and methods of another class while adding its own functionality. An.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Advanced Object-Oriented Programming Features
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CSI 101 Elements of Computing Spring 2009 Lecture # 14 – Classes and Objects Wednesday, April 15th, 2009 and Monday, April 20th, 2009.
Graphical User Interfaces A Quick Outlook. Interface Many methods to create and “interface” with the user 2 most common interface methods: – Console –
DT265-2 Object Oriented Software Development 2 Lecture 3 : Windows Programming Lecturer Pat Browne
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Class & Object 蔡柏灃.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Visual Programming Fall 2012 – FUUAST Topic: Development environment.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 1B Introduction (Tutorial)
Using the Netbeans GUI Builder. The Netbeans IDE provides a utility called the GUI Builder that assists you with creating Windows applications. The Netbeans.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Applications Development
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Programming With Java ICS201 University Of Ha’il1 ICS 201 Introduction to Computer Science Inheritance.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Introduction to Object-oriented Programming
Creating Your Own Classes
Classes C++ representation of an object
CIS 200 Test 01 Review.
Welcome to Visual Programming using C#
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
3.01 Apply Controls Associated With Visual Studio Form
How to design a Windows Forms application
3.01 Apply Controls Associated With Visual Studio Form
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming Language Concepts (CIS 635)
Introduction to Events
Module 4: Implementing Object-Oriented Programming Techniques in C#
OOP’S Concepts in C#.Net
Defining Classes and Methods
Department Array in Visual Basic
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Advanced Programming Lecture 02: Introduction to C# Apps
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Inheritance Cse 3rd year.
Abstract Classes and Interfaces
CIS 199 Final Review.
Java Programming Language
Review of Previous Lesson
Classes C++ representation of an object
Final Exam Review Inheritance Template Functions and Classes
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Object Oriented Programming C# Part 2 Object Oriented Programming

Ternary operators Ternary operators allow you to make several lines of code into 1 Example: x = (i > 5) ? 3 : 2; or if (i > 5) x = 3; else x = 2;

Accessibility

Fyi, variables inside of a class without an explicit accessibility modifier is private

Polymorphism This is used to inherit a class from a base class and override some methods Example: Every mammal speaks, but each mammal speaks in a different way (lets say a different word)

To override a method, use the new keyword class c1 { public void fun()… class c2 : c1 { public new void fun()… (See p2)

Class Example Write a class with a private, public, and protected member, and attempt to access both the public and private member. Also include functions.

To make sure a method will always execute for a given object, use the virtual keyword and override in the new function Ie public virtual void fun() {… public override void fun() {… This will prevent the executing of the fun function from the base class (See p3)

A virtual method will always be virtual ….until you used the sealed keyword public sealed override void fun() {…

Get/set… You knew these as mutator functions (Ok…maybe not) C# introduces the concept of a field A field is way to access private data without the implicit use of get/set methods

Example: public Int32 X { get { return x; } set { x = value; } } When defining properties you can use 2 functions:

get – returns data back to the caller set – sets the data in the class Example: Point p1 = new Point(); p1.X = 10; Console.WriteLine(“{0}”, p1.X);

Class Exercise Lets create a bike shed! (BSD joke) Create 2 classes: Shed, and bikeshed Shed should have a field for get/set the color Also should override the ToString method and output “I am a bikeshed” The bikeshed should derive from the shed

When using the new keyword and you do a cast to the baseclass, the functions in the old baseclass will be executed: Ie. Class2 B = new Class2(); Class1 A = (Class1) B; A.fun(); //will execute A.fun();

HOWEVER…. When you use the virtual and override keywords to define the functions a new set of rules come into play: Class2 B = new Class2(); Class1 A = (Class1) B; A.fun(); //will execute B.fun();

Trick questions Given the following code: This is halfway

Given the following code:

Constructor As you may already know from a previous TV show (namely C++ or Java)…. Constructors allow for you to create a function to fire on object creation Example: public classname(){ this.x = 5; } (See p6)

Callbacks Callbacks allow you to treat a method like a pointer This is useful to allow other code to plug into your classes (See p8)

Events Events allow you to trigger an event upon a condition (ie a variable is changed) This is useful if you want to trigger an error or important message when a variable hits a certain point (See p7)

GUI (finally) Open Visual Studio File -> New Project -> Visual C# -> Windows Forms Application

GUI Best Practices Labels prefix with lbl Buttons prefix with btn Checkboxes prefix with chk Integers prefix with int Longs prefix with lng Doubles prefix with dbl Ect…

GUI Elements

Textbox -> Allows users to input data CommandButton -> Allow users to interact with the form Checkbox -> Usually used for turning on/off settings

To add controls just drag and drop to the form All controls have a name property (It is usually a good idea to change this from the default) To access control properties just use dot notation – Textbox1.text = “Some text”; //Don’t forget semicolon!!!!

Class Example Create a form that has 2 input boxes and adds them up. Output the result to a label

Class Exercise I burn backups to CDs and DVDs. I need a program that will allow me to input 2 textboxes (1 for number of CDs and 1 for number of DVDs) and calculate how much data total I have on media (to make this easy lets stick with megabytes). CD = 700MB DVD = 4813MB (rounded)

Credits http://devhood.com/training_modules/dist-a/WritingOOCSharp/WritingOOCSharp.htm#_Toc520858423 http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx http://weblogs.asp.net/dburke/archive/2004/03/26/97195.aspx