0% found this document useful (0 votes)
2 views10 pages

Lecture 9

The document discusses type equivalence in programming languages, explaining name and structural equivalence. It also covers key object-oriented programming concepts such as polymorphism, overloading, and inheritance, along with their advantages and disadvantages. Additionally, it introduces type parameterization, highlighting its role in creating generic types and enhancing code reusability.

Uploaded by

ttanish984
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Lecture 9

The document discusses type equivalence in programming languages, explaining name and structural equivalence. It also covers key object-oriented programming concepts such as polymorphism, overloading, and inheritance, along with their advantages and disadvantages. Additionally, it introduces type parameterization, highlighting its role in creating generic types and enhancing code reusability.

Uploaded by

ttanish984
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lecture 9,10

Q.Explain notation of type equivalance in principles of programming


In programming languages, type equivalence determines whether two types are considered the same.
This is crucial for type checking, function calls, assignments, and other operations.

There are two main type equivalence approaches:

1. Name Equivalence

Two types are considered equivalent if they have the same name (i.e., explicitly declared as the
same type).

Example in Pascal:

pascal
CopyEdit
type
A = Integer;
B = Integer;
var
x: A;
y: B;

 Here, A and B are both defined as Integer, but under strict name equivalence, x and y
are not considered the same type.
 Under loose name equivalence, they might be treated as equivalent.

2. Structural Equivalence

Two types are considered equivalent if they have the same structure (i.e., identical components
and arrangement), regardless of their names.

Example in C:

c
CopyEdit
struct Point1 { int x; int y; };
struct Point2 { int x; int y; };

 Here, Point1 and Point2 have the same structure, so under structural equivalence,
they are considered the same type.
 In C, however, struct types are typically treated with name equivalence, meaning
Point1 and Point2 are not the same unless explicitly aliased.
Comparison Table:

Type Equivalence Definition Example

Types are the same only if they have the type A = Integer; type B =
Name Equivalence
same name Integer; (A ≠ B)

Structural Types are the same if they have the


struct Point { int x; int y; };
Equivalence same structure

Q Explain about polymorphism ,overloading ,inheritance concept with program code.

Key Object-Oriented Programming Concepts with Examples

1. Polymorphism
2. Overloading
3. Inheritance

1. Polymorphism

Polymorphism allows objects of different types to be treated as objects of a common


supertype. It enables method overriding, where a subclass provides a specific
implementation of a method already defined in its parent class.

Example in C++ (Runtime Polymorphism using Method Overriding)


cpp
CopyEdit
#include <iostream>
using namespace std;

class Animal {
public:
virtual void makeSound() { // Virtual function for polymorphism
cout << "Animal makes a sound" << endl;
}
};

class Dog : public Animal {


public:
void makeSound() override { // Overriding the parent class method
cout << "Dog barks" << endl;
}
};

int main() {
Animal* a; // Pointer of base class type
Dog d;
a = &d; // Base class pointer points to derived class object
a->makeSound(); // Calls Dog's makeSound() due to polymorphism

return 0;
}

Output:

CopyEdit
Dog barks

Key Points:

 virtual keyword enables runtime polymorphism.


 The base class pointer calls the overridden method in the derived class.

2. Overloading

Overloading allows multiple methods/functions to have the same name but different
signatures (parameter lists). It occurs in two ways:

 Function Overloading: Multiple functions with the same name but different parameters.
 Operator Overloading: Redefining operators for user-defined types.

Example in C++ (Function Overloading)


cpp
CopyEdit
#include <iostream>
using namespace std;

class Math {
public:
// Function Overloading: Same function name, different parameters
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}
};

int main() {
Math obj;
cout << obj.add(5, 10) << endl; // Calls int version
cout << obj.add(5.5, 2.2) << endl; // Calls double version

return 0;
}

Output:

CopyEdit
15
7.7

Key Points:

 Function names are the same (add), but parameters differ.


 The compiler determines which function to call based on arguments.

3. Inheritance

Inheritance allows a class (child) to derive properties and behavior from another class
(parent), promoting code reusability.

Example in C++ (Single Inheritance)


cpp
CopyEdit
#include <iostream>
using namespace std;

class Parent {
public:
void show() {
cout << "This is the Parent class" << endl;
}
};

class Child : public Parent { // Child inherits from Parent


public:
void display() {
cout << "This is the Child class" << endl;
}
};

int main() {
Child obj;
obj.show(); // Inherited function
obj.display(); // Child class function

return 0;
}

Output:

python
CopyEdit
This is the Parent class
This is the Child class

Key Points:

 Child class inherits from Parent using public access specifier.


 The child class can access Parent's public methods.

Summary Table
Concept Description Example

Same interface, different behavior


Polymorphism Virtual functions in C++
(method overriding)

Same function/operator name, different Function overloading (add(int, int) vs


Overloading
parameters add(double, double))

Inheritance A class derives from another class Child : public Parent

Q.What are the advantages and disadvantages of using


polymorphism ,overloading ,inheritance concept.
Advantages and Disadvantages of Polymorphism, Overloading, and Inheritance

1. Polymorphism

🔹 Definition: Allows objects of different classes to be treated as objects of a common base class, enabling
method overriding and dynamic method dispatch.

✅ Advantages of Polymorphism

1. Code Reusability – Allows the same interface for different types, reducing code duplication.
2. Extensibility – New behaviors can be added with minimal modification.
3. Improves Readability & Maintainability – Easier to manage large codebases by using a common
interface.
4. Dynamic Method Invocation – Enables runtime decision-making (via virtual functions in C++).

