Software Development Lecture 6 PDF

Document Details

EyeCatchingAffection2852

Uploaded by EyeCatchingAffection2852

Misr International Technological University, Egyptian-German College of Technology in Assiut

Dr. Hesham Abd Elnaby Mohamed

Tags

software development object-oriented programming (OOP) C# programming computer science

Summary

This document is a lecture on software development, focusing on object-oriented programming (OOP) concepts and C# examples. It discusses classes, objects, and their interactions in the context of building computer programs.

Full Transcript

Software Development ITATCIT03 Lecture 6 ‫ هشام عبدالنبى محمد‬/‫د‬ 1 Objectives  We are going to get familiar with the basic concepts of OOP – classes and objects.  We are going to explain how to use classes from the standard libraries of.NET Frame...

Software Development ITATCIT03 Lecture 6 ‫ هشام عبدالنبى محمد‬/‫د‬ 1 Objectives  We are going to get familiar with the basic concepts of OOP – classes and objects.  We are going to explain how to use classes from the standard libraries of.NET Framework.  We are going to mention some commonly used system classes and see how to create and use their instances (objects).  We are going to discuss how we can access fields of an object, how to call constructors and how to work with static fields in classes. 2 Classes and Objects  Object-oriented programming (OOP) is a programming paradigm, which uses objects and their interactions for building computer programs, which gives an opportunity to the programmer to solve intuitively (by simple logic) many of the problems that occur in the real world. 3 What Is an Object?  Software objects model real world objects. Examples of real-world objects are people, cars, goods, purchases, etc. In objects from the real world, we can distinguish the following two groups of their characteristics: States – these are the characteristics of the object which define it. Behavior – these are the specific distinctive actions, which can be done by the object.  Let’s take for example an object from the real world – "dog". The states of the dog can be "name", "fur color" and "breed", and its behavior – "barking", "sitting" and "walking".  Objects in OOP combine data and the means for their processing in one. They correspond to objects in real world and contain data and actions: Data members – Embedded in objects variables, which describe their states. Methods – They are a tool for building the objects. 4 Classes, Attributes, and Behavior The class defines the characteristics of an object (which we are going to call attributes) and its behavior (actions that can be performed by the object). The attributes of the class are defined as its own variables in its body (called member variables). The behavior of objects is modeled by the definition of methods in classes. Let’s return to the example with the dog. We would like to define a class Dog that models the real object "dog". The class is going to include characteristics which are common for all dogs (such as breed and fur color), as well as typical for the dog behavior (such are barking, sitting, walking). In this case we are going to have attributes breed and furColor, and the behavior is going to be implemented by the methods Bark(), Sit() and Walk(). 5 Objects – Instances of Classes  Usually, an object is an instance of just one class.  Creating the object of a defined class is called instantiation (creation).  The instance is the object itself, which is created at runtime.  This instance is characterized by state – set of values, associated with class attributes.  In the context of such behavior, the object consists of two things: current state and behavior defined in the class of the object.  The state is specific for the instance (the object), but the behavior is common for all objects which are instances of this class. 6 Classes in C# A class in C# is defined by the keyword class, followed by an identifier (name) of the class and a set of data members and methods in a separate code block. Classes in C# can contain the following elements: o Fields – member-variables from a certain type. o Properties – these are a special type of elements, which extend the functionality of the fields by giving the ability of extra data management when extracting and recording it in the class fields. o Methods – they implement the manipulation of the data. 7 An Example Class We are going to give an example of a class in C#. The class Cat models the real-world object "cat" and has the properties Name and Color. The given class defines several fields, properties and methods, which we are going to use later. You can now see the definition of the class in the next slide. 8 Class Cat 9 Dealing with Class Cat The example class Cat defines the properties Name and Color, which keep their values in the hidden (private) fields name and color. Furthermore, two constructors are defined for creating instances of the class Cat, respectively with and without parameters, and a method of the class SayMiau(). After the example class is defined we can now use it in the following way: 10 System Classes Calling the method Console.WriteLine(…) of the class System.Console is an example of usage of a system class in C#. We call system classes the classes defined in standard libraries for building applications with C#. Such are for example the classes String, Environment and Math, which we are going to consider later. 11 Creating and Using Objects - I The creation of objects from defined classes during program execution is performed by the operator new. The newly created object is usually assigned to the variable from type coinciding with the class of the object. We are going to note that in this assignment the object is not copied, and only a reference to the newly created object is recorded in the variable (its address in the memory). Here is a simple example of how it works: We assign the newly created instance of the class Cat to the variable someCat of type Cat, The variable someCat remains in the stack, and its value (the instance of the class Cat) remains in the managed heap: 12 Creating and Using Objects - II  Creating Objects with Set Parameters o We are going to consider a slightly different variant of the example above in which we set parameters when creating the object: we would like the objects someCat to represent a cat whose name is "Johnny" and its color is “brown”. We indicate this by using the words "Johnny" an "brown", written in the brackets after the name of the class. When creating an object with the operator new, two things happen: memory is set aside for this object and its data members are initialized. As the member variable name and color of the class Cat are of reference type (of the class String) they are also recorded in the dynamic memory (heap) and in the object itself are kept their references (addresses / pointers). o The following figure illustrates how the Cat object is represented in the computer memory (arrows illustrated the references from one object to another): 13 Access to the Fields and Methods of an Object The access to the fields, methods, and properties of a given object is done by the operator. 14 Constructors - I  The constructor is a special method of the class, which is called automatically when creating an object of this class, and performs initialization of its data.  The constructor has no type of returned value and its name is not random, and mandatorily coincides with the class name.  The constructor can be with or without parameters. A constructor without parameters is also called parameterless constructor.  The constructor can take parameters as well as any other method. Each class can have different count of constructors with one only restriction – the count and type of their parameters have to be different (different signature). 15 Constructors - II 16 Static Fields and Methods - I  The data members, which we considered up until now implement states of the objects and are directly related to specific instances of the classes.  In OOP there are special categories fields and methods, which are associated with the data type (class), and not with the specific instance (object). We call them static members because they are independent of concrete objects.  Furthermore, they are used without the need of creating an instance of the class in which they are defined. They can be fields, and methods.  A static field or method in a given class is defined with the keyword static, placed before the type of the field or the type of returned value of the method.  When to Use Static Fields and Methods? To find the answers of this question we have to understand very well the difference between static and non-static members.  Let’s interpret the class as a category of objects, and the object as a representative of this category. Then, the static members reflect the state and the behavior of the category itself, and the non-static represent the state and the behavior of the separate representatives of the category. 17 Static Fields and Methods - II  We are going to pay special attention to the initialization of static and non-static fields.  We already know that non-static fields are initialized with the call to the constructor of the class when creating an instance of it.  However, the initialization of static fields cannot be performed when the object of the class is created, because they can be used without a created instance of the class.  It is important to know the following: Static fields are initialized when the data type (the class) is used for the first time, during the execution of the program. 18 Static Fields and Methods - III  The example, which we are going to give, solves the following simple problem: we need a method that every time returns a value greater with one than the value returned at the previous call of the method.  Let’s assume that the method is called NextValue() and is defined in a class called Sequence. The class has a field currentValue from type int, which contains the last returned value by the method.  We would like the following two actions to be performed consecutively in the method body: the value of the field to be increased and its new value to be returned as a result.  Obviously the returned by the method value does not depend on the concrete instance of the class Sequence. For this reason the method and the field are static. You can now see the described implementation of the class: 19 Static Fields and Methods - IV The reader has noticed that the defined class has a default constructor, which is declared as private. This usage of a constructor may seem strange, but is quite deliberate. It is good to know the following: A class that has only private constructor cannot be instantiated. Let’s take a look at a simple program, which uses the class Sequence: 20 Examples of System C# Classes  The static property TickCount of the class Environment returns the count of milliseconds that have passed since the computer is on until the time of the method call. With its help we detect the milliseconds past before and after the execution of the source code. Their difference is the wanted time for the execution of the fragment source code measured in milliseconds.  As a result of the execution of the program on the standard output, we print the result of the following type (the measured time varies according to the current computer configuration and its load): 21 System.Math Class The System.Math class contains methods for performing basic numeric and mathematical operations such as raising a number to a power, taking a logarithm and square root, and some trigonometric functions. 22 System.Random Class  Sometimes in programming we have to use random numbers. For instance, we would like to generate 6 random numbers in the range 1 to 49. This could be done by using the System.Random class and its method Next(). We can randomly generate a number in the range [0…n) by calling the method Next(n). Notice that this method can return zero, but always returns a random number smaller than the set value n. Therefore, if we would like to get a number in the range [1…49], we have to use the expression Next(49) + 1.  Below is an example source code of a program, which generates 6 random numbers in the range from 1 to 49 by using the Random class. 23 Thanks 24

Use Quizgecko on...
Browser
Browser