Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Object Oriented Programming

CSC2071
Lecture No. 08
Muhammad Shahid
Department of Computer Science
National Textile University

shahid.abdullah@hotmail.com
Recap

 What is Object-Orientation?
 Procedural v/s OO Programming Paradigm
 Benefits of Object-Orientation
 Object-Oriented Principles
‒ Encapsulation
‒ Abstraction
‒ Inheritance
‒ Polymorphism

2 Object Oriented Programming – CSC2071


What Will You Learn Today?

 What are Classes?


 What are Objects?
 Difference between Class and Object
 How to create classes using Visual Studio?
 The new Operator

3 Object Oriented Programming – CSC2071


Announcement

 Project Titles
 3 Group Members
 No Changeable
 March 31, 2016
(Monday)

Be Ready for Quiz No. 1 in Coming Week


4 Object Oriented Programming – CSC2071
Announcement

 What are Classes?


 What are Objects?
 Difference between Class and Object
 How to create classes using Visual Studio?
 The new Operator

5 Object Oriented Programming – CSC2071


Classes

6 Object Oriented Programming – CSC2071


Class

 A class is a user-defined type that is


composed of field data and members that
operate on this data
 Collection of objects that share common
attributes and behavior
 A class is the blueprint of similar object from
which individual objects are created
 The attributes and operations defined by a
class are for its objects, not for itself
7 Object Oriented Programming – CSC2071
Class

8 Animals
Object Oriented Programming – CSC2071
Class

9 Vehicle
Object Oriented Programming – CSC2071
Class

10 Aeroplan
Object Oriented Programming – CSC2071
Class
class ClassName
{
access type variable1;
.............
access type variableN;

access return_type Method1(parameters)


{
// body of Method
}
..............
access return_type MethodN(parameters)
{
// body of Method
}
11 } Object Oriented Programming – CSC2071
Class
Access Details
Private Only other members of the same class can access
private members.
Public Public members can be accessed by an object, from
any derived class, and from other parts of the
program. The methods defined inside other classes
can access them.
Protected Members of the same class and inheriting members
of the same class can access protected members.
Internal Internal members are accessible only within a file of
the same assembly.
Protected The members defined in the containing assembly
internal and the inheriting members of the class can access
the protected internal member.
12 Object Oriented Programming – CSC2071
Object

 An object stores its state/information in fields


(attributes) and exposes its behavior through
methods (member functions)

 Other objects can change the state of an


object by using only those methods that are
exposed to the outer world

 Object is an encapsulation of data (attributes)


as well as methods (functions)
13 Object Oriented Programming – CSC2071
Object

 Real-world objects share two characteristics


 They all have state and behavior

Stat Current Gear


Current Speed
Current Pedal
Behavior Changing gear
Changing pedal
Applying brakes

14 Object Oriented Programming – CSC2071


Object

 Real-world objects share two characteristics


 They all have state and behavior

Stat Speed
Bombs
Fuel
Behavior Fire Bomb
Changing direction
Change speed

15 Object Oriented Programming – CSC2071


Object

 Real-world objects share two characteristics


 They all have state and behavior

Stat ?
Behavior ?
Stat ?
Behavior ?

16 Object Oriented Programming – CSC2071


Object

 Real-world objects share two characteristics


 They all have state and behavior

Stat ON/OFF
Volume
Read
Stat ON/OFF
Behavior Turn ON
Behavior Turning ON Turn OFF
Turning OFF Change volume
Seek

17 Object Oriented Programming – CSC2071


Object

 Real-world objects share two characteristics


 They all have state and behavior

Stat ?
Behavior ?

Stat ?
Behavior ?

18 Object Oriented Programming – CSC2071


Difference b/w Classes & Objects

19 Objects HaveProgramming
Object Oriented Unique–Identity!!!
CSC2071
Difference b/w Classes & Objects

20 Objects HaveProgramming
Object Oriented Unique–Identity!!!
CSC2071
Task

 Identify objects, their states and behaviors in


your classroom.

 Identify objects, their states and behaviors in


the examination of National Textile University.

 Identify objects, their states and behaviors in


the Bus Ticket Reservation System.

21 Object Oriented Programming – CSC2071


Class

22 Object Oriented Programming – CSC2071


Class

23 Object Oriented Programming – CSC2071


Class

class Student
{
public string regNo;
public string name;
public double cgpa;

public void Display()


{
Console.WriteLine("Reg No.{0}",regNo);
Console.WriteLine("Name{0}", name);
Console.WriteLine("CGPA{0}", cgpa);
}
}

24 Object Oriented Programming – CSC2071


The new Operator

 The object created using new operator for


allocating memory
 The allocated memory is then filled with the
data members of the class
used while defining the parameterized constructor

ClassName objName = new ClassName(ArgsList)

Circle c = new Circle(20);

Student std = new Student("14-NTU-12", "Ali");


25 Object Oriented Programming – CSC2071
The new Operator

 Attempt to make use of class variable without


using the new keyword will receive a compiler
error compiler error
static void Main(string[] args)
{
Student std;
std.name = "Haris";
std.regNo = "14-NTU-1001";
} static void Main(string[] args)
{
Student std = new Student();
std.name = "Haris";
std.regNo = "14-NTU-1001";
26 }
Object Oriented Programming – CSC2071
Class

static void Main(string[] args)


{
Student account = new Student();
account.regNo = "14-NTU-1001";
account.name = "Haris";
account.cgpa = 3.0;
account.Display();
}

Reg No. 14-NTU-1001


Name Haris
CGPA 3.0

27 Object Oriented Programming – CSC2071


Recap

 What are Classes?


 What are Objects?
 Difference between Class and Object
 How to create classes using Visual Studio?
 The new Operator

28 Object Oriented Programming – CSC2071


Questions

Object Oriented Programming – CSC2071

You might also like