Question 2a: Explain further on the three main principles of Object-Oriented Programming
2b: More Details on each one How it works on C++ with 2 examples each
2a
Encapsulation - The process of combining data members and functions in a single unit called class.
Polymorphism - The same entity (function or object) behaves differently in different situations.
Inheritance- The process of extending and modifying a class without modifying it.
Examples:
2b
Examples of Polymorphism:
Operator + can perform the following operations:
- Adding numbers
int x= 6;
int y=1;
int sum = x + y;
- Concaneting strings
string first = "John";
string second = "Paul";
string full_name = first +" "+second;
Examples of Inheritance:
class Shape{
};
class Square: Shape{
};
The class Square will inherit the members of class Shape;
Examples of Encapsulation:
class Rectangle {
public:
int l;
int b;
int getArea() {
return l* b;
}
};
Both l and b are being used inside the function getArea()
Therefore, l and b are being encapsulated inside the function getArea()
Comments
Leave a comment