CIS Intro to Programming in C#

Similar documents
Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

Microsoft Visual Basic 2005: Reloaded

Chapter 2. Building Multitier Programs with Classes The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Advanced Programming Using Visual Basic 2008

Chapter 6. Multiform Projects The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Building Multitier Programs with Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CIS 3260 Intro to Programming in C#

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 12: How to Create and Use Classes

Chapter 5 Object-Oriented Programming

Framework Fundamentals

Programming II (CS300)

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Introduction to Programming Using Java (98-388)

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming II (CS300)

CS313D: ADVANCED PROGRAMMING LANGUAGE

1. A Web Form created in Visual Basic can only be displayed in Internet Explorer. True False

C++ Important Questions with Answers

CS313D: ADVANCED PROGRAMMING LANGUAGE

Microsoft Visual Basic 2005: Reloaded

CIS 3260 Intro to Programming with C#

CIS 3260 Intro. to Programming with C#

Data Structures using OOP C++ Lecture 3

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Programming Using C# QUEEN S UNIVERSITY BELFAST. Practical Week 7

PROGRAMMING LANGUAGE 2


Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

CIS 3260 Intro to Programming with C#

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Inheritance and Polymorphism

Lecture 18 Tao Wang 1

Chapter 02 Building Multitier Programs with Classes

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Object-Oriented Programming

Java Primer 1: Types, Classes and Operators

Introduction to Java. Handout-1d. cs402 - Spring

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

What is Inheritance?

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

CMSC 132: Object-Oriented Programming II

More C++ : Vectors, Classes, Inheritance, Templates

Object Oriented Design

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

Object Orientated Analysis and Design. Benjamin Kenwright

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Practice for Chapter 11

Ch02. True/False Indicate whether the statement is true or false.

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Chapter 6 Introduction to Defining Classes

Interview Questions of C++

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Chapter 10 Introduction to Classes

Chapter 4. Defining Classes I

Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Chapter 4.!Data Abstraction: The Walls! 2011 Pearson Addison-Wesley. All rights reserved 4-1

CS313D: ADVANCED PROGRAMMING LANGUAGE

CIS 209 Final Exam. 1. A Public Property procedure creates a property that is visible to any application that contains an instance of the class.

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Polymorphism. Zimmer CSCI 330

Cpt S 122 Data Structures. Introduction to C++ Part II

Software Design and Analysis for Engineers

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CMSC 132: Object-Oriented Programming II

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide


Object Oriented Design

Lecture 7: Classes and Objects CS2301

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 4 Defining Classes I

CIS 3260 Intro. to Programming with C#

Data Abstraction: The Walls

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Programming overview

OVERRIDING. 7/11/2015 Budditha Hettige 82

CLASSES AND OBJECTS IN JAVA

JAVA GUI PROGRAMMING REVISION TOUR III

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Chapter 7. Inheritance

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Transcription:

OOP: Creating Classes and Using a Business Tier McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. Understand how a three-tier application separates the user interface from the business logic and data Differentiate between a class and an object Create a class that has properties and methods Declare object variables and assign values to the properties with a constructor or property methods McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-2 Analyze the characteristics (variables) and behaviors (methods) needed by a new object Example A form gathers price and quantity of a product (presentation tier) Design a class to perform calculation of extended price (business tier)***very IMPORTANT Price, quantity, and extended price are stored in private variables in the class Variables are accessed through property methods McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-3 Dr. David McDonald Chapter 12a Page 1

The presentation tier form instantiates the business tier class Pass the price and quantity to it through property procedures or Invokes a method and passes arguments to the method s parameter list to calculate the extended price Display the extended price on the form by retrieving it from a property method McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-4 Define private member variables inside your class Stores the values for the properties of the class Declare all variables as private or protected, not public, which violates the rules of encapsulation Protected variables and methods behave as private but are available in classes that inherit from the class Use property set and get methods to pass values between a class and the class where objects of the class are instantiated McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-5 Create methods of a new class by coding public methods within the class Methods declared with the private keyword are available only within the class Methods declared with the public keyword are available to external objects created from this class or other classes Methods declared with the protected keyword behave as private within the class and any class that inherits from it McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-6 Dr. David McDonald Chapter 12a Page 2

Executes automatically when an instance (object) of the class is created Ideal location for initialization tasks such as setting the initial values of variables and properties Constructor must be public because the objects created must execute this method If no constructor is written for a class, the compiler creates an implicit default constructor with an empty parameter list McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-7 Overloading means that two methods have the same name but a different list of arguments (the signature) Create overloaded methods in a class Give the same name to multiple methods Each with a different argument list An empty constructor public ClothingSale() A constructor that passes arguments to the class public ClothingSale(string productnumberstring, int quantityinteger, decimal discountratedecimal) McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-8 A constructor that requires arguments Allows arguments to be passed when creating a new object from the class Assign incoming values to the properties of the class Assign an argument to the property name, which then uses the set method Validation is often performed in the set methods McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-9 Dr. David McDonald Chapter 12a Page 3

Add a new class to a project Choose Project/Add Class In the Add New Item dialog box, select Class Name the class and click Add Define the class properties Declare private class-level variables to hold the property values of the new class Write the property methods with a public get and a private set method Write the constructor Code a method McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-10 Set a property statement as public and then assign a private get or set method public string EmployeeID { get { return employeeidstring; } private set { employeeidstring = value; } } Allows public access to the get method while the set method is private McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-11 Creating a new class does not create any objects of the class Similar to creating a new tool for the toolbox Generally a two-step operation Declare the variable Instantiate the new object using the new keyword Can declare and instantiate in the same statement ClothingSale aclothingsale = new ClothingSale(); McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-12 Dr. David McDonald Chapter 12a Page 4

If an object variable is to be used in multiple methods, declare the object at class level Instantiate the object inside of a method at the time the object is needed Use a try/catch if converting and passing values entered by a user Pass values for the arguments at instantiation when using a parameterized constructor McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-13 Instance variables and properties Separate memory location for each instance of the object Each object created from the class has its own set of properties Also called instance members An instance member has one copy for each instance or object of the class Static variable, property or method Exists, or is available, for all objects of the class Used for a count or total of all objects of the class Also called static members A static member has one copy for all objects of the class Access static members without instantiating an object McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-14 Use the static keyword to create a static member Make properties for static members readonly Allows values to be retrieved, but not set directly A static keyword on a private class-level variable is required The static keyword on a property method is optional Allows a property to be retrieved without creating an instance of the class McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-15 Dr. David McDonald Chapter 12a Page 5

A new class can be based on another class Can inherit from One of the existing.net classes One of your own classes The inheritance clause must follow the class header prior to any comments McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-16 When writing code for a derived class, all public and protected data members and methods of the base class can be referenced Use the protected keyword to declare elements that are accessible only within their own class or any class derived from that class McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-17 Visual Studio by default begins execution of C# applications with the Program.cs file The first form added to a project is the startup form To change the startup form, modify the Application.Run method in the Program.cs file Application.Run(new MainForm()); McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-18 Dr. David McDonald Chapter 12a Page 6

Two ways to include an existing form or other class in a project 1. Reference the file in its original location 2. Move or copy the file into the project folder (best way) Select Project/Add Existing Item to add the file to the project Or, right-click the project name in Solution Explorer and select from the context menu McGraw-Hill 2010 The McGraw-Hill Companies, Inc. All rights reserved. 12-19 Dr. David McDonald Chapter 12a Page 7