Chapter 12: How to Create and Use Classes

Similar documents
C++ Important Questions with Answers

Chapter 4 Defining Classes I

Introduction to Programming Using Java (98-388)

CIS Intro to Programming in C#

Framework Fundamentals

Object-Oriented Programming

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

C# Language. CSE 409 Advanced Internet Technology

Chapter 6 Introduction to Defining Classes

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

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

C++ (Non for C Programmer) (BT307) 40 Hours

Objectives. Introduce static keyword examine syntax describe common uses

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

C++11: 10 Features You Should be Using. Gordon R&D Runtime Engineer Codeplay Software Ltd.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 02 Building Multitier Programs with Classes

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

An Introduction to C++

Problem Solving with C++

Exceptions, Case Study-Exception handling in C++.

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

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

Interview Questions of C++

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

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

Absolute C++ Walter Savitch

JAVA GUI PROGRAMMING REVISION TOUR III

CS 162, Lecture 25: Exam II Review. 30 May 2018

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

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

Microsoft Visual Basic 2005: Reloaded

Advanced Programming Using Visual Basic 2008

Short Notes of CS201

Standard. Number of Correlations

JAVA MOCK TEST JAVA MOCK TEST II

CS201 - Introduction to Programming Glossary By

What does it mean by information hiding? What are the advantages of it? {5 Marks}

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

Tokens, Expressions and Control Structures

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Java and OOP. Part 2 Classes and objects

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Object Oriented Programming in C#

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

M Introduction to Visual Basic.NET Programming with Microsoft.NET 5 Day Course

Function Overloading

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

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

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

Building Multitier Programs with Classes

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

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

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Objects as a programming concept

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

STUDENT LESSON A5 Designing and Using Classes

Object Oriented Programming

CHAPTER 7 OBJECTS AND CLASSES

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

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

Week 6: Review. Java is Case Sensitive

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

C# Syllabus. MS.NET Framework Introduction

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

Chapter 4. Defining Classes I

CHAPTER 7 OBJECTS AND CLASSES

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lecture 18 Tao Wang 1

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

DOT NET Syllabus (6 Months)

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Chapter 15: Object Oriented Programming

Programming Abstractions

C#.NET TRAINING / /

Chapter 1 Getting Started

Abstract Data Types. Different Views of Data:

Chapter 4. Defining Classes I

Microsoft. Microsoft Visual C# Step by Step. John Sharp

Public Class ClassName (naming: CPoint)

CSC Java Programming, Fall Java Data Types and Control Constructs

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Object-Oriented Design (OOD) and C++

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Object-Oriented Programming in C# (VS 2015)

Variables. Data Types.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Introduction to Classes and Objects. David Greenstein Monta Vista High School

More About Objects and Methods

Object-Oriented Programming Concepts

CS 2530 INTERMEDIATE COMPUTING

Overloading Operators in C++

Learning C# 3.0. Jesse Liberty and Brian MacDonald O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

AN OVERVIEW OF C++ 1

Transcription:

CIS 260 C# Chapter 12: How to Create and Use Classes 1. An Introduction to Classes 1.1. How classes can be used to structure an application A class is a template to define objects with their properties and functionality Windows forms applications: three-layered class architecture Presentation layer Main Windows form class Other form classes Middle layer Business classes Database layer Database classes Database Presentation layer controls the user interface Database layer handles the application s data processing Middle layer represents business entities such as customer, product, etc 1

Advantages of this architecture: - Allows the development to be spread among team members - Allows classes to be shared among applications - Allows different layers to run on different servers to improve performance 1.2. Class members: properties, methods, events Properties defined as private variables Methods define the functionality of the class objects and relations to other objects. Special method: constructor used to create an object of the class responded to. Events signals sent by the object that something has happened and has to be Encapsulation: a fundamental concept in OOP Refers to the capability to use an object without knowing its internal structure and how its methods are implemented Types of class members Property Method Constructor Delegate Event Field Constant Indexer Operator Class Represents a data value associated with an object instance. An operation that can be performed by an object. A special type of method that s executed when an object is instantiated. A special type of object that s used to wire an event to a method. A signal that notifies other objects that something noteworthy has occurred. A variable that s declared at the class level. A constant. A special type of property that allows individual items within the class to be accessed by index values. Used for classes that represent collections of objects. A special type of method that s performed for a C# operator such as + or ==. A class that s defined within the class. 2

