Sie sind auf Seite 1von 28

1.

1 Basics of Object oriented


programming
and
1.3 Overview of OOP Language

Dr.Narayana Swamy Ramaiah


Assoc.Prof, Dept of Electrical and
Computer Engineering,
Arba Minch University
Basic of Object Oriented Programming
• Program
– In computing, program is a specific set of ordered operations for a computer to perform.
– Is like a recipe, it contains a list of ingredients (called variables) and a list of directions (called
statements) that tell the computer what to do with the variables.
• The variables can represent numeric data, text or graphical images.

• Computer software
– Is the product that software engineers design and build.
– It encompasses
• Programs that execute with in a computer of any size and architecture.
• Documents that encompass hard copy and virtual forms.
• Data that combine numbers and text but also includes representations of pictorial, video and
audio information.

• Programming Language
– Like any human language, a programming language provides a way to express concepts.
– Program development involves creating models of real world situations and building computer
programs based on these models.
– Computer programs describe the method of implementing the model.
– Computer programs may contain computer world representations of the things that constitute the
solutions of real world problems
• Procedural programming (structural programming)
– Pascal, C, BASIC, Fortran, and similar traditional programming languages are
procedural languages. That is, each statement in the language tells the
computer to do something.
– Uses structural constructs like decision making (if else, while), looping,
functions.
– In a procedural language, the emphasis is on doing things (functions).
– A program is divided into functions and—ideally, at least— each function has
a clearly defined purpose and a clearly defined interface to the other
functions in the program.
#include<stdio.h>

