y
nl
O
se
U
tre
en
Session: 3 C
h
ec
Classes and Methods
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited.
y
nl
Explain Class
O
Describe Visibility Modifiers
se
Explain Methods
U
tre
Explain Static data fields and Constant data fields
en
Describe Accessor, Mutator, and Forward declaration
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 2
y
nl
O
Every object can be categorized into some class
se
U
tre
Characteristics of object are enclosed within the
en
class as data members
C
h
ec
To access and manipulate data members, the
pt
object has to make use of methods
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 3
y
nl
It is a type or a software construct that encloses:
Syntax
O
Code
dataSnippet
members
<access-modifier> class <class-name>
se
{ functions or behavior of an entity
U
public class Horse
<data-members>
{
tre
<methods>
string color; // data member
en
}The figure shows the structure of a class diagram.
public void display() // method
{
C
h
Console.WriteLine(color);
ec
}
pt
}
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 4
y
nl
The figure shows an example of entity with its
O
characteristics.
se
U
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 5
y
nl
The figure shows how class members can be secured
O
using visibility modifiers.
se
U
tre
en
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 6
y
nl
Defines the behavior of a class
O
se
Consists of the actions that an object or instance of a
U
class can perform on the data members
tre
Syntax
en
C
<access-modifier> <return-type> <method-
name> (<data-type> <parameter-name>,…)
h
ec
{
// statement1
pt
// statement2
rA
<return-value (or expression)>
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 7
y
Code Snippet
nl
To access any member of a class, a copy of that class
O
public class
called the Horse
object of the class, has to be created
se
{
private string color; // attribute
U
public
The void setColor(string
figure shows col) // method
examples of two objects
tre
{
en
color = col;
}
static void Main() C
h
{
ec
Horse h1 = new Horse(); // creating
pt
object of class Horse
rA
h1.setColor(“Black”);
}
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 8
y
nl
Syntax
Used to construct an object of a class and initialize its
O
data members
se
Default or no-argument constructor
U
Specialized method that()has the same name as the
<constructor-name>
tre
{class name
// initialization statements
en
}Constructor arguments are used to initialize the fields
of an object at runtime C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 9
y
nl
For a static or class variable, only a single copy is shared
O
by all objects of the class
se
U
tre
Class that is declared as static, cannot be instantiated and
en
its members can be accessed directly using the class name
C
h
ec
Method that is declared static, can be accessed directly
pt
using the class name without creating object of the class
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 10
y
nl
It is a variable whose value is fixed during compilation
O
and does not change later
se
Values of such fields can be fixed before the object of
U
the class is created
tre
en
The following C# code demonstrates the creation of a
constant. C
h
public class Horse
ec
{
pt
public const int feet = 4;
rA
…..
Fo
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 11
y
nl
These are special methods in OOP that provide an easy
O
yet secure way of accessing a data field of a class
se
U
Accessor is the method through which the object can
tre
retrieve the value of a data field
en
C
Mutator is a method through which the object can
h
assign the value to a data field
ec
pt
The figure shows accessor and mutator methods used
rA
to access a data field named hid of class Horse.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 12
y
nl
The followingHelps classesan
code shows that are referenced
example later in a
of function
O
file to be used at an earlier stage in the
prototype in C++.
se
#include <iostream.h> code without any conflict
// function prototype
U
void display(string col); // line1
tre
static void Main ()
{ It is a prior hint to the compiler that the
en
string color;
function definition or implementation has
cout << “Enterbeen
cin >>color ;
color
done C
“; later in the code
h
ec
display(color); // line 2
} // end of main()
pt
void display (string col) // line 3
rA
{ It is also called a function prototype
Fo
cout<<”Color is ”+ col;
}
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 13
y
nl
A class is a type or a software construct that encloses the data
O
members and of an entity into an enclosed structure.
se
A visibility modifier is used with a class, data field as well as a
U
method to restrict access to them from outside the class.
tre
en
A method defines the behavior of a class and consists of the
actions that an object of a class can perform on the data
members. C
h
ec
A constructor is a specialized method that has the same name as
pt
the class name and is used to initialize its data members.
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 14
y
nl
A static or class variable is one for which there is only a single
O
copy and it is shared by all objects of a class.
se
A constant is a variable whose value is fixed during compilation
U
and does not change later.
tre
en
Forward declaration of a function is a statement that informs the
compiler about the signature of the function without its
implementation. C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 3 15