Anda di halaman 1dari 15

Spring 2016

W03-Classes and Objects


Abstraction and Encapsulation

Class Declaration
Access Control Specifiers
Instantiation of Objects
Constructor and Destructor
Examples

Structured Programming

MAIN PROGRAM DATA

FUNCTION FUNCTION FUNCTION


1 2 3

FUNCTION FUNCTION
4 5

W03- Classes and Objects


3

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 1
Object Oriented Programming
Object 1 Object 2

Data Data

Function Function

Object 3

Data

Function

W03- Classes and Objects


4

Object

Object in programming is an abstraction for a


real-world object.

 Object has State (properties or attributes)


 Has values at a particular time

 Object has Behavior


 A set of operations (functions)

W03- Classes and Objects


5

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 2
Class

A class specifies the common properties and


behavior of objects

W03- Classes and Objects


6

Class
 A class is a user-defined data type from
which we can instantiate objects.

Class
 A class encapsulates
data1
data and functions data2
data3

functiona()
functionb()
functionc()

W03- Classes and Objects


7

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 3
Class

 A class is used to define abstractions that do


not map naturally to the built-in data types e.g.,
date, time, complex numbers, vector, circle.

 A well-designed class separates the


implementation from the interface.

W03- Classes and Objects


8

Class and Objects

W03- Classes and Objects


9

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 4
UML Class Diagram

 Unified Modeling Language (UML) is used for graphical


representation of object-oriented systems.

Class name goes here

Data is listed here

Functions are listed here

W03- Classes and Objects


10

UML Class Diagram Example

W03- Classes and Objects


11

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 5
Access Control Specifiers
Class private members of a class
private: can be accessed within the
same class only
data1
data2 public members of class can be
functiond() accessed from outside the class
public:
Typically, data is kept private
functiona() whereas member functions are
functionb() mostly public.
functionc()
The default access is private

W03- Classes and Objects


12

Public and Private Access

W03- Classes and Objects


13

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 6
Class Definition

class Cat
int main ()
{
{
public: Cat Kitty;
int Age; Kitty.Age = 5;
void Meow() Kitty.Meow();
{ cout << “meow…”; } return 0;
}; }

W03- Classes and Objects


14

Structure vs. Class

 By definition a struct is a class in which members are by


default public, whereas in a class data and function
members are by default private.

W03- Classes and Objects


15

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 7
Class Data Members

• Data members of a class cannot be


initialized when they are declared inside the
class.

• Data members should be initialized using


specific functions (known as “get” and “set”
functions)

W03- Classes and Objects


16

Example: The Counter Class


class Counter
{
private:
int x;
public:
void increment() { x++; }
void decrement() { x--; }
void set (int c) { x = c;}
int get() { return x;}
void reset () { x = 0; }
void show() { cout << “Count = “<< x << endl; }
};
W03- Classes and Objects
17

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 8
Using the Counter Class

int main()
{
Counter c; //object instantiation
c.set(100);
while (c.get() != 0)
{ c.decrement();
c.show();
}
cout <<“Time out……!”<<endl;
return 0;
}

W03- Classes and Objects


18

Example: The Point Class


class Point
{
private: // private data of the class
int x,y;

public: // visible public interface of the class


void set (int u, int v)
{ x = u; y = v;}

void print()
{ cout << “(” << x << “,” << y << “)”; }
};

W03- Classes and Objects


19

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 9
Using a Class: Instantiating Objects

int main()
{
Point p; //object instantiation
p.set(4,7); // member function call
p.print(); // member function call
}

W03- Classes and Objects


20

Inline Functions

inline is used to suggest to the compiler that


the function body is inserted at each point the
function is called.

W03- Classes and Objects


21

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 10
Class Definition

• Functions defined within a class are inline by


default.

• Class member functions can be defined outside the


class definition using scope resolution operator ::

• In this case, only the function prototype needs to be


included within the class.

W03- Classes and Objects


22

Class Definition
class Point
{
private:
int x, y;
public:
void set (int u, int v) // inline
{ x = u; y = v; }
void print(); // not inline, prototype
};

void Point::print() {
cout << “(“ << x << “,” << y << “)” << endl;
}

W03- Classes and Objects


23

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 11
Using a Class: Creating Objects

int main()
{
Point p; //object instantiation
p.set(4,7); // member function call
p.print(); // member function call
}

W03- Classes and Objects


24

Example: The Rect Class


class Rect
{
private:
int w, h;
public:
void setValues (int a, int b);
int area() { return (w*h);}
};

void Rect::setValues(int a, int b)


{ w = a; h = b;}

W03- Classes and Objects


25

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 12
Constructor

 A constructor is a special function that is


automatically invoked when a new object of a
class is instantiated.

 The constructor must always have the same


name as the class and has no return type.

 Constructors can be overloaded.

W03- Classes and Objects


26

Example: The Date Class


class Date
{
private:
int day, month, year; // data members

public:
Date() // default constructor
{ day = 1; month = 1; year = 2000;}
Date (int d, int m, int y); // parameterized constructor
void setYear(int y);
void setMonth(int m);
void setDay(int d);
void show(); // displays date
};
W03- Classes and Objects
27

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 13
Destructor
 Just like a Constructor, Destructor is also a special function.
 A Destructor is automatically invoked when an object is
destroyed.
 The most common use for destructor is to deallocate
dynamically allocated memory.

 Example:
class Date
{
public:
Date(); // constructor
~Date(); // destructor

};

W03- Classes and Objects


28

Class Implementation
Date::Date (int d, int m, int y)
{
day=d;
month=m;
year = y;
}

void Date::setMonth (int m)


{
month=m;
}

W03- Classes and Objects


29

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 14
Class Implementation

void Date::setDay (int d)


{
day = d; }

void Date::setYear (int y)


{
year=y; }

void Date::show()
{
cout << day << ”-” << month << ”-” << year << endl;
}
W03- Classes and Objects
30

Instantiation of Objects
void main ()
{
Date d1 (14,8,1947);
Date d2 (17,2,2016);
Date d3;
Date d[10];
d1.show(); d2.show(); d3.show();
d[0] = d1; d[0].show();
}
W03- Classes and Objects
31

Object Oriented Programming - Spring 2016


Instructor: Saima Jawad 15

Anda mungkin juga menyukai