Introduction to Classes SWE 344 Internet Protocols & Client Server Programming.

Slides:



Advertisements
Similar presentations
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Advertisements

Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Getting Started with C# 1 SWE 344 Internet Protocols & Client Server Programming.
Section 5 – Classes. Object-Oriented Language Features Abstraction –Abstract or identify the objects involved in the problem Encapsulation –Packaging.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
ASP.NET Programming with C# and SQL Server First Edition
Review of C++ Programming Part II Sheng-Fang Huang.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
COMP 205 – Week 10 Chunbo Chu. Method Local variables The existence of a local variable is limited to the block in which it is created and the blocks.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Distributed Systems (236351) Tutorial 1 - Getting Started with Visual Studio C#.NET.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Module 3: Working with Components. Overview An Introduction to Key.NET Framework Development Technologies Creating a Simple.NET Framework Component Creating.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
ILM Proprietary and Confidential -
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
ECE122 Feb. 22, Any question on Vehicle sample code?
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
1 Introduction to C# Programming Console applications No visual components Only text output Two types MS-DOS prompt - Used in Windows 95/98/ME Command.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
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.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Lecture 8: Object Oriented Programming. What is a Programming Object? An object is an instance of a class. A class is a template for an object. Everything's.
Introduction to Object-Oriented Programming Lesson 2.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
METADATA IN.NET Presented By Sukumar Manduva. INTRODUCTION  What is Metadata ? Metadata is a binary information which contains the complete description.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 1.2 Introduction to C++ Programming
Creating and Using Objects, Exceptions, Strings
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Static data members Constructors and Destructors
Examples of Classes & Objects
2.7 Inheritance Types of inheritance
Methods Attributes Method Modifiers ‘static’
Reference: COS240 Syllabus
Chapter 3: Using Methods, Classes, and Objects
group work #hifiTeam
CSC 113 Tutorial QUIZ I.
Advanced Programming Lecture 02: Introduction to C# Apps
Classes & Objects: Examples
Chapter 3 – Introduction to C# Programming
How to organize and document your classes
Presentation transcript:

Introduction to Classes SWE 344 Internet Protocols & Client Server Programming

 Classes are declared by using the keyword class followed by the class name and a set of class members surrounded by curly braces.  Every class has a constructor, which is called automatically any time an instance of a class is created.  The purpose of constructors is to initialize class members when an instance of the class is created.  Constructors do not have return values and always have the same name as the class. Introduction to Classes 2

 Complete list of the types of members in classes: 1.Constructors 2.Destructors 3.Fields 4.Methods 5.Properties 6.Indexers 7.Delegates 8.Events 9.Nested Classes Introduction to Classes 3

