Top Banner
WELCOME TO OBJECT ORIENTED PROGRAMMING WITH C#.NET
38

WELCOME

Mar 18, 2016

Download

Documents

MAXIMA

WELCOME. TO. Object oriented programming with C#.NET. Features of Object Oriented Programming:. Any programming language to become as object oriented should follow the following features. Abstraction Encapsulation Inheritance Polymorphism. What is an Abstraction?. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: WELCOME

WELCOME

TOOBJECT ORIENTED PROGRAMMING WITH C#.NET

Page 2: WELCOME

Features of Object Oriented Programming:

Any programming language to become as object oriented should follow the following features.

AbstractionEncapsulationInheritancePolymorphism

Page 3: WELCOME

What is an Abstraction?

It’s a process of Hiding the Implementation but providing the service. There are two types of abstraction available: 1. Data Abstraction2. Functional Abstraction

It is a process of Binding the member variable of a class along with member functions.Encapsulation can be implemented with the help of object and also Access modifiers like Private ,public and Protected etc.

What is an Encapsulation?

Page 4: WELCOME

What is an Inheritance?

This is process of creating a new class from already existing classIn inheritance process existing class is known as Parent/Base class, newly created class is know as Derived/Child class.In inheritance process, child class will get all the features of Parent/Base class.Main purpose of inheritance is code Re-usability and providing additional functionality/enhancement.

Page 5: WELCOME

What is Polymorphism?

It’s derived from a Greek word, where poly means many morph means Faces/Behaviors.Types of Polymorphism:There are two types of polymorphism1. Static polymorphism/Compile Time polymorphism

/Early Binding2. Dynamic polymorphism/Run Time polymorphism/late

Binding Static polymorphism is achieved using, Function over

loading, operator over loading. Dynamic polymorphism is achieved by using , Function

overriding.

Page 6: WELCOME

Class in C# .NET

A class is a collection of things which posses common similarities.

In C#.NET a class is a user defined Data type and is Known as Reference Type.

A class can contain following members in C#.NET

1. Data Fields2. Function3. Constructors4. Destructors5. Properties6. Indexers7. Events

Page 7: WELCOME

1.Data Fields

It’s used to store the data related to the class. Except Data Fields no other member of a class can store the data.To declare any Data Field, we use the following

Syntax

Access modifier Data type Data Field Name = [Initializing value];

Ex:Public int Empid;

Page 8: WELCOME

2.FUNCTIONS

Any programming language will have methods.

A named block of code is refer as a Method or subprogram.

It can be either value returning or non value returning.

Syntax[<modifiers>]<type|void><name>([<pram

def’s])

{

statements;

};

Page 9: WELCOME

Syntax to create a class

Access Modifier class Class Name

Syntax to create an Object

Class Name Object Name=new Class Name([args list])

Constructor name

Ex: Program p= new program() orProgram p;P=new program();

Page 10: WELCOME

How to open Microsoft visual studio .NET

Open V.S Go to File New project Change the language as C# template as Console application Named return as OOPS Project and the write the following code under the default class program