❌ Disadvantages of Polymorphism

1. Increased Complexity – More abstract classes and interfaces make code harder to understand.
2. Performance Overhead – Runtime polymorphism (via virtual functions) introduces a slight
performance cost.
3. Difficult Debugging – Tracing overridden methods across multiple classes can be challenging.
4. Memory Overhead – Requires extra memory for vtables and pointers in languages like C++.
2. Overloading

🔹 Definition: Allows multiple functions or operators with the same name but different signatures
(different parameters or return types).

✅ Advantages of Overloading

1. Improves Code Readability – The same function name represents multiple related behaviors.
2. Enhances Code Reusability – Allows different implementations without defining multiple
function names.
3. Reduces Function Naming Conflicts – Provides intuitive function names for different parameter
types.
4. Efficient Use of Memory – The compiler determines the function at compile-time (no runtime
overhead).

❌ Disadvantages of Overloading

1. Confusion in Usage – Too many overloaded functions can make it difficult to determine the right
one.
2. Limited by Language Rules – Some languages (e.g., Python) do not support function overloading
natively.
3. Maintenance Issues – Changes to one overloaded function might require changes to others.
4. Ambiguity Errors – The compiler might struggle to determine which overloaded function to use
in some cases.

3. Inheritance

🔹 Definition: Allows a class (child) to inherit properties and behaviors from another class (parent),
promoting code reuse.

✅ Advantages of Inheritance

1. Code Reusability – Reduces duplication by using base class methods.


2. Enhances Extensibility – New functionality can be added by extending the base class.
3. Better Organization – Establishes a clear hierarchical relationship between classes.
4. Improved Maintainability – Changes to a parent class automatically propagate to child classes.

❌ Disadvantages of Inheritance

1. Tight Coupling – Child classes depend heavily on parent classes, making changes difficult.
2. Increased Complexity – Multiple levels of inheritance can make code harder to manage.
3. Unintended Side Effects – Changes in a base class can affect all derived classes, potentially
causing unexpected behavior.
4. Performance Overhead – Inherited methods take additional memory and processing time.

Summary Table
Concept Advantages Disadvantages

Code reusability, flexibility, maintainability, Increased complexity, debugging


Polymorphism
dynamic behavior issues, performance overhead

Improved readability, reusability, avoids naming Confusion in usage, ambiguity errors,


Overloading
conflicts, compile-time efficiency language limitations

Reduces redundancy, easy extension, better Tight coupling, unintended side effects,
Inheritance
code organization performance overhead

Q.What were type parameterization in principles of programming


langauge.

Type Parameterization in Principles of Programming Languages

Type parameterization is a programming language feature that allows the creation of generic types or
polymorphic functions that can operate on different data types while maintaining type safety. It enables
code reusability and abstraction.

1. What is Type Parameterization?

Type parameterization means defining functions, classes, or data structures that can operate on
multiple types without specifying the exact type at the time of writing. The actual type is provided as a
parameter when the function or class is used.

Example in C++ (Templates)

cpp
CopyEdit
#include <iostream>
using namespace std;

// Template Function with Type Parameter T


template <typename T>
T add(T a, T b) {
return a + b;
}

int main() {
cout << add(5, 10) << endl; // Works with integers
cout << add(5.5, 2.3) << endl; // Works with floats
return 0;
}

✅ Benefits: The same function add() works for multiple data types.

2. Type Parameterization in Different Languages

A. Generic Programming (C++, Java)

Languages like C++ (Templates) and Java (Generics) allow defining parameterized classes and methods.

Example in Java (Generics)

java
CopyEdit
// Generic Class with Type Parameter T
class Box<T> {
private T value;

public void setValue(T value) {


this.value = value;
}

public T getValue() {
return value;
}
}

public class Main {


public static void main(String[] args) {
Box<Integer> intBox = new Box<>();
intBox.setValue(100);
System.out.println(intBox.getValue()); // Output: 100

Box<String> strBox = new Box<>();


strBox.setValue("Hello");
System.out.println(strBox.getValue()); // Output: Hello
}
}

✅ Benefits: Box<T> can store any data type (Integer, String, etc.).
B. Parametric Polymorphism (Functional Languages - Haskell, Scala)

Functional languages use parametric polymorphism for type abstraction.

Example in Haskell

haskell
CopyEdit
-- Function that works for any type 'a'
identity :: a -> a
identity x = x

✅ Benefit: Works for any type (Int, String, etc.) without changing the function.

3. Advantages and Disadvantages of Type Parameterization


Aspect Advantages Disadvantages

Code Reusability Write once, use with multiple types May increase compilation time

Type Safety Reduces runtime errors Can be complex to debug

Flexibility Works with any compatible data type Not all languages support it

Performance No need for explicit type conversions Might introduce overhead in some cases

4. Type Parameterization vs. Other Concepts


Feature Type Parameterization Polymorphism Overloading

Use type parameters to Same interface, different Same function name,


Definition
generalize code implementations different parameters

template <typename T> in C+


Example Virtual functions in C++ Function overloading in C++
+

Type
High Medium High
Safety

Use Case Generic classes & functions Method overriding Different function signatures

5. Conclusion

Type parameterization enhances code reusability, flexibility, and type safety, making it a core feature in
modern programming languages. It is widely used in generic programming (C++, Java), functional
programming (Haskell, Scala), and databases (SQL, GraphQL).

You might also like