using System; public class Person { public int Age; public string HairColor; static void Main(string[] args) { Person Ahmed = new Person(); Person Sami = new Person(); // Specify some values for the instance variables Ahmed.Age = 20; Ahmed.HairColor = "Brown"; Sami.Age = 25; Sami.HairColor = "Black"; // print the console's screen some of the variable's values Console.WriteLine(“Age of Ahmed = {0}, Age of Sami = {1}", Ahmed.Age, Sami.Age); Console.ReadLine(); } Example #1: Classes (Public variables) Age of Ahmed = 20 Age of Sami = 25 4

Example #2: Classes (Private variables) using System; public class Kid { private int age; private string name; // Default constructor: public Kid() { name = "N/A"; } // Constructor: public Kid(string name, int age) { this.name = name; this.age = age; } // Printing method: public void PrintKid() { Console.WriteLine("{0}, {1} years old.", name, age); } 5

public class MainClass { public static void Main() { // Create objects // Objects must be created using the new operator: Kid kid1 = new Kid("Sami", 11); Kid kid2 = new Kid("Khalid", 10); // Create an object using the default constructor: Kid kid3 = new Kid(); // Display results: Console.Write("Kid #1: "); kid1.PrintKid(); Console.Write("Kid #2: "); kid2.PrintKid(); Console.Write("Kid #3: "); kid3.PrintKid(); Console.ReadLine(); } Example #2: Classes (Private variables) 6

Kid #1: Sami, 11 years old. Kid #2: Khalid, 10 years old. Kid #3: N/A, 0 years old. In this example, the private fields (name and age) can only be accessed through the public methods of the Kid class. So, we cannot print the kid's name, from the Main method, using a statement like this: Console.Write(kid1.name); // Error Notes: Example #2: Classes (Private variables) 7

// Namespace Declaration using System; // helper class class OutputClass { string myString; // Constructor public OutputClass(string inputString) { myString = inputString; } // Instance Method public void printString() { Console.WriteLine("{0}", myString); } // Destructor ~OutputClass() { // Some resource cleanup routines } Example #3: Classes 8

// Program start class class ExampleClass { // Main begins program execution. public static void Main() { // Instance of OutputClass OutputClass outCl = new OutputClass("Hail University... print using C# classes"); // Call Output class' method outCl.printString(); Console.ReadLine(); } Example #3: Classes Hail University... print using C# classes 9

In C#, there are two types of class members: 1.instance 2.static.  Instance class members belong to a specific occurrence of a class.  Every time you declare an object of a certain class, you create a new instance of that class.  The ExampleClass Main() method creates an instance of the OutputClass named outCl. public static void staticPrinter() { Console.WriteLine("There is only one of me."); } Then you could call that function from Main() like this: OutputClass.staticPrinter(); Introduction to Classes 10

 You can create multiple instances of OutputClass with different names.  Each of these instances are separate and stand alone: OutputClass oc1 = new OutputClass("OutputClass1"); OutputClass oc2 = new OutputClass("OutputClass2"); Introduction to Classes 11

 A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the.NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.  Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called. To access a static class member, use the name of the class instead of a variable name to specify the location of the member Static class 12

 The main features of a static class are:  They only contain static members.  They cannot be instantiated.  They are sealed.  They cannot contain Instance Constructors Static class 13

public static class TemperatureConverter { public static double CelsiusToFahrenheit(string temperatureCelsius) { // Convert argument to double for calculations. double celsius = System.Double.Parse(temperatureCelsius); // Convert Celsius to Fahrenheit. double fahrenheit = (celsius * 9 / 5) + 32; return fahrenheit; } public static double FahrenheitToCelsius(string temperatureFahrenheit) { // Convert argument to double for calculations. double fahrenheit = System.Double.Parse(temperatureFahrenheit); // Convert Fahrenheit to Celsius. double celsius = (fahrenheit - 32) * 5 / 9; return celsius; } Example of a static class 14

class TestTemperatureConverter { static void Main() { System.Console.WriteLine("Please select the convertor direction"); System.Console.WriteLine("1. From Celsius to Fahrenheit."); System.Console.WriteLine("2. From Fahrenheit to Celsius."); System.Console.Write(":"); string selection = System.Console.ReadLine(); double F, C = 0; switch (selection) { case "1": System.Console.Write("Please enter the Celsius temperature: "); F = TemperatureConverter.CelsiusToFahrenheit(System.Console.ReadLine()); System.Console.WriteLine("Temperature in Fahrenheit: {0:F2}", F); break; Example of a static class 15

case "2": System.Console.Write("Please enter the Fahrenheit temperature: "); C = TemperatureConverter.FahrenheitToCelsius(System.Console.R eadLine()); System.Console.WriteLine("Temperature in Celsius: {0:F2}", C); break; default: System.Console.WriteLine("Please select a convertor."); break; } Example of a static class 16

Sample Output: Please select the convertor 1. From Celsius to Fahrenheit. 2. From Fahrenheit to Celsius. :2 Please enter the Fahrenheit temperature: 98.6 Temperature in Celsius: Please select the convertor 1. From Celsius to Fahrenheit. 2. From Fahrenheit to Celsius. :1 Please enter the Celsius temperature: Temperature in Fahrenheit:

 Use static constructor to initialize static fields in a class.  You declare a static constructor by using the keyword static just in front of the constructor name.  A static constructor is called before an instance of a class is created, before a static member is called, and before the static constructor of a derived  OutputClass also has a destructor. Destructors look just like constructors, except they have a tilde, "~", in front of them. They don't take any parameters and do not return a value.  Destructors are places where you could put code to release any resources your class was holding during its lifetime. Introduction to Classes 18

END 19