Facebook : Manoj Acharya
Object oriented Programming introduction
Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects, which can contain data and code to manipulate that data. Objects are instances of classes, which are templates or blueprints for creating objects.
OOP emphasizes the use of encapsulation, inheritance, and polymorphism. Encapsulation refers to the practice of hiding the internal workings of an object from the outside world, so that changes to the object's implementation don't affect its external behavior. Inheritance allows new classes to be based on existing classes, inheriting their properties and methods while also adding new ones. Polymorphism allows objects of different classes to be used interchangeably, as long as they implement the same interface.
One of the main advantages of OOP is its ability to create reusable code. By encapsulating data and behavior in objects, the code can be used again and again without having to rewrite it. OOP also allows for easier collaboration among programmers, since they can work on different parts of a project independently and then integrate their code together.
Some popular programming languages that support OOP include Java, Python, Ruby, and C++.
Functions:
In Object-Oriented Programming (OOP), a function is called a method and it is defined within a class. A method is a function that is associated with a specific class and can access and manipulate the data members of that class. There are several types of methods in OOP:
1. Instance methods: These are the most common type of methods in OOP. They are associated with an instance of a class and can access and manipulate the instance variables of that class.
2. Class methods: These are methods that are associated with a class rather than an instance of that class. They can access and manipulate the class variables of that class.
3. Static methods: These methods are not associated with either an instance or a class. They are used to perform some utility function that does not depend on the state of an object or a class.
4. Getter and setter methods: These are special methods used to get and set the values of private data members of a class.
5. Constructor methods: These methods are used to initialize an object when it is created. They have the same name as the class and do not have a return type.
6. Destructor methods: These methods are used to perform some cleanup operations when an object is destroyed. They have the same name as the class with a tilde (~) prefix.
Inline function
In object-oriented programming (OOP), an inline function is a function that is defined and declared inline within a class definition. An inline function is a type of function that is expanded by the compiler at the point where it is called, rather than being executed as a separate function call.
When a function is declared inline, the compiler generates code for the function at the point where it is called, rather than creating a separate function call. This can result in faster execution times and reduced memory usage, as the overhead of a function call is eliminated.
In OOP, inline functions can be useful for small, frequently-used functions, such as getters and setters, that are called frequently within a class. By declaring these functions as inline, the compiler can optimize their execution and reduce the overall overhead of the program.
Here is an example of an inline function in C++:
```
class MyClass {
private:
int x;
public:
inline void setX(int value) {
x = value;
}
inline int getX() const {
return x;
}
};
```
In this example, the `setX` and `getX` functions are declared inline within the `MyClass` definition. These functions are small and frequently-used, so declaring them as inline can help to optimize the execution of the program.
Friend Function
In object-oriented programming (OOP), a friend function is a non-member function that is granted access to the private and protected members of a class. Friend functions can be useful for providing functions that are closely associated with a class, but are not part of the class interface.
A friend function is declared inside a class using the `friend` keyword, but is not a member of the class. Instead, it is a standalone function that can access private and protected members of the class. Friend functions can be defined either inside or outside the class definition, but they are not inherited by any derived classes.
Here's an example of a friend function in C++:
```
class MyClass {
private:
int x;
public:
MyClass(int value) : x(value) {}
friend void printX(MyClass obj);
};
void printX(MyClass obj) {
std::cout << "The value of x is: " << obj.x << std::endl;
}
int main() {
MyClass myObj(5);
printX(myObj); // Output: The value of x is: 5
return 0;
}
```
In this example, the `MyClass` class has a private member `x` and a friend function `printX`. The `printX` function is able to access the private member `x` of the `MyClass` object that is passed as an argument.
Friend functions can be useful for providing functions that are closely associated with a class, but are not part of the class interface. However, overuse of friend functions can lead to poor encapsulation and a violation of the principle of information hiding, so they should be used judiciously.
Function Overloadding
In object-oriented programming (OOP), function overloading is a feature that allows multiple functions to have the same name, but with different parameters or argument lists. The function that is called is determined by the number and types of arguments that are passed to it.
Function overloading is useful for creating functions that perform similar tasks but with different data types or parameter lists. This can help to simplify code and make it more readable.
Here is an example of function overloading in C++:
```
#include <iostream>
class MyClass {
public:
void print(int x) {
std::cout << "Printing integer: " << x << std::endl;
}
void print(double x) {
std::cout << "Printing double: " << x << std::endl;
}
void print(char x) {
std::cout << "Printing character: " << x << std::endl;
}
};
int main() {
MyClass myObj;
myObj.print(5); // Output: Printing integer: 5
myObj.print(3.14); // Output: Printing double: 3.14
myObj.print('a'); // Output: Printing character: a
return 0;
}
```
In this example, the `MyClass` class has three overloaded `print` functions, each with a different parameter type. When the `print` function is called with a specific parameter type, the compiler selects the appropriate function to execute based on the parameter list.
Function overloading can be useful for creating functions that perform similar tasks but with different data types or parameter lists. However, it is important to ensure that the functions are distinct and have unique parameter lists to avoid ambiguity and confusion.
Function overriding
In object-oriented programming (OOP), function overriding is a feature that allows a subclass or derived class to provide a different implementation of a method that is already defined in its parent class or base class. When a subclass overrides a method, it provides its own implementation of the method that is used instead of the implementation provided by the parent class.
Function overriding is useful for creating specialized versions of methods that are already defined in a parent class. This allows subclasses to customize the behavior of inherited methods to better suit their own specific needs.
Here is an example of function overriding in Java:
```
class Animal {
public void speak() {
System.out.println("Animal is speaking");
}
}
class Dog extends Animal {
@Override
public void speak() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Animal();
animal.speak(); // Output: Animal is speaking
Dog dog = new Dog();
dog.speak(); // Output: Dog is barking
Animal animal2 = new Dog();
animal2.speak(); // Output: Dog is barking
}
}
```
In this example, the `Animal` class has a `speak` method that is overridden in the `Dog` class. When the `speak` method is called on an instance of the `Dog` class, the overridden method in the `Dog` class is executed instead of the method in the `Animal` class. Similarly, when the `speak` method is called on an instance of the `Animal` class that has been assigned to a `Dog` variable, the overridden method in the `Dog` class is executed.
Function overriding is a powerful feature of OOP that allows for greater flexibility and customization in class hierarchies. However, it is important to ensure that the method signature (name and parameters) and return type of the overridden method matches that of the parent class, and that the overridden method is marked with the `@Override` annotation (in Java) to ensure that it is properly overridden.