Answer to Question #264557 in C++ for Sami

Question #264557

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


1
Expert's answer
2021-11-11T17:34:23-0500

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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS