Академический Документы
Профессиональный Документы
Культура Документы
What is an object?
Object
Objects are the basic run-time entities of an object oriented system. They may
represent a person, a place or any item that the program must handle.
It is an instance of a class.
Here are some examples of a class and an object:
Class Objects
Fruits orange
apple
banana
Class Objects
Car volvo
audi
suzuki
Class Objects
Person student
manager
teacher
How to create class?
First Step:
Since class is a template to create an object, it is important to define the object’s attributes and
behavior before declaring a class.
Mypoint’s attributes – x , y
Second Step:
Now, to declare a class, the keyword “class” is use followed by the class name.
Syntax:
class className{
}
class Person{
}
class Car{
}
class Fruits{
}
Third Step:
1. Fields - member-variables from a certain type (usually variables that represent object’s
attributes).
2. Constructor – it is used for creating new objects.
3. Properties – this is the way to describe the characteristics of a given class. Usually, the value of
the characteristics is kept in the fields of the object. Similar to the fields, the properties may be
held by certain object or to be shared among the rest of the objects.
4. Methods - it implement the manipulation of the data (usually to implement object’s behavior).
Encapsulation is defined as the process of enclosing one or more items within a physical or logical
package. Encapsulation is implemented by using access modifier.
Access modifier is used to set the access level/visibility for classes, fields, methods and properties.
Field
Syntax:
public className( ){
}
Parameterized constructors – have one or more parameters. Also, this technique helps you to assign
initial value to an object at the time of its creation.
public className(parameter/s) {
}
//constructor where x and y can have any value
public Mypoint(int x, int y) {
this.x = x;
this.y = y;
this.name = name;
this.age = age;
this.color = color;
this.breed = breed;
}
“this” keyword
“this” keyword in C# is used to refer to the current instance of the class.
It is also used to differentiate between the method parameters and class fields if they
both have the same name.
Third component: Properties
Properties
Property encapsulates a private field. It provides getters (get{}) to retrieve the value of the underlying
field and setters (set{}) to set the value of the underlying field.
get{return x;}
set{ x = value;}
get { return y; }
set { y = value; }
}
The X property is associated with the x field. It is a good practice to use the same name for both the
property and the private field, but with an uppercase first letter.
The set method assigns a value to the x variable. The value keyword represents the value we assign to
the property.
Invoking or calling a property is actually the process of getting or setting the value.
Syntax:
<property name>();
or
getX();
setY();
X;
Y;
Methods
To reuse code
define the code once
can be use multiple times
Syntax :
Method Body
}
Elements of a Method
Access Modifier − it determines the visibility of a variable or a method from another class.
Return type − A method may return a value. The return type is the data type of the value the method
returns. If the method is not returning any values, then the return type is void.
Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any
other identifier declared in the class.
Parameter list − Enclosed between parentheses, the parameters are used to pass and receive data from
a method. The parameter list refers to the type, order, and number of the parameters of a method.
Parameters are optional; that is, a method may contain no parameters.
Method body − this contains the set of instructions needed to complete the required activity.
a. method with two parameters (num1 & num2), and int as the return type.
int sum;
sum = num1 + num2;
return sum;
}
b. method with no parameter and void as the return type.
c. method with two parameters (array name arr and the size) and double as the return type.
//method that will compute the average of all odd numbers in an array
private double computeAverageOdd(int[] arr, int size) {
double ave=0;
int c=0,sum=0;
for (int i = 0; i < size;i++ )
{
if (arr[i] % 2 == 1) {
sum += arr[i];
c++;
}
}
if (c != 0)
{
ave = (double)(sum / c);
}
return ave;
Invoking or calling a method is actually the process of execution of the method’s code,
placed into its body.
Syntax:
// for void return datatype
<method_name>();
//for other return type (variable data type must match the return data type)
variable = methodname();
//for objects
objectname.<method_name>();
variable = objectname.methodname();
// average hold the return value from all the odd numbers of myarray
int [] myarray = new int[10]
double average = computeAverageOdd(myarray,10);
Note: When the return type of the method are data types that hold values except void, method must
have a return statement in the method’s body.
Now, let’s create a class:
In you project, right click your project name in the Solution Explorer.
After right click, select add then class or (shift + alt + c), as shown below:
After add, a window will pop up same as below. Class C# is selected and the name of the class must be
change (your class name) as highlighted in yellow color.
Mypoint’s attributes – x , y
Mypoint’s behavior – display value of x and y (you can add more behavior like at what quadrant)
class Mypoint
{
private int x;
private int y;
// default constructor
public Mypoint() {
}
//constructor where x and y can have any value
public Mypoint(int x, int y) {
this.x = x;
this.y = y;
}
//properties (setter and getters)
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
//methods
//method that will display the value of x and y by calling the properties
public void displayXY() {
class Dog
{
private string name;
private int age;
private string color;
private string breed;
//default constructor
public Dog(){
}
//constructors with parameters
public Dog(string name, int age, string color, string breed)
{
this.name = name;
this.age = age;
this.color = color;
this.breed = breed;
}
//properties
public void setName(string name)
{
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
//method
public void displayDog()
{
}
Last Step:
After defining a class, let’s learn how to create an instance of a class (objects).
Syntax:
p2.displayXY();
d2.displayDog();
Creating instances of a class (objects) and calling methods in the main program
class Program
{
static void Main(string[] args)
{
//declaring variable for objects
Mypoint p1, p2;
Dog d1, d2;
Console.WriteLine("\nObject P2");
//creating the object p2 using constructor with parameters
p2 = new Mypoint(7,20);
//getting the value of x and y of object p2
Console.WriteLine("The value of x:" + p2.getX());
Console.WriteLine("The value of y:" + p2.getY());
Console.WriteLine("\nObject d1");
//creating the object d1 using default constructor
d1 = new Dog();
// setting the values for object d1
d1.setName("Coby");
d1.setAge(2);
d1.setColor("white");
d1.setBreed("Aspin");
// display the attributes of object d1
Console.WriteLine("\nObject d1");
d1.displayDog();
Console.ReadKey();
}
}
Example:
A straight line is an object connecting two points. Therefore, a line can be represented by two instances
of class Mypoint.
//default constructor
public MyLine()
{
}
//construtor
public MyLine(Mypoint beg, Mypoint end)
{
this.beg = beg;
this.end = end;
}
// no need to have properties for p1 and p2 since it is an instance of a class
//we can access the properties of Mypoint class for p1 and p2
//add property for new fields in the class
//methods
class Program
{
static void Main(string[] args)
{
//declaring variable for objects
Mypoint p1, p2;
MyLine l1, l2;
Console.WriteLine("\nObject P2");
//creating the object p2 using constructor with parameters
p2 = new Mypoint(7,20);
//getting the value of x and y of object p2
Console.WriteLine("The value of x:" + p2.getX());
Console.WriteLine("The value of y:" + p2.getY());
Console.ReadKey();
}
}
Output:
1. The circle has two data members, a Point representing the center of the circle and a float value
representing the radius.
Sample Output:
Your choice: 1
Your choice: 2
Your choice: 2
INSUFFICIENT FUNDS
DEBIT CARD TRANSACTION
[1] Deposit Cash
[2] Withdraw Cash
[3] Inquire Balance
[4] Calculate Interest Rate
[5] Exit
Your choice: 3
Your choice: 4
Your choice: 5