Classes Objects | PDF | Object Oriented Programming | Inheritance (Object Oriented Programming)
0% found this document useful (0 votes)
94 views

Classes Objects

This document provides an overview of object-oriented programming concepts. It discusses the limitations of procedural programming and how object-oriented programming addresses these limitations through concepts like encapsulation, inheritance and polymorphism. Key characteristics of OOP include classes, objects, data abstraction, inheritance and polymorphism. Classes define the data and behavior of objects, while objects are instances of classes. The document provides examples to illustrate classes and objects.

Uploaded by

ayyazm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Classes Objects

This document provides an overview of object-oriented programming concepts. It discusses the limitations of procedural programming and how object-oriented programming addresses these limitations through concepts like encapsulation, inheritance and polymorphism. Key characteristics of OOP include classes, objects, data abstraction, inheritance and polymorphism. Classes define the data and behavior of objects, while objects are instances of classes. The document provides examples to illustrate classes and objects.

Uploaded by

ayyazm
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Object Oriented Programming

Lecture Outline

Procedural Language The Object-Oriented Approach Characteristics of OOP

Procedural Languages
e.g. C, Pascal, Fortran Structured programming is a programming paradigm that to a large extent relies on the idea of dividing a program into functions and modules. A program is basically a list of instructions As programs become larger -> broken down into smaller units, such as functions, procedures, subroutines Functions can be grouped together into modules according to their functionality, objectives and tasks.

1. Problems with Structured Programming


Functions have unrestricted access to global data Function A: local data Function B: local data Function C: local data

global data X

global data Y

global data Z

program structure difficult to conceptualize difficult to modify and maintain the program e.g. : it is difficult to tell which functions access the data

2. Problems with Structured Programming


data and function are considered as two separate entities difficult to model things in the real world

Because complex real world objects have both; attributes people: name, date of birth, eye color, job title cars:number of seats, number of doors, color behaviours people: ask a person to drive the car cars: apply the brakes, turn on the engine sufficient to realistically model real world objects but a unified view is needed

Object Oriented Approach


Object functions

data
Encapsulation: integrate data and functions into one object Data hiding : data is hidden to the outside world and can only be accessed via the functions

Object Oriented Approach


separation: objects interact with each other only via the their member functions separation helps to maintain the integrity of the entire program Object A functions Object C functions data Object B functions

data

data

Characteristics of OOP

Objects Classes Inheritance Reusability Creating new data types Polymorphism & Overloading

Classes versus Objects


A class is a prototype specification from which one can generate a number of similar objects An object is said to be a member or instance of a

class

A class can be considered as a more complex data structure than an ordinary built-in data type already know the struct command for user defined data types: struct complex { double re; double im; }; complex x;

Examples of Objects
physcial objects vehicles in a traffic-flow simulation electrical components in a circuit-design program data-storage constructs arrays stacks human entities employees students collections of data an inventory an address book user defined data types time complex numbers

Inheritance
In our daily lives we use the concept of classes divided into subclasses, for example vehicles are divided into cars, trucks, buses and motor cycles. The principle in this sort of division is that each sub-class shares some common features with the base class from which it is derived, but also has its own particular features. base class Vehicle wheels engine Car Truck wheels wheels engine sub-classes or engine trunk derived classes trailer

Inheritance
A sub-class also shares common methods with its super-class but can add its own methods or overwrite the methods of its super-class.

base class

Car brake() start_engine() open_door()

Vehicle brake() start_engine() sub-classes or derived classes

Truck brake() start_engine() open_door() pull_trailer()

Inheritance
Terminology: Car is a sub-class (or derived class) of Vehicle

Car inherits from Vehicle


Car is a specialization of Vehicle Vehicle is a super-class (or base class) of Car Vehicle is a generalization of Car

Reusability
The concept of inheritance provides an important extension to the idea of reusability, as it allows a

programmer

to

take

an

existing

class

without

modifying it and adding additional features and functionality. This is done by inheriting a new sub-class from the exisiting base class.

Polymorphism & Overloading


polymorphism means having many shapes Polymorphism : using functions and operators in different ways, depending on what they are operating on. Overloading is a kind of polymorphism. Overloading: an existing operator, such as + or = is given the capability to operate on a new data type, for example define the operator + for the class Complex, Time, Fractional

Objects and Classes

Objects and Classes

An object has the same relationship to a class that a variable has to a data type. An object is said to be an instance of a class. Think of a class as a blueprint, and the object is the actual, tangible thing, or object.

Defining the Class

The definition of a class starts with the keyword class, followed by the class name. Just like a structure, the body of a class is delimited by braces and terminated by a semicolon.

class time //define a class { private: int hr, min, sec; //class data public: void setdata(int h, int m, int s) { hr = h; min=m; sec=s; } void showdata() { cout << "Hour: " << hr << endl; cout << "Min: " << min << endl; cout << "Sec: " << sec<< endl; }
};

void main() { time t1, t2; t1.setdata(6, 12, 45); t2.setdata(2,9,30);

t1.showdata(); t2.showdata(); }

Private and Public Keywords

A key feature in object-oriented languages is data hiding. This term data hiding, means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside of the class. To hide data, you put it in a class and make it private.

Private and Public Keywords

Private data or functions can only be accessed from within a class. Public data or functions, on the other hand, are accessible from outside the class.

All class members are private by default

Class Data

The time class in our example contains 3 data items: hr, min, sec, which are of type int. The data items within a class are called data members. There can be any number of data members in a class, just as there can be any number of data items in a structure.

Member Functions

Member Functions are functions that are included within classes. In our last example we had 2 member functions, setdata() and showdata(). You can define the member functions in the class or more common is outside of your class which we will see later on.

Member Functions

In our last example the member functions follow the keyword public. Since they are declared as public, they can be accessed from outside of the class.

Functions are Public, Data is Private

Usually the data within a class is private and the functions are public. This is a result of the way classes are used.

The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed outside of the class.
However there is no rule that says data must be private and functions public.

Using the Class

Now that we have the class defined, lets see how main() will take use of it. In our last example, the first statement in main() was time t1, t2; This statement defines two objects of class time.

Using the Class

Remember that the definition of the class does not create any objects. The definition only describes how they will look when they are created. Objects are what participate in program operations. Defining an object is similar to defining a variable of any data type, space is set aside for it in memory.

Calling Member Functions

In our example, the next 2 statements in main() call the member function setdata(): t1.setdata(6, 12, 45); t2.setdata(2,9,30);

This strange syntax is used to call a member function that is associated with a specific object.
Because setdata() is a member function of the time class, it must always be called in connection with an object of this class.

Calling Member Functions

To use a member function, the dot operator connects the object name and the member function. The syntax is similar to the way we refer to structure members.

Another Class Example


class part { private: int modelnumber; int partnumber; float cost; public: void setpart(int mn, int pn, float c) { modelnumber = mn; partnumber = pn; cost = c; } void showpart() { cout << "Model " << modelnumber; cout << ", part " << partnumber; cout << ", costs $" << cost << endl; } }; //declare a class

Another Class Example


void main() { part part1; part1.setpart(6244, 373, 217.55); part1.showpart(); }

Recommended Reading

Robert Lafore, Chapter 1: The Big Picture Dietel & Dietel, Chapter 3: Introduction to Classes and Objects

You might also like