int main() {

int a, b, c;

printf("Enter two numbers to add\n");

scanf("%d%d",&a,&b);

c = a + b;

printf("Sum of entered numbers = %d\n",c);

return 0;

}
#include<stdio.h>
1
2
int main() {
3
4
int num1, num2, res;
5
6
printf("\nEnter the two numbers : ");
7
scanf("%d %d", &num1, &num2);
8
9
//Call Function Sum With Two Parameters
10
res = sum(num1, num2);
11
12
printf(“\nAddition of two number is : ");
13
return (0);
14
}
15
16
int sum(int num1, int num2) {
17
int num3;
18
num3 = num1 + num2;
19
return (num3);
20
}

Enter the two numbers : 12 15


Addition of two number is : 27
• Problems With Procedural Programming
– Data Is Undervalued
– Emphasis was on functions to introduce modularity.
– Procedural programs (functions and data structures) don’t model the real
world very well. The real world does not consist of functions.
– Structure variables are created and data stored in member variables which are
processed by independently written functions.
– Global data can be corrupted by functions that have no business changing it.
– To add new data items, all the functions that access the data must be modified
so that they can also access these new items.
– Creating new data types is difficult.
– It is also possible to write good programs by using procedural programming
(C programs).
– Not suitable for large programs and simulation programs were difficult to
develop. The programmers were highly paid. There was need to make use of
their efforts in a cost effective manner. It led to the new paradigm of object
orientation.
– But object-oriented programming offers programmers many advantages, to
enable them to write high-quality programs.
• Example of date
– A date forms part and parcel of many programs. It can be
your birthday, maturity date of your fixed deposit, date
this file was created.

– Programmers used to define a structure like

• Struct DATE {
int date;
int month;
int year;
};

and later on declare variables of type DATE.


The function written to manipulate date were scattered
and not bound to date.
• Example of date using typical class declaration in an object oriented like c++.

Class DATE {
Private:
int date, month, year;

Public:
void set_date(int d, int m, int y);
void show_date(int d, int m, int y);
};

DATE d1;
• Here we are defining a variable date d1. here d1 is an object. The object
and functions set_date() and show_date() are tightly coupled. Once this
class is fully tested, it can be easily reused in any new application.

• You can modify date using only set_date() function and see or print date
only with help of show_date().
• OVERVIEW of Object Oriented Programming Language
– The fundamental idea behind object-oriented programming is:
– The real world consists of objects. Computer programs may contain
computer world representations of the things (objects) that constitute
the solutions of real world problems.
– Real world objects have two parts:
• Attributes (or state: characteristics that can change),
• functionality (or abilities: things they can do).
– To solve a programming problem in an object-oriented language, the
programmer no longer asks how the problem will be divided into
functions, but how it will be divided into objects.
– The emphasis is on data
– What kinds of things become objects in object-oriented programs?
• Human entities: Employees, customers, salespeople, worker, manager
• Graphics program: Point, line, square, circle, ...
• Mathematics: Complex numbers, matrix
• Computer user environment: Windows, menus, buttons
• Data-storage constructs: Customized arrays, stacks,
• Features of object oriented language
– Encapsulation: in above example we are putting three variables and two functions into a
bundle. there is an invisible shell around this data. In other word we are encapsulating the
data. It is more secure. You cannot use a function without its corresponding object. This adds
much safety to the code.

– Data hiding: you cannot tamper date, month or year. They are actually hidden from your
program (main and other functions). This is the principle of data hiding. Data is available only
through a predefined interface ( function set and show). Otherwise it is inaccessible; in other
words, invisible.

– Inheritance: if a particular application needs more functions to operate on date, we can


inherit the properties of this class in a new class. Here we define a new class, say, DATE1 based
on class DATE. All the definitions of the class DATE are available to class DATE1. In other words,
the work done earlier is intact . We have to add few new functions as required; a great saving
in development time.

– Polymorphism: we can design new classes based on a single class. Imagine we have defined a
general class animal. Now we may develop new classes like tiger, wolf, hare and so on. These
classes have many things common with animal class. They have few properties which are
different. They will have many functions with same name like RUN. Though speed of these
animals is quite different, they will share a similar interface. This is the principle of
polymorphism. When same function applied on different object will give you different
results.

– Message passing: when objects wants to communicate with each other, they do so in terms
of messages. Message is information sent by one object to other.
inheritance
class person /*Parent class*/ {
private:
char fname[100],lname[100],gender[10];
protected: int age;
public: void input_person();
void display_person();
};

class student: public person /*Child class*/ {


private: char college_name[100]; char level[20];
public: void input_student(); void display_student();
};
• Benefits of object oriented programming (OOP)

– Software reusability: when we develop software we develop it in terms of


objects. Objects are data abstraction. Hence they have some existence outside
the program. When we write another program of similar nature, all these
objects can be directly used . It means that half the job is done. Our efficiency
is thus doubled.

– Code sharing: reusability refers to using our own code, while code sharing
refers to code written by our friends

– Rapid prototyping: object orientation helps in rapid prototyping. When basic


design is done, elementary objects can be designed with minimum of
methods. Only at a later stage can all the methods be developed.

– Information hiding: it adds lots of safety to software , thus a considerable


amount of time is saved because compiler does not allow us to make many
such errors.
• Classes and objects
– Class represents a general template that shows how a data is
stored and processed

– OOP involves modelling abstract object as well as real life


objects like, say, a student

– Let us understand the attributes of a student relevant to a


programmer.
• A student has attributes like roll number, name and marks. These
attributes become data members of the class
• The functions for processing these data members (like get_data and
show_data) become member functions of the class.
• This way system analyst designs the required class named say, STUDENT
• Class: a class is a software representation of an object (real life or abstract). In
programmers words it is a user defined data type.

• Consider the declaration


– Class STUDENT
{ private :
int roll_number;
char name[15];
float average_marks;
public :
void get_data ();
void show_data();
}
• In class example above, the analyst studies attributes of a student and
decides his roll number, name and average marks. These have been made
data members.
• In this simple example, students data has to be gathered and his result
has to be shown. Two functions are required. One to gather data of each
student, get_data(), and the other to process data, generate result and
show it, show_data(). These two functions are member function of this
class. Member functions are also called methods.
• Let us observe few details about class
declaration
– It starts with the keyword class
– Next comes class name. It is STUDENT in this
example
– Next comes the keyword private with colon
– Then follows private data member declaration
– There after private member functions, if any
– Now comes the keyword public with colon
– Then public data members, if any
– Lastly public methods
– Declaration are enclosed with in braces
• Let us take another example of class, DATE

– Class DATE
{ private:
int date, month, year;
public:
set_date();
show_date();
}
• In this the class name is DATA.
• It has three data members vz date, month and
year which are private.
• It also has two public methods viz set_date()
and show_date()
• If you notice one of the three data members
of class DATE has name date. In C++ DATE and
date are distinct identifiers.
• Class name uses all capital letters
• Encapsulation
– The process of holding together data members
and method members in a single variable type
called class is called encapsulation.
– Encapsulation not only makes a class almost a self
contained entity, but also protects the data from
inadvertent processing by other functions,
independently written or methods of other
classes.
– Look at private qualifier of the data members,
making them private ensures their safety. The
private data can be processed by member
methods only.
• Declaration of objects
– Class represents a general template that shows how a
students data is stored and processed.
– How do we process each students data? Then why
objects are declared?
• If there are three students , three objects say S1, S2,S3 are
declared.
– For example,
STUDENT S1,S2,S3;
– S1,S2,S3 are objects of the class STUDENT.
– There fore an object is an instance of a class.
– In a programmer’s words an object is a variable of user defined
data type
– It means S1,S2,S3 are variables of class STUDENT.
– The word variable implies that contents of these objects can vary
(just like inbuilt type int)
• Qualifying data members :
– Class is an aggregate type like struct.
– A member of the aggregate has to be qualified to avoid ambiguity.
• If there are two cricket teams, say india and newzealand, and we havt to
talk about the captains, we say indian captain or newzealand captain.
Saying only captain creates ambiguity-which captain?
• Similarly, if we simply refer to roll no., there is ambugity, whose roll no?
We have to qulify thus
– S1.roll_number, it refers to S1s roll no
– S2.roll_number, it refers to S2s roll no and so on.
• Let us take another example of object declaration
– DATE d1,d2,d3;
– d1, d2, d3 are objects which hold dates like 19/12/2003, 15/08/1947 and so
on
– It is possible to use pointers for specifying objects. We have to
declare as show below;
• STUDENT * ptr1;
– Here we declare a pointer to a class STUDENT. We can initialize this
pointer with address of s1, that is
– Ptr1 = &s1;
– Then qualify members using pointer arrow mechanism , for example
– Ptr1 ->roll_number instead of s1. roll_number
• Data hiding and encapsulation
– Principle of Data hiding
• Private and public members
– The trouble with the last example was that the data members were
private. Let us declare them as public and see the result
• The only change we made is to add the keyword public. It allowed
us to modify and see/print the variable. ‘private’ and ‘public’ are
access specifiers for the members of a class.
• They specify whether the members can be accessed freely or not.
• A class with only public data members will work similar to structure.
• What is advantage of class? In this example class was limited only to
public and private data members.
• Unlike structures in c, a class also contains methods that operate on
data members and process them.

• Now the scope of the data members and the functions can be so
defined such that data members of a class can normally be
processed only by these function members and not by methods of
other classes or independently written function.

• The philosophy of OOP is that in an object, data members and


methods are coupled together. We are expected to process the
objects data only through the member functions.
• MEMBER FUNCTIONS
– Let us add two member functions, (methods) to the class DATE which was declared
earlier. These methods are used to read and display the data members of the class.
• Now you are able to access the data members of the class DATE by the member functions (methods) of
the class DATE.
• If you run the program you will see the following output.

• Scope resolution operator


– To resolve to which class given method belongs, C++ has defined new operator called scope
resolution operator (::) for this purpose
– This is a binary operator on the left hand side is the class name. On right hand side is the method.
This helps us to specify a method unambiguously.
– In above program we have seen the following lines using the scope resolution operator.
– Void DATE::set_date(int d1, int m1,int y1)
– The operator is mainly needed for method definition. For invoking the method a dot
operator is used with the object and the method. For example d1.set_date(),
here scope resolution operator not required
• Accessing class members
– One of salient features of OOP is to qualify data members
of the class as private and public.
– Private members can be referenced only within the class
and not outside, that is, in the function main or by other
methods of other class or independently written functions.
– Public data members can be referenced any where by the
dot operator mechanism. Similarly public method can be
called from any where.
– A private data member of a class can be referenced by the
private as well as public method of that class.
– When the method is public it can be accessed outside the
class in main function or any other function, thus gaining
access to the private data member of a class.
– Hence programmer has to be careful while designing a
public method.
Short questions and answers

Das könnte Ihnen auch gefallen