OBJECT-ORIENTED
Chapter 2: Object-Oriented Programming Concepts
Concepts of OOP
Key Concepts:
Objects
Classes
Data Abstraction and Encapsulation
Inheritance
Polymorphism
Overloading
Reusability
Objects and Classes
Class:
A collection of data members and member functions.
Acts as a user-defined data type.
An object is a variable of class type, often referred to as an instance of the class.
Each object contains all members (variables and functions) declared in the class.
Helps in managing complex problems efficiently by grouping related data and methods.
Blueprint for objects; no memory is allocated until an object is created.
Defining the Class
Syntax of Class Definition in C++:
class class_name { // data members // member functions };Example:
class temp { private: int data1; float data2; public: void func1() { data1=2; } float func2(){ data2=3.5; return data2; } };
Access Specifiers:
Private:
Data and functions accessible only within the class.
Public:
Data and functions accessible from outside the class.
Prevents misuse of data through data hiding.
Objects
Relationship of an object to a class is similar to that of a variable to a data type.
Syntax to Define Object:
class_name variable_name;Example:
temp obj1, obj2;
Data Members and Member Functions
Data Members:
Variables declared inside a class.
Member Functions:
Functions declared inside a class.
Example from class temp:
Data members:
data1,data2Member functions:
func1(),func2()
Accessing Data Members and Member Functions
Access using the member operator (.) as in structures.
Example Access:
Call member function for
obj2:obj2.func1();
Note:
You cannot access private data members from outside the class.
Example Code of Objects and Class in C++
C++ Program:
#include <iostream>
using namespace std;
class temp {
private:
int data1;
float data2;
public:
void int_data(int d){ data1=d; cout<<"Number: "+to_string(data1); }
float float_data(){ cout<<"\nEnter data: "; cin>>data2; return data2; }
};
int main(){
temp obj1, obj2;
obj1.int_data(12);
cout<<"You entered "<<obj2.float_data();
return 0;
} Output:
Number: 12
Enter data: 12.43
You entered: 12.43
Explanation of the Program
The program defines two data members (
data1,data2) and two member functions (int_data(),float_data()) within the classtemp.Objects
obj1andobj2are instantiated.The function
int_data()setsdata1forobj1andfloat_data()retrieves input fordata2inobj2.
Defining Member Functions Outside the Class
To enhance code clarity, member functions can be declared in the class and defined outside using the scope resolution operator (::).
Abstraction and Encapsulation
Abstraction:
Represents complex real-world problems in a simplified manner, focusing on relevant features and behaviors.
Encapsulation:
Hides internal details of an object, allowing interaction with an interface without exposing underlying implementation details.
Inheritance and Polymorphism
Inheritance:
When an object or class is derived from another, inheriting properties and behaviors.
Polymorphism:
Ability of different classes to respond to the same function name based on their respective implementations.
Comparison Between Structured and OOP
Keywords and Identifiers:
Keywords are reserved words with fixed meaning in C++, such as
int.Identifiers are user-defined names for entities like variables and functions. Examples include
money,mango_tree.
Rules for Writing Identifiers
Comprised of letters, digits, and underscores only.
Should start with a letter or underscore (starting with underscore discouraged).
No length limit, but the first 31 characters must be unique to avoid conflicts.
Literals and Constants
Literals:
Directly represented data, e.g.,
42,3.1415,'a'.
Constants:
Identifiers representing fixed values that cannot change during execution.
Variables:
Identifiers with values that can change during execution.
Comments and Punctuators
Comments:
Portions of code ignored by compilers for notes, either single-line (
//) or multi-line (/* ... */).
Punctuators:
Symbols used to separate code components, e.g.,
{,},;.
Operators
Operations combining expressions, e.g.,
a + b. Operators vary in C++ for different operations.
Reasons for Embracing OOP
Code Reuse and Recycling:
Objects from OOP can be easily reused in different programs.
Encapsulation:
Once an object is created, its implementation details are hidden from the user.