Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes and OOP.

Similar presentations


Presentation on theme: "Classes and OOP."— Presentation transcript:

1 Classes and OOP

2 OOP The fundamental idea behind object-oriented programming is: The real world consists of objects. From the natural language definition of the word “class”: Collection of members that share certain attributes and functionality Likewise classes in object-oriented programming In object oriented programming languages (like C#, Java) classes are used to combine everything for a concept (e.g. date) Data (state / attributes) (e.g. date day, month, year) Methods (behavior / tasks) (e.g. display date, increment date)

3 Kinds of Objects Human Entities: employee, customer, worker, manager…
Graphics program: point, line, circle… Data storage: linked list, stack, matrix… Car, Robot …

4 What is a class? Essentially a struct with built-in functions
Properties and behaviours class Circle { double radius = 0.0; double Area() double pi = ; return (pi * radius * radius); } class keyword name of the class name of the method

5 Objects object: An entity that encapsulates data and behavior.
data: variables inside the object behavior: methods inside the object You interact with the methods; the data is hidden in the object. Constructing (creating) an object: Type objectName = new Type(parameters); Calling an object's method: objectName.methodName(parameters);

6 Blackbox analogy creates iPod blueprint
state: current song volume battery life behavior: power on/off change station/song change volume choose random song creates iPod #1 state: song = "1,000,000 Miles" volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song iPod #2 state: song = "Letting You" volume = 9 battery life = 3.41 hrs iPod #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs

7 Encapsulation By default the class definition encapsulates, or hides, the data inside it. Key concept of object oriented programming. Hiding data: why? you can drive cars, but you don’t need to know how the fuel injection works when the car’s fuel injection changes, you can still drive that new car The outside world can see and use the data only by calling the build-in functions. Called “methods” 8

8 Class Members Methods and variables declared inside a class are called members of that class. Member variables are called fields. Member functions are called methods. In order to be visible outside the class definition, a member must be declared public. As written in the previous example, neither the variable radius nor the method Area could be seen outside the class definition. 9

9 Making a Method Visible
To make the Area() method visible outside we would write it as: public double Area() { return pi * radius * radius; } We will keep the radius and pi field private. 10

10 Methods The best way to develop and maintain a large application is to construct it from small, simple pieces  divide and conquer Methods allow you to modularize an application by separating its tasks into reusable units. Reuse the Framework Library, do not reinvent the wheel Divide your application into meaningful methods such that it is easier to debug and maintain. Methods == Worker analogy:

11 Methods and Classes The behavior of a class is defined by its methods and properties by which objects of that class are manipulated You should know about the public methods and what they do name of the method parameters and parameter types return type functionality You don’t need to know how the method is implemented or how the property is stored analogy: you can add two int variables using +, but you don’t need to know how computer really adds

12 First example Dice class Have a number of sides
When rolled returns an int between{1- num_sides}

13 Another Class Example Lets write a class named Authenticator for password authentication as a console application It should have two member functions (i.e., methods) Check if given password is correct? Change a password

14 Access Modifiers private password;
private variables/ functions are not accessible outside the class public variables/ functions allows access outside the class

15 Instantiating and Using Objects
The class Authenticator contains everything the compiler needs to know about to process this new variable type. Constructor – create an instance Whenever a class object is created, its constructor is called A class may have multiple constructors that take different arguments There is always a default one Authenticator myAccess = new Authenticator();

16 Static variables Each instance of a class has its own set of fields.
Authenticator alice = new Authenticator(); Authenticator bob = new Authenticator(); alice.ChangePassword("OldAlicePassword", “NewAlicePassword); bob.ChangePassword("OldBobPassword", "NewBobPassword); The two objects are different and have distinct values of password Variable that applies to all can be done static variable belongs to class not its instances private static uint minPasswordLength = 6;

17 GUI design for Authenticator class
Lets design a windows form application similar to the one we did as console application

18 HW – account class Write a class for bank accounts
A double field for balance A constructor Deposit and Withdraw methods Write a test program that generates at least two different accounts Apply withdraw and deposit functions and show they work correctly by printing the result.


Download ppt "Classes and OOP."

Similar presentations


Ads by Google