Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Your OwnClasses

Similar presentations


Presentation on theme: "Creating Your OwnClasses"— Presentation transcript:

1 Creating Your OwnClasses
AKEEL AHMED

2 Overview The Object Concept Private Member Data Constructor
Instance Methods, Accessor, Mutators, Property Named and Optional Parameters

3 The Object Concept To use an object-oriented approach, the solution is defined in terms of a collection of cooperating objects. Each of the objects is capable of sending messages to other objects and receiving messages from objects. Each object is also capable of processing data.. An object is one instance or example of a class. When you define the class, you describe its attributes, or characteristics or fields, in terms of data and its behaviors, or methods, in terms of what kinds of things it can do.

4 Private Member Data When you define a class and determine what data members it should have, you declare instance variables or fields that represent the state of an object.. These fields are declared inside the class, not inside any specific method They become visible to all members of the class, including all of the method members. Create Class with member data in visual Studio. class Student{ private int id; private String name; }

5 Constructor Constructors are special types of methods used to create objects . Constructors differ from other methods. Constructors do not return a value, but the keyword void is not included.You receive an error message if you type void as part of the constructor method heading. Constructors use the same identifier (name) as the class name. Constructors are methods. Like other methods, they can be overloaded //Default constructor public Student ( ) { } //Constructor with one parameter public Student (string sID ){ studentNumber = sID; }

6 Writing Your Own Instance Methods
When you define the methods of the class, you are writing instance methods. Instance methods do not use the static keyword in their heading To call an instance method, however, an object must be instantiated and associated with the method. class Motorcycle { public void StartEngine() { Console.WriteLine(”Instance method”); } } Motorcycle motor=new Motorcycle(); motor.StartEngine();

7 Accessor To read the current state or value of an object member’s data, accessors can be used. Accessors are also referred to as getters.

8 Mutators To change the current state or value of an object member’s data, special methods called mutators can be used. Mutators are sometimes called setters.

9 Property One of the underlying themes of object-oriented programming is encapsulation, which states that the internal representation of an object is generally hidden, thus member data are defined with private access. A property looks like a data field, but it does not directly represent a storage location. Properties are more closely aligned to methods. They provide a way to change or retrieve private member data.

10 Property

11 Conditional Expressions
A conditional expression is also referred to as a test condition. Shorter and more concise when dealing with direct value comparisons and assignments. int x = 10; int y = 20; int maxValue = (x > y) ? x : y; int minValue = (x < y) ? x : y;

12 Decision Statements(If-else)
A one-way selection statement is used when an expression needs to be tested. When the result of the expression is true, additional processing is performed.

13 Switch Selection Statement
The switch statement is considered a multiple selection structure. It also goes by the name case statement The switch or case statement allows you to perform a large number of alternatives based on the value of a single variable


Download ppt "Creating Your OwnClasses"

Similar presentations


Ads by Google