Professional Documents
Culture Documents
p (x, y)
15
10
X
(0,0)
Y
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 {
Fields are used to store
//data - variables data.
Attributes
private int x;
private int y; Methods are used to
perform operations on
//code – methods
Setters data.
public void SetX (int xx) {
x = xx;
} Class defines fields and ,
public void SetY (int yy){ methods.
y = yy;
} Encapsulation: putting
data and code together-
Getters binding of behavior and
public int GetX (){
return x; state to a particular
} object.
public int GetY (){
return y; Abstraction: providing
} essential and relevant
features and ignoring the
}
rest.
NULL
class TestPoint { OBJECT
p
x
static void Main () { 150 y
p
Point p; //object reference variable 150
p = new Point (); //creating an object and assigning an address to a variable
DATA (variables)
behavior
CODE (methods)
From outside
DATA hidden and all operations to data are performed though METHODS of the object.
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.
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 drops
its 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.
C2
BUS
PERSON
C1
E F
D
G H I
C3
CAR
Objects: A, B, C, D, E, F, G, H and I
class TestPoint 2 {
Input
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); OR
Assigning object reference variable : Do not creates a copy of the object, you are only making a
copy of reference.
p1 150 x
Point p1,p2;
p1 = new Point(); y
p2 150
p2 = p1; 150
Class Diagram:
Classes:
1. Class name
2. Class attributes(fields)
3. Class operations(methods)
Class Name
Class Attributes
Class Operations
Note: Name should be center aligned; Attributes and Operations should be left aligned.
Class Name:
Class Attributes:
Class Operations:
The +, -, # and ~ symbols before an attribute and operation name in a class denote the
visibility of the attributes and operations.
The UML specifies two types of scope for attributes and operations
1. Instance
2. Classifier
Instance:
1. In case of instance members, the scope is specific to instance.
Classifier:
1. The scope is class.
2. Classifier members are commonly recognized as “static” in
many programming languages.
3. To indicate that a member has the classifier scope, its name
must be underlined. Otherwise as default, the instance scope
is considered.
Each parameter in an operation may be denoted as in, out or inout which specifies its
direction with respect to caller.
In: states that parameter “p” passed to operation “op” by the caller. Here “p”
is the “in” parameter.
Out: states that parameter “p” is not set by the caller but is modified by
operation "op” and is passed back out. Here “p” called as “out”
parameter.
InOut: states that parameter “p” is passed to operation “op” by the caller and is
then possibly modified by “op” and is passed back out. “p” is the “inout”
parameter.
Default Values
The UML specification allows for the identification of default values in the attribute list
section by using the following notation.
Example:
ConsoleApp
Point TestPoint
- x : int
- y : int
Variables
Instance variables
Class variables
Local variables
Block Level variables and Formal Arguments
class Variables {
private int v1; //instance
private static int v2; //class
Default Values
The constructors provide a default value for each member variable.
For value types: integer-0, floating point-0.0, Boolean-false, character-‘\u0000’
For reference types: null (neither zero nor blank)
Note: for local and block level variables; an explicit initialization is required
Assignment:
1. Course : description, fee and duration
2. Time: hours, minutes and seconds
} }
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.
class Point {
private int x; Point p1, p2;
private int y;
//1st object
public void SetX(int x) {
p1 = new Point();
this.x = x;
//x = x;
} //2nd object
p2 = new Point();
public int GetX() {
return x;
} p1.SetX(10); //1st
…
… p2.SetX(20); //2nd
}
X X
Y Y
STACK
p1-150 p2-250
this = 250
2nd call
x = 20 P2.SetX(20); //2nd
t
this = 150
1st call
p1.SetX(10); //1st
x = 10
Points to remember
class TestBook {
static void Main() {
Book b1,b2;
// 1st book
b1 = new Book();
b2.SetTitle(“ASP.NET”); b2.SetDetails(“ASP.NET”,
b2.SetAuthor(“Stephen Walther”); ”Stephen Walther”,
b2.SetCost(650.00);
650.00);
Console.WriteLine(“Book #1”);
Console.WriteLine (“----------------------------“);
Console.WriteLine (“\tTitle: “ + b1.GetTitle());
Console.WriteLine (“\tAuthor: “ + b1.GetAuthor()); b1.GetDetails();
Console.WriteLine (“\tCost: “ + b1.GetCost());
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);
// 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());
}
// Array of objects
class TestBook2 {
static void Main() {
Book[] b;
b = new Book[3]; // array of 3 books
title tttt
// creating an object 1
author
aa
b[i] = new Book(); cccccost
// placing values
b[i].SetTitle(title); b[2]
b[i].SetAuthor(author); title tttt
b[i].SetCost(cost); 2
author
aa
} cccccost
for(int I = 0; i<3; i++){
Console.WriteLine (“Book # “ + (i+1) ); b
Console.WriteLine (“---------------------------“);
Console.WriteLine (“\tTitle: “ + b[i].GetTitle());
Console.WriteLine (“\tAuthor: “ + b[i].GetAuthor());
Console.WriteLine (“\tCost: “ + b[i].GetCost());
Console.WriteLine (); // empty line
}
}
validation
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;
}
additional methods (optional)
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();
}
}
class TestNumber {
static void Main() {
int n;
bool status;
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
// Constructors
output
V1 0 V1 = 0
class TestDefaultConstructor {
static void Main() { V2 0.0 V2 = 0.0
V3 null V3 = null
DefaultConstructor d;
d = new DefaultConstructor();
d.GetValues();
output
Console.WriteLine(); //empty line V1 10 V1 = 10
V2 1.25
d.SetValues(10,1.25,"scott"); V2 = 1.25
d.GetValues(); V3 scott V3 = scott
}
}
// Explicit Constructor
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; class InternetURL {
private string url;
public InternetURL() {
url = null; public InternetURL() {
//or this("http://www.google.co.in/
url = “http://www.google.co.in/ index.html");
index.html"; }
}
public InternetURL(string url) {
public InternetURL(string url) {
this.url = url;
this.url = url;
}
}
public void SetUrl(string url) {
this.url = url; public void SetUrl(string url) {
} this.url = url;
public string GetUrl() { }
return 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");
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 explicitly 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.
Of course, implicitly constructors return an object of the enclosing class type. Even then
a programmer should not return anything including void in its header, doing so is a compile-time
error.
} }
…. ….
…. ….
} }
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.
Destructor method
Before any object is garbage collected, CLR calls the DESTRUCTOR method implicitly.
class Account {
static variables;
non-static variables;
~Account() {
}
….
….
}
Note: Destructors are not called when object goes out of scope. They are called before object is
garbage collected.
VARIABLES
static/class
METHODS non-static/instance
CLASS
// In the main
// 1st object
tm1 = new TestMembers();
V1
// 2st object
tm2 = new TestMembers(); tm1
V2
V2
V1
V1 V1
V2 V2 tm2
V2
tm1 tm2
V2 V2
class TestAccount {
static void Main() {
Account a;
a = new Account(100,"scott",5000);
a.GetDetails();
}
}
private
minBalance 500.00
minimumBalance()
System.out.println(Account.MinimumBalance());
//constants
class Account {
// non-static variables
private int no;
private String name;
private double balance;
//constant
//public static double minBalance=500.00;
public const double MIN_BALANCE=500.00;
// non-static constructor
public Account(int no,
String name,
double balance) { public
this.no=no;
MIN_BALANCE 500.00
this.name=name;
this.balance=balance;
}
// In the main
//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;
}
// In the main
System.out.print("Minimum Balance: ");
System.out.println(Account.MIN_BALANCE);
//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;
}
}
const :
readonly:
class Greetings {
private String name;
private String message;
Points to remember
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.
Overloading is a OOP technique to avoid the usage of several method names that
perform closely related operations/jobs.
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.