Class program{//No input and out put Void Text1 () { Console.WriteLine (“first method”); }

Page 11: WELCOME

// No output but has Input Void Text2 (int x, int y)

{ For (int i=1;i<=y;i++)

Console.writeLine (“{0} *{1}={2}”;x, i, x*i); }

//No Input but has out put String Text3 ()

{Return “third method”;

}//has input and out put

String text4 (string name){

return “Hello” +name;}

Page 12: WELCOME

//Method with Multiple output parametersVoid Meth1 (int x, int y, ref int a, ref int b) {a=x+y;b=x/y;}Static VoidMain (string [] args){Program p=new program ();p.Text1 (); p.Text2 (5, 10);Console.WriteLine (p.Text3 ());Console.WriteLine (p.Text4 (“venkat”));Int m=0, n=0; //Initialization is mandatoryp.Math1 (100, 50, ref m, ref n);Console.WriteLine (M+” “+n);Int q, r; //Initialization is optionalp.Math2 (100, 50, out q, out r);Console.WriteLine (q+” “+r);Console.Readline ();}}

Page 13: WELCOME

Constructors

A constructor is a member method of a class which is invoked automatically when an object to the class is created.A constructor name should be same name as class name .A constructor does not have any return type even void also.A constructor is used to keep something ready for the object when it is created .

Syntax[<modifiers>]<Name>([<Param Def’s>]){Statements;}

Page 14: WELCOME

Example program

Class ConDemo{ConDemo (){Console.WriteLine (“Constructor”);}Void Demo (){Console.writeLine (“Method”);}Static VoidMain (){ConDemo cd1=new ConDemo ();ConDemo cd2=new ConDemo ();ConDemo cd3=cd2;Cd1.Demo (); cd2.Demo (); cd3.demo ();Console.ReadLine ();}}

Add a class ConDemo.cs and write the following code

Page 15: WELCOME

Constructors are TWO Types1

2

Default Constructor

Parameterized Constructor

A constructor without any parameters is a “Default constructor” which can be defined either by the programmer or will be defined by the compiler provided the class doesn't contain any constructor in it.A constructor with parameters is referred as “parameterized constructor” which can be defined only by a parameters.If at all constructors are parameterized we can send values to the parameters at the time of object creation.

Page 16: WELCOME

Example programAdd a class Maths.cs and write the following codeClass Maths{Int x, y; //class variablesMaths (int x, int y ) //block variables{This.x=x;This.y=y;}Void Add (){Console.WriteLine(x+y);}Void Sub()Console.WriteLine(x-y);}Void Mul (){Console.WriteLine(x*y);}

Page 17: WELCOME

Void Div (){Console.WriteLine(x/y);}Static VoidMain (){Maths m=new Maths (100, 50);m.Add (); m.Sub () ;m.Mul();m.Div();Console.ReadLine ();}}

Page 18: WELCOME

INHERITANCE

The process creating a new class form already existing class.Existing class is known as Parent Class or Base Class.New class is known as Child Class or Derived Class.

Child class will get all the features of parent classCode Reusability and providing additional functionalities

consume

members

class1

Parent child Relation

Page 19: WELCOME

Syntax

[<Modifiers>] class <C C Name>:<P C Name>

Class Class1{ Members}Class Class2 : Class1{ Consume the parent members here}

Child Class Parent Class

Page 20: WELCOME

Example program

Add a class class1.cs and write the following code

Class class1{Public class1 (){Console.WriteLine (“class1 constructor”);}Public void Text 1(){Console.writeLine (“Method one”);}Public void Text2(){Console.WriteLine (“Method Two”);}}

Add a class class2.cs and write the following code

Class class2:class1{Public class2 (){Console.writeLine (“class 2 constructor”);}Public void Text3 (){Console.writeLine (“Method Three”);}Static VoidMain (){Class2 c=new class2 ();c.Text1 ();c.Text12 ();c.Text3 ();console.Readline ();}}

Page 21: WELCOME

Polymorphism

Polymorphism can be implemented with the help of TWO approaches

Over Loading

Overriding

Same name different signatures are called overloading method

Same name and same signature is called overriding method

Page 22: WELCOME

Example program

Add a class Load demo.cs and write the following code

Class LoadDemo{Public void show (){Console.WriteLine (1);}Public void show (int x){Console.WriteLine (2);}Public void show (string s){Console.WriteLine (3);}

Public void show (int x, string s){Console.WriteLine (4);}Public void show (string s, int x){Console.WriteLine (5);}Static VoidMain (){Load demo obj =new LoadDemo ( );Obj. Show (10);Obj. show (“Hello”);Obj. Show (10,”Hello”);Obj. Show (“Hello”, 10);Console.Readline ();}}}

Page 23: WELCOME

Properties

A property is a member of a class , used to write the data in the data field and read the data from the data field of a class.A property can never store the data, just used to transfer the data.Properties are members that provide a flexible mechanism to read, write or compute the values of private fields.Properties enable a class to expose a public way of getting and setting values, while hiding implementation or verification code.To perform read and write operations, property can contain two assessors/methods.

Page 24: WELCOME

Assessors with in the property

Accessor enable data to be accessed easily while still providing the safety and flexibility of methods.

Set Accessor Get Accessor

Page 25: WELCOME

Set Accessor

Set accessor is used to write the data into the data field.This will contain a default and fixed variable named as “value”.Whenever we call the property to write the data, any data we supply will come and store in value variable by default.

Set{Data File Name=value;}

Get AccessorGet Accessor is used to read the data field, using this accessor we can’t write the data.

Get{Return Data File Name:}

Syntax

Page 26: WELCOME

Types of properties C#.NET will support three types of Properties.

1 •Read Only Property

2 •Write Only Property

3 •Read Write property

This property is used to read the data from the Data field

It is used to write the data into Data field of a class.

It is used to read the data from the Data field and to write the data in to the Data Field

Access Modifier Data Type property Name{ set { Data Field Name=Value; }}

Access Modifier Data Type Property Name{ get { return data Field Name; }}

Page 27: WELCOME

