Object Oriented Programming Lecture 1 PDF

Document Details

MarvelousSerpentine6623

Uploaded by MarvelousSerpentine6623

Suez Canal University

2025

Dr. Amira Sayed - Dr. Amira Eleff

Tags

object-oriented programming programming computer science software development

Summary

This document is a lecture on object-oriented programming (OOP). It covers topics such as structured programming, its disadvantages, object attributes, behavior, and object-oriented languages. It also touches on inheritance, polymorphism, and the importance of data encapsulation and data hiding in OOP.

Full Transcript

Object Oriented Programming Dr. Amira Sayed - Dr. Amira Eleff Faculty of Computers & Information Science Suez Canal University General Program Second Year -First Term 2025 2 Structured programming A program in a pr...

Object Oriented Programming Dr. Amira Sayed - Dr. Amira Eleff Faculty of Computers & Information Science Suez Canal University General Program Second Year -First Term 2025 2 Structured programming A program in a procedural language is a list of instructions. A procedural program is divided into a number of functions. Each function has a defined purpose and a defined interface to the other functions in the program. Dividing a program into functions is called structured programming. 3 Disadvantage of Structured programming Large programs become excessively complex. There is no data protection where functions have unrestricted access to global data. The restricted access is only provided to local variables. A change in a single global data item may necessitate modifying all the functions that access that global data item. That makes large programs very difficult to modify. 4 Disadvantage of Structured programming A poor model of the real world where functions and data are not related. In the physical world we deal with objects such as people, university, and cars. Complex real-world objects have both attributes and behavior. 5 Object Attributes and Behavior Attributes (sometimes called characteristics) in the real world are equivalent to data in a program have certain specific values. For a person as an object has certain attributes: name, eye color, length, weight, and job title; For a car, the attributes: car color, horsepower and number of doors. Behavior is something a real-world object does in response to some stimulus. For example, if you apply the brakes in a car, it will generally stop. Behavior is simply like a function to do something. 6 Object-oriented languages The fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions that operate on that data. Such a unit is called an object. 7 Object An object consists of member data and a group of member function. The object’s member functions provide the only way to access its member data. To read a data item in an object, you call a member function in the object. It will access the data and return the value to you. You can’t access the data of a certain object directly. The data are hidden inside an object. 8 OOP characteristics Data and its functions are encapsulated into a single entity. Data encapsulation and data hiding are key terms in the description of object- oriented languages (OOP). If you want to modify a certain data in an object, you simply call the object member function which interacts with it, and NO other functions can access the data. This simplifies writing and modifying large complex programs. 9 Object communication A program typically consists of a number of objects. Objects communicate with each other by calling one another’s member functions. Such communication is called sending messages. Figure 2. Interaction between objects by sending messages. 10 Inheritance The vehicle class is divided into cars, trucks, buses, motorcycles, and so on. The principle in this sort of division is that each subclass shares common characteristics with the class from which it’s derived. Cars, trucks, buses, and motorcycles all have wheels and a motor; these are the defining characteristics of vehicles. In addition to the characteristics shared with other members of the class, each subclass also has its own particular characteristics: Buses, for instance, have seats for many people, while trucks have space for hauling heavy loads 11 Inheritance Member data A and B, which are part of the base class, are common to all the derived classes, but that each derived class also has its own data. The original class is called the base class. Other classes can be defined that share its characteristics, but add their own as well. These are called derived classes. Figure 3. Inheritance. 12 Polymorphism OOP can define new behaviors for the known operators, such as +, -, *. This is called operator overloading. These operations will be member functions of a certain class. By calling that operator, we call the defined member function of the class. Overloading is a kind of polymorphism. 13 OBJECT-ORIENTED LANGUAGES Some of the most popular Object-oriented Programming languages are : C++ Java. smalltalk Eiffle. Ruby Delphi 14 Characteristics of OOPS Programs are divided into classes and functions. Data is hidden and cannot be accessed by external functions. Use of inheritance provides reusability of code. New functions and data items can be added easily. Data is given more important than functions. Data and function are tied together in a single unit known as class. Objects communicate each other by sending messages in the form of function. 15 Advantages of OOP Code reusability in terms of inheritance. Complex projects can be easily divided into small code functions. The principle of abstraction and encapsulation enables a programmer to build secure programs. Software complexity decreases. Principle of data hiding helps programmer to design and develop safe programs. Rapid development of software can be done is short span of time. More than one instance of same class can exist together without any interference. 16 17 18 Class Vs Object A class is a data type that allows programmers to create objects. A class provides a definition for an object, describing an object’s attributes (data) and methods (operations). A class serves as a plan, or blueprint. Defining the class doesn’t create any objects. Acts as the existence of data types int, long, float, and double doesn’t create any variables. An object is often called an instance of a class. With one class, you can have as many objects as required. 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 Assignment 1 1. Write a program that prints Hello world. Use comments and proper layout. 2. Write a program that prints three lines, name, address, tel. no., Use comments and proper layout. 35 Assignment 1 Q3 36

Use Quizgecko on...
Browser
Browser