lecture02
TRANSCRIPT
![Page 1: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/1.jpg)
Lecture 02
Fundamentals
of Object-Oriented
Programming
![Page 2: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/2.jpg)
What Are Objects?
Software objects model real-world objects or abstract concepts. dog, bicycle, queue
Real-world objects have states and behaviors. Dogs' states: name, color, breed, hungry Dogs' behaviors: barking, fetching
How do software objects implement real-world objects? Use variables/data to implement states. Use methods/functions to implement behaviors.
An object is a software bundle of variables and related methods.
![Page 3: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/3.jpg)
Software ObjectVisual Representation
Instance Variables Vs Instance Methods
![Page 4: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/4.jpg)
Software BicycleVisual Representation
![Page 5: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/5.jpg)
What Are Classes?
• A class is a blueprint or prototype defining the variables and methods common to all objects of a certain kind.
• An object is an instance of a certain class.
• After you have created a class, you must create an instance of it before you can use.
• Class variables and class methods.
![Page 6: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/6.jpg)
What Are Messages?
Software objects interact and communicate with each other by sending messages to each other.
![Page 7: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/7.jpg)
Components Of A Message
The object to whom the message is addressed (YourBicycle)
The name of the method to perform (changeGears) Any parameters needed by the method (lowerGear)
![Page 8: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/8.jpg)
O-O Principles
• Abstraction - take only important information
• Encapsulation - hiding or combine data and operations on data in a single unit.
• Inheritance - create new objects from existing objects.
• Polymorphism-the ability to use the same expression to denote different operations.
![Page 9: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/9.jpg)
Encapsulation
• The objects' variables make up the center of the object.
• Methods surround and hide the object's center from other objects.
• Benefit of encapsulation: Modularity & Information Hiding.
• For implementation or efficiency reasons, an object may wish to expose some of its variables or hide some of its methods.
![Page 10: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/10.jpg)
Inheritance
• Inheritance allows classes to be defined in terms of other classes.
• Each subclass inherits variables and methods from its superclass.
• Subclasses can add variables and methods to the ones they inherit from the superclass.
• Subclasses can also override inherited methods and provide specialized implementations for those methods.
![Page 11: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/11.jpg)
Class Hierarchy(Inheritance Hierarchy)
Superclass vs Subclass. Base Class vs Derived Class.
![Page 12: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/12.jpg)
Polymorphism and Overloading
• Polymorphism allows the use of operators or functions in different ways, depending on the data they are operating on.
• When an existing operator (eg. + or =) is given the capability to operate on a new data type, it is said to be overloaded.
• Overloading is a kind of polymorphism; it is also an important feature of OOP.
Example of operator overloading and polymorphism)• the + operator know how to treat an integer and a float
differently.
![Page 13: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/13.jpg)
Benefits of OOPReusability
• Once written, created, and debugged, a class can be distributed to other programmers for use in their own programs.
• Reusability facilitates component-based software design and implementation.
• A programmer can also take an existing class and, without modifying it, add additional features and capabilities to it.
![Page 14: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/14.jpg)
Benefits of OOPCreating New Data Types
• Objects and classes give the programmer a convenient way to construct new data types.
• Many features of C++ are intended to facilitate the creation of new data types.
• The ability to create new data types leads to extensible languages and programs.
• In C++, new data types can be built on top of existing (system or user-defined) data types.
![Page 15: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/15.jpg)
C++ Program Structure
Header files
preprocessor statements
class
{ …..
}
main( )
{ ……
}
#include <iostream.h>
int main( ) // VOID
{
cout << “Welcome to C++”;
return 0;
}
Variable declarations & valid C++ statements
Data Members and Member Functions
![Page 16: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/16.jpg)
Example 2
#include <iostream.h>
int main ( )
{ int a , b, c, sum =0;
cout << “ enter a value “<<endl;
cin >> a;
cout << “ enter b value “<<endl;
cin >> b;
cout << “ enter c value “<<endl;
cin >> c;
Sum = a + b + c;
cout << “ the sum is “<< sum;
OTHER DATA TYPESChar
int long int
floatdouble
![Page 17: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/17.jpg)
Input / Output
Cout - used for printing out the output
Syntax :
Cout << “Message if needed”<< variablename;
<< is called the insertion operator.
Cin - used for getting a input from the keyboard
Syntax:
Cin >> variable name;
>> is called the extraction operator
![Page 18: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/18.jpg)
Other Points
Remark statement - it is a non executable statement
used for documentation
// or /* statements */
escape sequences:
\n - new line
\t - horizontal tab
\r - carriage return
\a - bell
\\ - back slash \” - double quotations
![Page 19: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/19.jpg)
![Page 20: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/20.jpg)
![Page 21: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/21.jpg)
![Page 22: Lecture02](https://reader035.vdocuments.net/reader035/viewer/2022070317/5562f92dd8b42a275f8b48ad/html5/thumbnails/22.jpg)