1.3. Object instantiation An object is an instance of a class. Instantiation - the process of creating an object. Classes that instantiate objects define types (the type is the name of the class). Object declaration: Memory is reserved for the variable that will contain the address of the memory where the object properties are located. This is a reference variable. Example: Product product1; product1 is a reference variable. It will contain the memory address where the specific properties of the object product1 of type Product will be stored. The declaration statement above stores null in the variable product1 Object instantiation: A memory block is reserved for the properties of the object and its address is stored in the object variable. The constructor is invoked and its code is executed. It usually initializes the object properties. There may be more than one constructor. Example: public class Product private string code; private string description; private decimal price; public Product() public Product(string code, string description, decimal price) this.code = code; this.description = description; this.price = price; Instantiation of two objects of type Product: Product product1, product2; product1 = new Product("CS10", "Murach's C# 2010", 54.50m); product2 = new Product("VB10", "Murach's Visual Basic 2010", 54.50m); 3

Declaration and instantiation can be combined in one statement: Product product1 = new Product("CS10", "Murach's C# 2010", 54.50m); 2. How to Create a Class Use Project Add Class Type the name of the class A class file is added to the project and will appear in the solution window The namespace and class blocks are automatically added. Then you type the code in the class block Class fields: variables defined at class level, available to the entire class Instance variables: contain information about a particular instance of that class Static variables: contain information that is available to all instances of the class Read-only fields: cannot be changed when the program runs, set at run time (constants are set at compilation) keyword: readonly E.G: public readonly int Limit = 100; Access modifiers: - Public: available to other classes - Private: available only within the class 3. How to Code Properties public type PropertyName get get accessor code set set accessor code Example: public string Code get return code; // code is a private variable that // holds the value of the property set code = value; // value is a keyword used to pass the value // to the variable code Example of usage: string myvariable = product1.code; // invokes the get method myvariable = myvariable + V1 ; product1.code = myvariable; // invokes the set method 4

Auto-implemented property when no additional code is necessary public string Code get; set; 4. How to Code Methods public returntype MethodName ([parameterlist]) statements Overloading methods: Same name, different parameter lists 5. How to Code Constructors Constructors are methods that assign values to the instance variables. They are invoked upon object instantiation (using the operator new) If the constructor does not have parameters, or if there is no constructor, C# assigns default values (numbers are 0, Boolean variables are false, objects are null) Example: public Product(string code, string description, decimal price) this.code = code; this.description = description; this.price = price; The keyword this shows that the properties listed are properties of the current class. 6. How to Code Static Members Static members are used without referring to an object instance. The keyword static is used to define static members a. Static classes Static classes contain only static methods (see (b) below). Example: public static class Validator public static bool IsPresent(TextBox textbox)..// other static methods 5

b. Static methods When defined in a static class, static methods are referred to by the name of the class: if (Validator.IsPresent(textbox)).//statements When static members are defined in a non-static class, they can be shared by all other instances of the class. 7. How to Create Code Stubs Generate From Usage feature can be used to generate code stubs for any undefined class, constructor, property, method, field, enumeration, structure or interface from the current location in your code. 7.1. A class stub A class stub can be generated from the code that uses that class. Example: Suppose you want to instantiate an object of a class that does not exist yet. School school = new School( ); Since the School class does not exist, you will see a syntax error. Open the smart tag menu for the class name (when you position the cursor on the class name, an icon will appear bellow the name, click on it and the menu will open). Select the Generate class option. A class named School will be generated and shown as School.cs in the Solution explorer window. class School Note that no access modifier will be given. It is up to the programmer to add public in front of the keyword class. 7.2. A method stub When you write code that refers to an undefined method, you can use the smart tag menu (similar to creating a stub for a class) to create a stub for that method. 6

Example: School school = new School( ); string schooladdress = school.getaddress(); Suppose GetAddress( ) is not defined. With the smart tag menu you can generate a stub for that method: string GetAddress() throw new NotImplementedException (); Note that the return type is automatically determined by the statement that calls the method. Note that no access modifier is supplied, you have to add it, e.g. public string GetAddress(). If you run the code, a run-time error Unhandled exception will occur. To avoid the run-time error, you should use try-catch around the statement that calls the method: try // other statements string schooladdress = school.getaddress(); catch (Exception ex) System.Windows.Forms.MessageBox.Show(ex.Message); The message that you will get is: The method or operation is not implemented. In order to get a more meaningful message, you may change the argument in the throw statement: throw new NotImplementedException("The method GetAddress in School class is not implemented"); 7

Learning Objectives After studying Chapter 12, you should be able to: 1. List and describe the three layers of a three-layered application. 2. Describe these members of a class: constructor, method, field, and property. 3. Explain how instantiation works. 4. Describe the concept of overloading a method. 5. Explain what a static member is. 6. Describe the basic procedure for using the Generate From Usage feature to generate code stubs. 8