bim313 advanced programming techniques n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
BIM313 – Advanced Programming Techniques PowerPoint Presentation
Download Presentation
BIM313 – Advanced Programming Techniques

Loading in 2 Seconds...

play fullscreen
1 / 40

BIM313 – Advanced Programming Techniques - PowerPoint PPT Presentation


  • 112 Views
  • Uploaded on

BIM313 – Advanced Programming Techniques. Object-Oriented Programming. Contents. Object-Oriented Programming Objects Constructors, Destructors OOP Techniques (Interfaces, Inheritance, Polymorphism, Operator Overloading, Events). Object-Oriented Programming (OOP). Contents. What is OOP?

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'BIM313 – Advanced Programming Techniques' - elaine-moon


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
contents
Contents
  • Object-Oriented Programming
    • Objects
    • Constructors, Destructors
    • OOP Techniques (Interfaces, Inheritance, Polymorphism, Operator Overloading, Events)
contents1
Contents
  • What is OOP?
  • OOP Techniques
  • Using OOP in Console Application
what is oop
What is OOP?
  • The type of programming we have covered so far is known as functional (or procedural) programming
  • OOP is an approach to creating computer applications which uses objects
  • Code reuse is easier in OOP
  • We have already used some objects in our programs (e.g. Console, Exception, etc.)
objects
Objects
  • An object has both member variables and methods
    • structs in C contain only member variables
  • Objects are instantiatedfrom classes
  • Objects are also called instances
  • The terms classand objectare often confused, and it is important to understand the distinction
class vs object
Class vs. Object

Class

Object

Object is the implementation

Objects are createdat runtime

You can create many objects from a class

Objects are concrete

  • Class is only a definition
  • Classes are codedin source files
  • You write a class once
  • Classes are abstract
everything is an object
Everything is an object
  • Everything in C# and .NET Framework is an object!
  • even the intvariables are objects too!
object members
Object Members
  • Properties
    • Data contained in an object
    • Determine the stateof the object
    • They may be read-only, write-only, or both readable and writable
    • Example: Columbian filter coffee with milk and two sugars
  • Methods
    • Functions of objects are called methods
    • Example: AddSugar(), Mix(), etc.
visibility
Visibility
  • public
    • Public variables or methods are accessible by other objects
  • private
    • Private variables or methods are accessible only by the methods of the object itself
the life cycle of an object
The Life Cycle of an Object
  • Construction
    • Initialization of the object
    • Implemented by constructors
    • There may be several constructors
    • The code snippet ctorcan be used to create a constructor in Visual Studio
  • Destruction
    • Resources used by the object are freed
    • Implemented by a destructorfunction
    • Each class may have only one destructor function
construction
Construction
  • CupOfCoffeemyCup = new CupOfCoffee();
  • CupOfCoffeemyCup = new CupOfCoffee(“Columbian”);
  • CupOfCoffeemyCup = new CupOfCoffee(“Columbian”, true, true, 2);

Source

Filtered?

Milk?

Sugar?

constructor syntax
Constructor Syntax

class MyClass

{

public MyClass()

{

// Default constructor code

}

public MyClass(intmyInt)

{

// Non-default constructor code

}

}

destruction
Destruction
  • You can make some extra operations when an object is about to be destroyed
    • e.g. Saving the object data into a file
  • Generally the default destructor does all the work for you and you don’t need to write a destructor
destructor syntax
Destructor Syntax

class MyClass

{

~MyClass()

{

// Destructor body

}

}

  • Use the Finalize() method to call the destructor
static and instance class members
Static and Instance Class Members
  • Static Members
    • Sharedbetween all instances of a class
    • You don’t need to instantiate an object to use a static member
    • You can access static member with the classname
    • Examples: Math.Sin(), Main(), Console.WriteLine(), int.Parse(), Convert.ToDouble(), etc.
    • static methods can access only the static members of a class
  • Instance Members
    • All objects have separate instance members
    • Instance members require an instance to be used
    • You can access instance members with the name of the objects
    • Examples: Length() (of strings and arrays), ToString(), etc.
oop techniques
OOP Techniques
  • Interfaces
  • Inheritance
  • Polymorphism
  • Operator Overloading
  • Events
interfaces
Interfaces
  • An interface is a collection of methods and properties that are grouped together to encapsulate specific functionality
  • Interfaces are only some definitions, and they should be implemented in classes
    • i.e. the class supports all functionality defined in the interface
  • You can’t instantiate an interface
  • Interfaces cannot contain any code
  • Interface names generally start with ‘I’
  • A class may implement more than one interfaces
interface syntax
Interface Syntax

public interfaceIMyInterface

{

}

public class MyClass: IMyInterface

{

}

