🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Object Oriented Programming, Lecture 06.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

ImprovedEnglishHorn

Uploaded by ImprovedEnglishHorn

Zagazig University

Tags

object-oriented programming C++ computer science

Full Transcript

Object Oriented Programming Ch.8: Introduction to Classes Ⅰ Prepared By Dr. Ibrahim Attiya © 2023 Zagazig University Ch.8: Outline ❑ Procedural vs. Object-Oriented Programming ❑ Introduction to Classes ❑ Defining an Instance of a Class ❑ Inline Member Functi...

Object Oriented Programming Ch.8: Introduction to Classes Ⅰ Prepared By Dr. Ibrahim Attiya © 2023 Zagazig University Ch.8: Outline ❑ Procedural vs. Object-Oriented Programming ❑ Introduction to Classes ❑ Defining an Instance of a Class ❑ Inline Member Functions ❑ Constructors ❑ Destructors ❑ Overloading Constructors ❑ Arrays of Objects ❑ The Unified Modeling Language Procedural vs. Object- Oriented Programming Procedural vs. Object-Oriented Programming There are two common programming methods in practice today: procedural programming and object-oriented programming (OOP). Procedural programming is a programming practice centered on the procedures or actions that take place in a program. Object-Oriented programming is centered around the object. Objects are created from abstract data types that encapsulate data and functions together. Limitations of Procedural Programming When the structure of the data (data structures) changes, many functions must also be changed to accept the new format. This results in additional work for programmers and a greater opportunity for bugs to appear in the code. Programs that are based on complex function hierarchies are: – difficult to understand and maintain. – difficult to modify and extend. Object-Oriented Programming Terminology Whereas procedural programming is centered on creating procedures or functions, object-oriented programming is centered on creating objects. An object is an instance of a class. Specifically, it is a software entity that contains both data and procedures. The data that are contained in an object are known as the object’s attributes. The procedures that an object performs are called member functions. More on Objects OOP addresses the problems that can result from the separation of code and data through encapsulation and data hiding. Encapsulation refers to the combining of data and code into a single object. Data hiding refers to an object’s ability to hide its data from code that is outside the object. Only the object’s member functions may directly access and make changes to the object’s data. Classes and Objects A Class is like a blueprint and objects are like houses built from the blueprint Classes and Objects Before an object can be created, it must be designed by a programmer. The programmer determines the attributes and functions that are necessary and then creates a class. A class is code that specifies the attributes and member functions that a particular type of object may have. So, a class is not an object, but it is a description of an object. Introduction to Classes Introduction to Classes In C++, the class is the construct primarily used to create objects. In other words, a class is a data type defined by the programmer, consisting of variables and functions. General format: class ClassName { declaration; declaration; }; Access Specifiers C++ provides the keywords private and public, which you may use in class declarations. These keywords are known as access specifiers because they specify how class members may be accessed. public: can be accessed by functions outside of the class. private: can only be called by or accessed by functions that are members of the class. Introduction to Classes The general format of a class declaration that uses the private and public access specifiers: class ClassName { private: //Declaration of private //members appear here; public: //Declaration of public //members appear here; }; More on Access Specifiers Can be listed in any order in a class. Can appear multiple times in a class. If not specified, the default is private. Notice that the access specifiers are followed by a colon (:) and then followed by one or more member declarations. All of the declarations that follow an access specifier, up to the other access specifier, are for this specific access specifier members. Class Example Private Members Public Members Using const With Member Functions Notice that the keyword const appears in the declarations of the getWidth, getLength, and getArea member functions, as follows: When the keyword const appears after the parentheses in a member function declaration, it specifies that the function will not change any data stored in the calling object. Defining a Member Function When defining a member function: – Put prototype in class declaration – Define function using class name and scope resolution operator (::) – General format: ReturnType ClassName::functionName(ParameterList) – Ex: double Rectangle::getArea() const { return width * length; } Accessors and Mutators A member function that stores a value in a private member variable, or changes its value in some way is known as a mutator. A member function that retrieves a value from a private member variable but does not change it is known as an accessor. In the Rectangle class, getLength and getWidth functions are accessors, and setLength and setWidth functions are mutators. Defining an Instance of a Class Defining an Instance of a Class An object is an instance of a class. Class objects must be defined after the class is declared. Defined as follows: ClassName objectName; Rectangle rect; Access members using dot operator: rect.setWidth(5.3); cout

Use Quizgecko on...
Browser
Browser