Access Modifier Data Type property Name{ set { Data Field Name=Value; } get { return data Field Name; }}

Namespace CAProperties{Class clsEmpolyee{ int EmpId,EAge; Public int PEmpId { set { EmpId=value; } get { return EmpId; } }

Syntax Example

Page 28: WELCOME

Public int PEAge { set { EAge=Value; } get { return EAge; } }}Class ClsProperty1{ Static void Main(string[] args) { clsEmployee obj1=new clsEmployee(); Console.write(“Enter Employee details:”); Obj1.PEmpId=Concert.ToInt32(Console.ReadLine()); Obj1.PEAge=convert.ToInt32(console.ReadLine()); } }}

Page 29: WELCOME

Abstract

It is a method with can have only syntax, but no implementation is called Abstract Method.It should be overridden in the Sub Class.It can be used mostly in Distributed Applications (Client/sever applications)

AccessSpecifier abstract Return Type

Method Name(parameters);

Syntax

Public abstract void Hello();Example

Page 30: WELCOME

Interfaces

In the .NET Framework, interfaces have a special naming convention. All interface name begin with a capital I. Interfaces can have properties as we difference from a class.

1. You cannot create an object of an interface2. An interface does not contain any constructor.3. All methods in an interface are abstract.4. An interface can inherit multiple interfaces.

[<modifiers>] interface<name>

{Abstract member declarations

}

Syntax

Page 31: WELCOME

Add an interface inter1.cs and write the following code

Interface inter1{ Void Add (int x, int y); Void sub (int x, int y); Void test (); }}Add an interface inter2.cs and write the following codeInterface inter2{ Void Mul (int x, int y); Void Div (int x, int y); Void test (); }}

To implement both the above two interfaces Add a class interclass.cs and write the following code

Class Interclass: Inter1, Inter2{ Public void Add (int x, int y) {Console.Writeline(x+y); }Public void Sub (int x, int y) {Console.Writeline(x-y);}Public void Mul (int x, int y) {Console.Writeline(x*y);}

Page 32: WELCOME

Public void Div (int x, int y) {Console.Writeline(x/y)}public void Test (){Console.Writeline (“Declared under multiple interfaces”);}Void Inter1.Test (){Console.Writeline (“Method of Interfaced 1”);}Void Inter2.Test (){Console.Writeline (“Method of Interfaced 2”);}

Static Void Main (){Inter class obj= new Inter class ();Obj. Add (100, 45);Obj. Sub (100, 45);Obj. Mul (100, 45);Obj.Div (100, 45);Obj. Test ();Inter1 i1 = obj; Inter i2 = obj;I1.Test (); i2.Test ();Console.ReadLine ();}}

 

Page 33: WELCOME

Delegates

It’s used to represent or refer one or more functions.Delegates are user defined types in C#.NET.Delegates in C#.NET are similar to function pointers in C++.It is not a member of a class, but similar to a class.To consume any delegate , we need to create an object to delegate. Delegate is a type that references a method.

The delegate method can be invoked like any other method with parameters and written value.These can be used to define callback methods.There are the backbone for events.

Page 34: WELCOME

Types of Delegates

There are of two types of Delegates available.a) Single cast delegateb) Multi cast delegate A delegate that represents only function is known as

Single Cast Delegate. A delegate that represents only a more than one functions

is known as Multi Cast Delegate.

To work with Delegate Use the Following Steps:Creating a DelegateInstantiating a DelegateInvoking a Delegate

Page 35: WELCOME

EX: If we consider a function, like Public Void Add (int a , int b ){Code……}To refer this function, if we want to use the Delegate, we use the following steps like…..

STEP 1 Creating A Delegate

Access modifier delegate return type Delegate name([arguments list])EX: public delegate void sampleDelegate(int x, int y)

Syntax

Page 36: WELCOME

STEP 2 Instantiating a Delegate

Delegate name object name = new Delegate name (Target function name);

EX: SampleDelegate obj=new SampleDelegate(Add);

STEP 3 Invoking a Delegate

Object Name([Arguments Values])

EX: Obj(10,20)

Syntax

Syntax

Page 37: WELCOME

Add a class del.cs and write the following the codeClass Delclass{ Public String SayHello(string name0 { Return “Hello” +name; }Public delegate string SayDel(String name);Static void Main() {Delclass obj=new Delclass();SayDel Sd=new SayDel(obj.sayHellow);Console.WriteLine(Sd(“xxx”));Console.WrireLine(Sd(“yyy”));Console.WrireLine(Sd(“zzz”));Console.ReadLine(); }}

Program

Page 38: WELCOME