object oriented programming

13
Some Basic Terms

Upload: independent

Post on 03-Apr-2023

0 views

Category:

Documents


0 download

TRANSCRIPT

Some Basic Terms

ClassA class is a definition of the behavior of an object.

The class is the essence of C# because it defines the nature of an object.

The class is the foundation upon which the entire C# language is built.

A class is a template that defines the form of an object.

What does a class contain? A class contains a complete description of the following:

The data elements the object containsThe methods the object can doThe way these data elements and methods can be accessed.

General Form of a ClassA class is created by using the keyword class.

For example,public class Duck{ // declare member variables

// declare member properties // declare member methods}

Access : Public or Private ?Classes and Class members can be marked as public which means that anyone can access them.

You can’t mark a Class Private unless that class is inside another class.

Class members can be marked as private which means that only other members can access them or other instances of that class can access them.

A class is a logical abstractionIt is not until an object of that class has been created that a physical representation of that class exists in memory.

You will use Duck to create objects of class Duck.

Classes are a way to create your own data types that are similar to the built in data types such as int, float, decimal, double, string and char.

Objects are Instances of ClassesTo get an object, you tell the program you want a new object of the class Duck. This new object is called an instance of the class Duck.

Creating instances of a class is called instantiation.

For example, the following code will create Duck objects called duck, mallard and decoy.

Duck duck = new Duck(); Duck mallard = new Duck(); Duck decoy = new Duck();

Variables are Data ElementsClasses can contain data in the form of variables.

A variable stores data.Data come in different types. For example,

int holds integers or whole numbersstring holds textbool holds Boolean true/false values

Declare your variables. Write the type followed by the name. Make sure the name describes the data.int age; // this variable will hold someone’s age.

Example 1 class Vehicle { public int passengers; // number of passengers

public int fuelcap; // fuel capacity in gallons

public int mpg; // fuel cosumption in mpg4

} Show how an object called minivan of class Vehicle is created.

Methods perform actionsWhen a class needs to do something, it uses a method.

A method takes an input, performs some action, and sometimes produces an output.

A method receives its inputs through parameters.

public void DoSomething(int count) { //statements go here }void is a keyword you use in front of a method that will not produce an output or that will not return a value.

Example 2public class Duck{ private int weight; // duck’s weight in pounds private string color; // duck’s color description

private void quack() { // implementation of quacking goes here. } private void swim() { // implementation of swimming goes here. } private void display() { // implementation of displaying a duck goes here. }}

Identify the

members of this class…

Example 3class SuperChef{ public string cookieRecipe; private string secretIngredient; private const int loyalCustomerOrderAmount = 60;

public int Temperature; public string GetRecipe (int OrderAmount) { //implement code here }}

Which class members cannot be accessed from

outside the class SuperChef?

Drill