c sharp - oo programming

40
Lab Excercise C Sha rp Programs : By PTSM 1 Object Oriented Programing for su pport: ptsm.4u@ gamil.com A class that represents a Point on the co-ordinate system. Setter begin with Set<variableName>(parameters)   SetRollno(..), SetName(..) Gett er begin wi th Get <variableName>()   GetRollno(), GetName() A boolean Gett er begin with Is<variableName>()   IsFull(), IsOpened(), IsAlive() class Point {  // data - variables private int x; private int y ;  // code  methods public void Set X (int xx) { x = xx; } public void Set Y (int yy){ y = yy; } public int Get X (){ return x; } public int Get Y (){ return y; } } p (x, y) 15 10 Setters Getters Attributes  X Y (0,0) Fields are used to store data. Methods are used to  perform operations on data. Class defines fields and , methods. Encapsulation: putting data and code together- binding of behavior and state to a particular object .  Abstraction: providing essential and relevant  features and ignoring the rest.

Upload: duggapu

Post on 09-Apr-2018

243 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 1/40

Lab Excercise C Sharp Programs : By PTSM

1 Object Oriented Programing for support: [email protected] 

A class that represents a Point on the co-ordinate system.

Setter begin with Set<variableName>(parameters) – SetRollno(..), SetName(..)

Getter begin with Get<variableName>() – GetRollno(), GetName()

A boolean Getter begin with Is<variableName>() – IsFull(), IsOpened(), IsAlive()

class Point {

 // data - variables 

private int x;

private int y;

 // code – methods 

public void SetX (int xx) {

x = xx;

}

public void SetY (int yy){

y = yy;

}

public int GetX (){

return x;

}public int GetY (){

return y;}

}

p (x, y)

1510

Setters

Getters

Attributes

 

X

Y

(0,0)

Fields are used to store

data.

Methods are used to

 perform operations on

data.

Class defines fields and ,

methods.

Encapsulation: putting

data and code together-

binding of behavior and 

state to a particular 

object .

 Abstraction: providingessential and relevant 

 features and ignoring the

rest.

Page 2: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 2/40

Lab Excercise C Sharp Programs : By PTSM

2 Object Oriented Programing for support: [email protected] 

class TestPoint {

static void Main () {

Point p; //object reference variable

p = new Point (); //creating an object and assigning an address to a variable

// or – above 2 lines can be combined into 1 line

// Point p = new Point();

// calling object’s setters – write operation 

p.SetX (15); 

p.SetY (10); 

// calling object’s getters – read operation 

int x, y;

x = p.GetX (); 

y = p.GetY (); 

Console.WriteLine(“ x = “ + x); 

Console.WriteLine(“y = “ + y); 

}

}

From outside 

From outside

DATA hidden and all operations to data are performed though METHODS of the object. 

150

p150

x

y

x

y

15

10

GetX

SetX

GetY

SetY

p

DATA (variables)

CODE (methods) 

CLASS

state

behavior

p.x = 10; // direct access

NULL

pOBJECT

Page 3: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 3/40

Lab Excercise C Sharp Programs : By PTSM

3 Object Oriented Programing for support: [email protected] 

Class:

Definition: A class is a blue print that defines the variables and methods common to all objects

of a certain kind. This allows you to create objects.

Object:Definition: An object is a software bundle of variables and related methods.

Object Life Cycle

As we work with objects in OOP, understanding

how objects are born,

live their lives,

and die is important.

Point p;p = new Point(); 

1.  Declaration:

Variable p will be used to refer to a Point object. Notice that a class name is used as

the variable's type.

2.  Instantiation:

new is a C Sharp operator that creates the new object (allocates space for it).

An object is an instance of a class.

3.  Initialization:

Point() is a call to Point's constructor, which initializes the object. 

Using Object

Once you've created an object, you probably want to use it for something.

You may need information from it,

want to change its state, or have to perform some action.

Destroying an object

When it's time for the object to die, the object is removed from memory and .NET dropsits internal reference to it. You don't have to destroy objects yourself. A special part of 

the .NET runtime called the garbage collector (GC) takes care of destroying all objects

when they are no longer in use.

Page 4: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 4/40

Lab Excercise C Sharp Programs : By PTSM

4 Object Oriented Programing for support: [email protected] 

RTC Bus Station

Classes :  C1, C2 and C3 

Objects:  A, B, C, D, E, F, G, H and I 

BUS PERSON

 CAR

A B C

DE F

G H I

C1

C2

C3

Page 5: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 5/40

Lab Excercise C Sharp Programs : By PTSM

5 Object Oriented Programing for support: [email protected] 

Input from keyboard

class TestPoint 2 {

static void Main () {

Point p = new Point();

int x, y;

Console.WriteLine(“Enter value for X”); 

x = int.Parse( Console.ReadLine());

Console.WriteLine(“Enter value for Y”); 

y = int.Parse( Console.ReadLine());

p.SetX(x);

p.SetY(y);

x = p.GetX();

y = p.GetY();

//Console.WriteLine(“ x = “ + x + “, y= ” + y); 

Console.WriteLine(“x={0}, y={1}”,x, y); 

//Console.WriteLine(“x = “ + p.GetX() + “, y= “ + p.GetY());

}

}

Assigning object reference variable : Do not creates a copy of the object, you are only making a

copy of reference.

Point p1,p2;p1 = new Point();

p2 = p1;

In ut

OR

150

150

p1

p2

x

y

150

Page 6: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 6/40

Lab Excercise C Sharp Programs : By PTSM

6 Object Oriented Programing for support: [email protected] 

UML (Unified Modeling Language) 

Class Diagram:

The UML class diagram is a graphical notation used to construct and

visualize object oriented systems.

A UML class diagrams made of:

A set of classes and

A set of relationships between classes.

Classes:

A consists of three parts

1. Class name

2. Class attributes(fields)

3. Class operations(methods)

Note: Name should be center aligned; Attributes and Operations should be left aligned.

Class Name

Class Attributes 

Class Operations 

Page 7: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 7/40

Lab Excercise C Sharp Programs : By PTSM

7 Object Oriented Programing for support: [email protected] 

Class Name:

1. The name of the class

Class Attributes:

1. The name of the each attribute

2. The attribute type is shown after the colon (attribute name : attribute type)

3. Attributes map onto the member variables (data members) in the code

Class Operations:

1. The name of the each operation. It is a service the class provides

2. The type of method parameters are shown after the colon following the

parameter name

3. The return type of a method is shown after the colon at the end of the method

signature (operation name : return type)

4. Operations map onto the class methods in code.

Visibility and Access for attributes and operations of a class  

The +, -, # and ~ symbols before an attribute and operation name in a class denote the

visibility of the attributes and operations.

+ denotes public attribute or operation 

- denotes private attribute or operation

# denotes protected attribute or operation

~ denotes namespace/package attribute or operations

Page 8: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 8/40

Page 9: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 9/40

Lab Excercise C Sharp Programs : By PTSM

9 Object Oriented Programing for support: [email protected] 

Example:

ConsoleApp

Point TestPoint

- x : int

- y : int

+ SetX(x : int) : void + Main() : void

+ GetX() : int

+ SetY(y : int) : void

+ GetY() : int

Page 10: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 10/40

Page 11: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 11/40

Lab Excercise C Sharp Programs : By PTSM

11 Object Oriented Programing for support: [email protected] 

Understanding this keyword

class Point {

private int x;

private int y;

public void SetX (int xx) {

x = xx;

}

public void SetY (int yy){

y = yy;

}

public int GetX (){

return x;

}

public int GetY (){

return y;

}

}

Within an instance method or a constructor, this is a reference to the current object - the object

whose method or constructor is being called. You can refer to any member of the current object from

within an instance method or a constructor by using this.

“this” with fields 

The most common reason for using the this keyword is because a field is shadowed by a

method or constructor parameter.

“this” with constructors 

From within a constructor, you can also use the this keyword to call another constructor

in the same class. Doing so is called an explicit constructor invocation.

“this” used as a method return type

Sometimes a method may want to return current object.

class Point {

private int x;

private int y;

public v oid SetX (int xx) {

this.x = xx;

}

public v oid SetY (int y y ) {

this.y = yy;

}

public int GetX () {

return this.x; 

}

public int GetY () {

return this.y; 

}

class Point {

private int x;

private int y;

public v oid SetX (int x) {

this.x = x;

// x=x;

}

public v oid SetY (int y ) {

this.y = y;

// y=y;

}

public int GetX () {

return x; 

}

public int GetY () {

return y; 

}

Page 12: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 12/40

Lab Excercise C Sharp Programs : By PTSM

12 Object Oriented Programing for support: [email protected] 

class Point {

private int x;

private int y;

public void SetX(int x) {

this.x = x; 

//x = x;

}

public int GetX() {

return x;

}

… 

… }

Point p1, p2;

//1st object 

p1 = new Point();

//2nd object 

p2 = new Point();

p1.SetX(10); //1st 

p2.SetX(20); //2nd 

X

Y

p1-150 p2-250

X

Y

x = 10

this = 150

x = 20

this = 250

p1.SetX(10); //1st 

P2.SetX(20); //2nd 2nd call

1st call

STACK 

Page 13: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 13/40

Lab Excercise C Sharp Programs : By PTSM

13 Object Oriented Programing for support: [email protected] 

Points to remember

1.  When a method is called, it is automatically passed an implicit argument that is reference to

the invoking object.

2.  This implicit reference is called as this reference.

3.  Instance variables and methods are automatically prefixed with this reference so that they

access correct object

Page 14: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 14/40

Lab Excercise C Sharp Programs : By PTSM

14 Object Oriented Programing for support: [email protected] 

Book details – reducing number of setters and getters 

class Book {private string title;

private string author;

private double cost;

public void SetTitle(String title){

this.title = title;

}

public string GetTitle(){

return title;

}

public void SetAuthor(string author){

this.author = author;

}

public string GetAuthor(){

return author;

}

public void SetCost(double cost){

this.cost = cost;

}

public double GetCost(){

return cost;

}

}

Class Book {

private String title;

private string author;

private souble cost;

public void SetDetails(string title 

string author,

double cost 

){

this.title = title;

this.author = author;

this.cost = cost;

}

public void GetDetails(){

Console.WriteLine (title);

Console.WriteLine (author);

Console.WriteLine (cost);

}

}

Page 15: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 15/40

Lab Excercise C Sharp Programs : By PTSM

15 Object Oriented Programing for support: [email protected] 

class TestBook {

static void Main() {Book b1,b2;

// 1st

book 

b1 = new Book();

b1.SetTitle(“Complete Reference”); 

b1.SetAuthor(“Herbert Schildt”); 

b1.SetCost(450.00);

// 2nd

book b2 = new Book();

b2.SetTitle(“ASP.NET”); 

b2.SetAuthor(“Stephen Walther”); 

b2.SetCost(650.00);

Console.WriteLine(“Book #1”); 

Console.WriteLine (“----------------------------“); 

Console.WriteLine (“\tTitle: “ + b1.GetTitle());

Console.WriteLine (“\tAuthor: “ + b1.GetAuthor());Console.WriteLine (“\tCost: “ + b1.GetCost());

Console.WriteLine (); // empty line

Console.WriteLine (“Book #2”); 

Console.WriteLine (“----------------------------“); 

Console.WriteLine (“\tTitle: “ + b2.GetTitle());

Console.WriteLine (“\tAuthor: “ + b2.GetAuthor());

Console.WriteLine (“\tCost: “ + b2.GetCost());

}

}

b1.SetDetails(“Complete Reference”, 

”Herbert Schildt”, 

450.00

b2.SetDetails(“ASP.NET”, 

”Stephen Walther”, 

650.00);

b1.GetDetails();

b2.GetDetails();

Page 16: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 16/40

Lab Excercise C Sharp Programs : By PTSM

16 Object Oriented Programing for support: [email protected] 

// this code reduces printing process 

class TestBook2 {

static void Main() {Book b1,b2;

// 1st

book 

b1 = new Book();

b1.SetTitle(“Complete Reference”); 

b1.SetAuthor(“Herbert Schildt”); 

b1.SetCost(450.00);

// 2nd

book 

b2 = new Book();b2.SetTitle(“ASP.NET”); 

b2.SetAuthor(“Stephen Walther”); 

b2.SetCost(650.00);

Console.WriteLine (“Book #1”); 

Console.WriteLine (“----------------------------“); 

TestBook2.Print(b1) ;

Console.WriteLine (); // empty line

Console.WriteLine (“Book #2”); 

Console.WriteLine (“----------------------------“); 

TestBook2.Print(b2) ;

}

// static method – to invoke this method ; use “class name followed with method name” 

public static void Print(Book b){

Console.WriteLine (“\tTitle: “ + b.GetTitle());

Console.WriteLine (“\tAuthor: “ + b.GetAuthor());

Console.WriteLine (“\tCost: “ + b.GetCost());

}

}

Page 17: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 17/40

Lab Excercise C Sharp Programs : By PTSM

17 Object Oriented Programing for support: [email protected] 

// Array of objects 

class TestBook2 {

static void Main() {Book[] b;

b = new Book[3]; // array of 3 books

string title, author;

double cost;

for(int I =0; I<3; i++) {

Console.WriteLine (“Enter “ + (i+1) + “ “ + “Book Details”); 

Console.WriteLine (“Title”); 

title = Console.ReadLine();Console.WriteLine (“Author”); 

author = Console.ReadLine(); System.out.println(“Cost”); 

Console.WriteLine(“Cost”); 

cost = double.Parse(

Console.ReadLine ());

// creating an object

b[i] = new Book();

// placing valuesb[i].SetTitle(title);

b[i].SetAuthor(author);

b[i].SetCost(cost);

}

for(int I = 0; i<3; i++){

Console.WriteLine (“Book # “ + (i+1) ); 

Console.WriteLine (“---------------------------“); 

Console.WriteLine (“\tTitle: “ + b[i].GetTitle());

Console.WriteLine (“\tAuthor: “ + b[i].GetAuthor());

Console.WriteLine (“\tCost: “ + b[i].GetCost());

Console.WriteLine (); // empty line

}

}

b

0

1

2

b[0]

titlea 

author

 cost

title

 author

 cost

title

 author

 cost

b[2]

b[1]

Page 18: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 18/40

Lab Excercise C Sharp Programs : By PTSM

18 Object Oriented Programing for support: [email protected] 

// Additional methods and providing validation logic 

class Number {

private int n;

public bool SetN(int n){

If(n>0 && n<150){

this.n = n;

return true;

}else{

Console.WriteLine (“Sorry.. !.. Invalid Number (required 1..150)”); 

this.n=0;

return false;

}

}public int GetN(){

return n;

}

public int Square(){

return n*n;

}

public int Cube(){

return n*n*n;

}

public int Factorial(){int f=1;

while(n>0){

f=f*n;

n=n-1;

}

return f;

}

public int NoOfDigits(){

string str = n.ToString();

return str.length();

}

}

validation

additional methods (optional)

Page 19: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 19/40

Lab Excercise C Sharp Programs : By PTSM

19 Object Oriented Programing for support: [email protected] 

class TestNumber {

static void Main() {

Number nobj = new Number();

int n;

bool status;

Console.WriteLine (“Enter Number (1..150) “); 

n = int.Parse(Console.ReadLine());

status = nobj.SetN(n);

if(status==true) {

Console.WriteLine (“Number: “ + nobj.GetN());Console.WriteLine (“Square: “ + nobj.Square() + “, Cube: “ + nobj.Cube());

Console.WriteLine (“No.of digits: “ + nobj.NoOfDigits());

}

}

}additional methods

setters & getters

Page 20: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 20/40

Lab Excercise C Sharp Programs : By PTSM

20 Object Oriented Programing for support: [email protected] 

// Constructors 

class DefaultConstructor {

private int v1;

private double v2;

private string v3;

public void SetValues(int v1,

double v2,

String v3) {

this.v1 = v1;

this.v2 = v2;

this.v3 = v3;}

public void GetValues() {

Console.WriteLine ("v1=" + v1);

Console.WriteLine ("v2=" + v2);

Console.WriteLine ("v3=" + v3);

}

}

class DefaultConstructor {

private int v1;

private double v2;

private string v3;

public DefaultConstructor() {

v1 = 0; //default

v2 = 0.0; //default

v3 = null; //default

}

public void SetValues(intv1,

double v2,

string v3) {

this.v1 = v1;

this.v2 = v2;

this.v3 = v3;

}

public void GetValues() {

Console.WriteLine ("v1=" + v1);

Console.WriteLine ("v2=" + v2);

Console.WriteLine ("v3=" + v3);

}

DefaultConstructor d;

D = new DefaultConstructor();

Page 21: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 21/40

Lab Excercise C Sharp Programs : By PTSM

21 Object Oriented Programing for support: [email protected] 

class TestDefaultConstructor {static void Main() {

DefaultConstructor d;

d = new DefaultConstructor();

d.GetValues();

Console.WriteLine(); //empty line

d.SetValues(10,1.25,"scott");

d.GetValues();

}

}

0

0.0

null

10

1.25

scott

V1

V2

V3

V1 = 0

V2 = 0.0

V3 = null

V1

V2

V3

V1 = 10

V2 = 1.25

V3 = scott

output

output

Page 22: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 22/40

Lab Excercise C Sharp Programs : By PTSM

22 Object Oriented Programing for support: [email protected] 

// Explicit Constructor 

class InternetURL {

private string url;

public InternetURL() {

url = "http://www.google.co.in/index.html";

}

public void SetUrl(string url) {

this.url = url;

}

public string GetUrl() {

return url;}

}

// In the main 

InternetURL u; InternetURL u;

u = new InternetURL(); //u = new InternetURL(); // error

u = new InternetURL(“http://www.google.co.in/index.html");

class InternetURL {

private string url;

public InternetURL(string url) {

this.url = url;

}

/*public void SetUrl(string url) {

this.url = url;

}*/

public string GetUrl() {return url;

}

}

Page 23: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 23/40

Lab Excercise C Sharp Programs : By PTSM

23 Object Oriented Programing for support: [email protected] 

// Multiple Constructors – constructor overloading; the role of “this” keyword 

class InternetURL {private string url;

public InternetURL() {

url = null;

//or

url = “http://www.google.co.in/ 

index.html";

}

public InternetURL(string url) {

this.url = url;}

public void SetUrl(string url) {

this.url = url;

}

public string GetUrl() {

return url;

}

}

// In the main 

InternetURL u;

u = new InternetURL();

//or

u = new InternetURL(“http://www.google.co.in/index.html"); 

Note: If you want , some additional methods can also be provided.

Ex: Protocol(), Domain(), FileName() etc.

class InternetURL {

private string url;

public InternetURL() {

this("http://www.google.co.in/

index.html");

}

public InternetURL(string url) {

this.url = url;

}

public void SetUrl(string url) {

this.url = url;

}

public string GetUrl() {

return url;

}

Page 24: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 24/40

Lab Excercise C Sharp Programs : By PTSM

24 Object Oriented Programing for support: [email protected] 

Points to remember:

1.  It is a function in the class that has the same name as the class name.

2.  Purpose: to initialize variables, allocating memory and establishing database connections

etc.

3.  A class can have 2 types of constructors: static and non-static 

4.  Non-static constructor is automatically called whenever an object of the class is created.

5.  Static constructor is implicitly called whenever a class is loaded into the memory.

6.  If no constructor is expl icitly defined then C#.Net creates a constructor which takes no

parameters – Default Constructor.

7.  A class can accommodate more than one non-static constructor – constructor overloading.

8.  Multiple static constructors can’t be created.  

Note: Once a constructor is created in the class, c# does not provide default constructor any

more. Every time you create an object of the class a constructor must be invoked otherwise

object cannot be created.

Non-Static Constructor Static Constructor

1.  Access Level private/public no keyword

2.  Name class name class name

3.  Overloading possible not possible

4.  Variables instance/non-static class/static5.  Invoking explicit implicit

Does really a constructor not return anything?

Of course, implicitly constructors return an object of the enclosing class type. Even then

a programmer should not return anything including void in i ts header, doing so is a compile-time

error.

Page 25: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 25/40

Lab Excercise C Sharp Programs : By PTSM

25 Object Oriented Programing for support: [email protected] 

class Account {

non-static variables;

public Account() {

initializing non-static

variables;

}

…. 

…. 

}

class Account {

static varia bles;

static Account() {

initializing static

variables;

}

…. 

…. 

}

Page 26: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 26/40

Lab Excercise C Sharp Programs : By PTSM

26 Object Oriented Programing for support: [email protected] 

Destructors and Garbage Collection (death of objects)

What is garbage and garbage collection?

Variables and objects whose purpose is over in the program and are not referenced

further (or no longer being used) are treated as garbage in .NET.

The implicit process performed by the CLR (Common Language Runtime) whenever

microprocessor is free is called garbage collection and the method (which is a low-

priority thread) that runs now and then is called garbage collector.

We can’t predict exactly when the garbage collection takes place.

Destructor method

Before any object is garbage collected, CLRcalls the DESTRUCTOR method implicitly.

class Account {

static variables;

non-static variables;

~Account() {

code to free the objects

before they are garbage

collected

}

…. 

…. 

}

Page 27: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 27/40

Lab Excercise C Sharp Programs : By PTSM

27 Object Oriented Programing for support: [email protected] 

1.  A class can have only one destructor – destructors cannot be overloaded.

2.  Destructor cannot be called. They are invoked automatically.

3.  It doesn’t take modifier or have parameters.

Note: Destructors are not called when object goes out of scope. They are called before object is

garbage collected.

Page 28: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 28/40

Lab Excercise C Sharp Programs : By PTSM

28 Object Oriented Programing for support: [email protected] 

// static and non-static members 

CLASS 

VARIABLES

METHODS

static/class

non-static/instance

Page 29: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 29/40

Lab Excercise C Sharp Programs : By PTSM

29 Object Oriented Programing for support: [email protected] 

class TestMembers {

private int v1; //non-static

private int v2; //non-static}

// In the main 

TestMembers tm1, tm2;

// 1st

object

tm1 = new TestMembers();

// 2st

object

tm2 = new TestMembers();

class TestMembers {

private int v1; //non-static

private static int v2; //static}

V1

V2

V1

V2

V1

V1

tm1 tm2

V2

tm1

tm2

Page 30: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 30/40

Lab Excercise C Sharp Programs : By PTSM

30 Object Oriented Programing for support: [email protected] 

class Account {

// non-static variables 

private int no;private string name;

private double balance;

// static variable 

private static double minBalance=500.00;

// non-static constructor 

public Account(int no,

String name,

double balance) {this.no=no;

this.name=name;

this.balance=balance;

}

public void GetDetails() {

System.out.println("No: "+ no);

System.out.println("Name: "+ name);

System.out.println("Balance: "+ balance);

}

// static method 

public static double MinimumBalance() {

return minBalance;

}

}

class Account {

// non-static variables 

private int no;

private string name;

private double balance;

// static variable 

private static double minBalance;

// non-static constructor 

public Account(int no,

String name,

double balance) {

this.no=no;

this.name=name;

this.balance=balance;

}

// static constructor 

static Account() {

minBalance=500.00;

}….. 

….. 

Page 31: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 31/40

Lab Excercise C Sharp Programs : By PTSM

31 Object Oriented Programing for support: [email protected] 

minBalance 500.00

minimumBalance() 

private

class TestAccount {

static void Main() {

System.out.print("Minimum Balance: ");

System.out.println(Account.MinimumBalance());

Account a;

a = new Account(100,"scott",5000);

a.GetDetails();

}

}

System.out.println(Account.MinimumBalance());

Page 32: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 32/40

Lab Excercise C Sharp Programs : By PTSM

32 Object Oriented Programing for support: [email protected] 

MIN_BALANCE 500.00

public

//constants

class Account {

// non-static variablesprivate int no;

private String name;

private double balance;

//constant 

//public static double minBalance=500.00;

public  const double MIN_BALANCE=500.00;

// non-static constructorpublic Account(int no,

String name,

double balance) {

this.no=no;

this.name=name;

this.balance=balance;

}

public void GetDetails() {

System.out.println("No: "+ no);System.out.println("Name: "+ name);

System.out.println("Balance: "+ balance);

}

}

// In the main 

System.out.print("Minimum Balance: ");

System.out.println(Account.MIN_BALANCE);

Page 33: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 33/40

Lab Excercise C Sharp Programs : By PTSM

33 Object Oriented Programing for support: [email protected] 

//readonly

class Account {

// non-static variables

private int no;

private String name;

private double balance;

//constant 

//public  readonly double MIN_BALANCE=500.00;

public readonly static double MIN_BALANCE=500.00;

// non-static constructor

public Account(int no,

String name,

double balance) {

this.no=no;

this.name=name;

this.balance=balance;

}

public void GetDetails() {System.out.println("No: "+ no);

System.out.println("Name: "+ name);

System.out.println("Balance: "+ balance);

}

}

// In the main 

System.out.print("Minimum Balance: ");

System.out.println(Account.MIN_BALANCE);

Account a = new Account(100,"scott",5000);

System.out.println(a.MIN_BALANCE);

Page 34: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 34/40

Lab Excercise C Sharp Programs : By PTSM

34 Object Oriented Programing for support: [email protected] 

//readonly

class Account {

// non-static variables

private int no;

private String name;

private double balance;

//constant public  readonly static double MIN_BALANCE;

// non-static constructor

public Account(int no,

String name,

double balance) {

this.no=no;

this.name=name;

this.balance=balance;}

// staticconstructor 

static Account() {

MIN_BALANCE=500.00;

}

}

Page 35: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 35/40

Lab Excercise C Sharp Programs : By PTSM

35 Object Oriented Programing for support: [email protected] 

Constant vs. ReadOnly 

const :

1.  Can’t be static. By default (implicitly) it is static.

2.  Value is evaluated at compile time.

3.  Initialized at declaration only.

readonly:

1.  Can be either instance or static level.

2.  Value is evaluated at run time.3.  Can be initialized in declaration or by code in the constructor.

Page 36: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 36/40

Lab Excercise C Sharp Programs : By PTSM

36 Object Oriented Programing for support: [email protected] 

// method overloading 

class Greetings {

private string name;

private string message;

public Greetings(String name,

String message) {

this.name=name;

this.message=message;

}

public String wishes() {

String str="Hello ! " + name;

str += "\n";

str += "Many More Happy Returns

Of The Day On This " +

message;

}

}

clas s Greetings {

private string name;private stri ng messa ge;

publi c Greetings(String name,

String mess age) {

this.name=name;

this.message=message;

}

public String wishes() {

String str="Hello ! " + name;

str += "\n";str += "Many More Happy Returns

Of The Day On This " +

message;

}

public String wishesMore(int times) {

String str=””; 

for(int i=1; i<=times; i++) {

str += "Hell o ! " + name;

str += "\n";

str += "Many More Happy ReturnsOf The Day On This " +

message;

str += “\n\n”; 

}

return str;

}

}

public String wishesMore(int times) {

String str=””; 

for(int i=1; i<=times; i++) {

str += wishes();

str += “\n\n”; 

}

return str;

}

Page 37: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 37/40

Lab Excercise C Sharp Programs : By PTSM

37 Object Oriented Programing for support: [email protected] 

// In the main 

Greetings g;

g = new Greetings (“scott”, 

“birthday”); 

g.sayWishes();

//or

g.sayWishes(3);

class Greetings {

private String name;

private String message;

public Greetings(String name,

String message) {

this.name=name;

this.message=message;

}

public String sayWishes() {

String str="Hello ! " + name;

str += "\n";

str += "Many More Happy Returns

Of The Day On This " +

message;

}

public String sayWishes(int times) {

String str=””; 

for(int i=1; i<=times; i++) {str += "Hello ! " + name;

str += "\n";

str += "Many More Happy Returns

Of The Day On This " +

message;

str += “\n\n”; 

}

return str;

}

}

Page 38: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 38/40

Lab Excercise C Sharp Programs : By PTSM

38 Object Oriented Programing for support: [email protected] 

Points to remember

What is method overloading?

When two or more methods in a class are created with the same name they are said to

be overloaded.

Or

Re-using the same method name with different arguments with the same or different

return type is known as method overloading.

How compiler identifies the method if different methods have same method name?

From the above definition, the different arguments means using the same method

name not only with different number of arguments but also the arguments with

different data types. Compiler identifies the method called by considering the method’s

number and type of arguments.

What is overloading for?

Overloading is a OOP technique to avoid the usage of several method names thatperform closely related operations/jobs.

What is method signature?

Method signature includes method name, number of arguments and type of arguments

and does not include return type. That is methods with a different types of return value

and having the same name will be considered as the same method and if exists in the

same class, compiler throws an error.

Note: It is one kind of polymorphism. This is also known as compile-time polymorphism.

Assignment: Product, Programmer

Page 39: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 39/40

Lab Excercise C Sharp Programs : By PTSM

39 Object Oriented Programing for support: [email protected] 

A few more topics will be included …coming soon…  

Page 40: C Sharp - OO Programming

8/7/2019 C Sharp - OO Programming

http://slidepdf.com/reader/full/c-sharp-oo-programming 40/40

Lab Excercise C Sharp Programs : By PTSM

40 Object Oriented Programing for support: [email protected]