exercise sorting cars
Exercise: Sorting Cars
  • Create an enumeration of ‘CarBrands’
  • Create a class ‘Car’
  • Add two members, ‘Brand’ and ‘Price’ to ‘Car’ class definition
  • Create a constructor (use ‘ctor’ code snippet)
  • Create a ‘Display()’ method in ‘Car’ class
  • Create an array of cars in Main
  • Change ‘Car’ definition so that it implements IComparable interface
    • IComparable objects can be sorted
  • Implement the CompareTo() method
  • Sort the cars in Main
solution page 1
Solution (Page 1)

enumCarBrand { Mercedes, BMW, Honda, Toyota, Volkswagen, Mazda }

class Car : IComparable<Car> {

// Properties:

CarBrand Brand;

decimal Price;

// Constructor:

public Car(CarBrand brand, decimal price) {

this.Brand = brand;

this.Price = price;

}

// Methods

public void Display() {

Console.WriteLine(this.Brand + " - " + this.Price + " TL.");

}

public intCompareTo(Car other) {

return (int)this.Price - (int)other.Price;

}

}

solution page 2
Solution (Page 2)

classProgram

{

staticvoid Main(string[] args)

{

Car[] cars = new Car[] {

new Car(CarBrand.Volkswagen, 70000),

new Car(CarBrand.Mercedes, 100000),

new Car(CarBrand.Honda, 65000) };

Array.Sort(cars);

Console.WriteLine("Cars sorted in price:\n");

foreach (Car car in cars)

{

car.Display();

}

}

}

inheritance
Inheritance
  • Any class may inheritfrom another
  • Inherited class will have all members of base class
  • Classes in C# may derive only from a single base class directly (No multiple-inheritance)
  • Interfaces may inherit from other interfaces (maybe multiple)
  • Syntax: class InheritedClass : BaseClass { … }
inheritance syntax
Inheritance Syntax

public class MyClass: BaseClass

{

}

class MyClass : BaseClass, Interface1, Interface2

{

}

visibility in inheritance
Visibility in Inheritance
  • Publicmembers of the base class are accessible from the derived class
  • Privatemembers of the base class are not accessible from the derived class
  • Protectedmembers of the base class are accessible from the derived class but not accessible from other classes
virtual members
Virtual Members
  • Virtualmembers of the base class can be overridden by the derived class
abstract classes
Abstract Classes
  • Abstract classes can’t be instantiated directly
  • You have to derive another class from the abstract class and then the derived class can be instantiated
  • Abstract classes may have abstract members, which have no implementation in the base class, so an implementation must be supplied in the derived class
sealed classes
Sealed Classes
  • A sealed class may not be used as a base class, so no derived classes are possible
polymorphism
Polymorphism
  • Using the same method with different implementations in base and derived classes is called polymorphism
  • Example: ToString() method of the Objectclass
operator overloading
Operator Overloading

if (carA.Price > carB.Price) {

}

  • You can use the following code if you overload the < and > operators:

if (carA > carB) {

}

events
Events
  • When you click a button or move the mouse in a Windows program, eventsare raised
  • When an event is raised, an event handler method is executed
  • You can add custom events into your own classes
reference types vs simple types
Reference Types vs. Simple Types
  • Simple Types
    • int, float, double, etc.
    • string
    • object
    • struct
  • Reference Types: Contents are stored in a separate memory location (heap)
    • Classes (Objects created with the newkeyword)
    • Arrays
shallow copying vs deep copying
Shallow Copying vs. Deep Copying
  • Shallow Copying
    • When you make a copy of an object, value types are copied correctly but reference types point to the previous copies
    • Accomplished by MemberwiseClonemethod of the objectclass
      • MemberwiseClone() is a protectedmember of the object class
  • Deep Copying
    • Reference types are copied too
    • Implement the ICloneableinterface
properties
Properties
  • You can’t make range-check on public members
    • Age of a person can only be positive
    • If negative values are assigned, program may crash
  • By using properties, you can check values before they are assigned
  • You can create read-only or write-only properties
properties syntax
Properties Syntax

public intMyIntProp

{

get

{

// Property get code.

}

set

{

// Property set code.

}

}

properties example
Properties Example

private intm_Age;

public intAge

{

get{

return m_Age;

}

set{

if (value < 0) m_Age= 0;

else m_Age = value;

}

}

read only property example
Read-Only Property Example

private string FirstName;

private string LastName;

public string FullName

{

get{

return FirstName + “ ” + LastName;

}

}

application
.:. Application .:.
  • Create a Curveclass which stores a list of points
  • Create a Pointclass to be used in the Curveclass
  • Add PrintPoints() and AddPoint() methods into the Curveclass
  • Make the Curveclass cloneable
    • try shallowand deepcopies
  • Create a propertynamed CurveLength
  • Overload the ‘–’operator so that Point1 – Point2 gives the distance between them
  • Overload the ‘<’and ‘>’operators for the Curveclass which compares the curve lengths