Download presentation
Presentation is loading. Please wait.
Published byDoreen Ross Modified over 9 years ago
1
Introduction to Classes SWE 344 Internet Protocols & Client Server Programming
2
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
3
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
4
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
5
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
6
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
7
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
8
// 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
9
// 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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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: 37.00 Please select the convertor 1. From Celsius to Fahrenheit. 2. From Fahrenheit to Celsius. :1 Please enter the Celsius temperature: 37.00 Temperature in Fahrenheit: 98.60 17
18
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
19
END 19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.