Unit 3
Unit 3
2023-24
Optimization of Design refers to improving a system's design to achieve the best possible
performance, efficiency, and functionality while minimizing costs, errors, and resource usage. This
involves refining the design to meet the required specifications with the least complexity and
maximum usability.
Key Objectives:
2. What do you mean by documentation? What are the various considerations in documentation
designing? Explain. (10 marks)
Documentation:
Documentation is the process of creating and maintaining written records, guides, and instructions
about a system, software, or process. It ensures that users, developers, and stakeholders can
understand, operate, and maintain the system effectively.
Types of Documentation:
2. Technical Documentation: Details for developers or system administrators about the system’s
functionality, structure, and implementation.
1. Audience Analysis: Understand who will use the documentation (e.g., developers, end-
users).
2. Clarity and Simplicity: Use simple language, avoiding technical jargon for non-technical users.
3. Structure and Layout: Use clear headings, subheadings, and sections for better navigation.
4. Accuracy and Completeness: Ensure all required information is included and up-to-date.
8. Accessibility: Ensure the documentation is accessible in various formats (PDF, online, etc.).
3. What is Data Abstraction? How is it different from Encapsulation? Explain with proper examples.
(10 marks)
Data Abstraction:
Data abstraction is the process of hiding the internal implementation details of a system or object
and exposing only the essential features to the user. It focuses on "what" the system does rather
than "how" it does it.
Encapsulation:
Encapsulation refers to wrapping data (variables) and methods (functions) into a single unit, usually a
class, and restricting direct access to some of the object’s components. It emphasizes controlling
access to the internal state of an object.
Code Example:
java
Copy code
void draw() {
System.out.println("Drawing a Circle");
}
// Encapsulation Example
class Account {
return balance;
Data Abstraction: The user only knows about the draw() method and not the internal
implementation.
Encapsulation: The balance field is protected, and access is controlled using getters and
setters.
4. How is it different from multiple inheritance and modeled using nested generalization? (2
marks)
Multiple Inheritance:
Multiple inheritance is when a class inherits features from more than one parent class.
Example: A class inheriting properties from two base classes like A and B.
Nested Generalization:
Example: Generalizing a base class "Vehicle" into "Car" and "Bike," which further branch into
more specific types.
Difference:
Nested generalization uses inheritance to create a tree-like structure, ensuring clarity and avoiding
the complexity of multiple inheritance where conflicts may arise due to the diamond problem.
Procedural Programming:
Example: C programming.
Key Differences:
Code Reusability Less reusable, relies on functions. More reusable through inheritance.
6. Prepare a DFD for computing the volume and surface area of a cone. Discuss some ways of
specifying operations. (10 marks)
DFD Representation:
[User Input]
[Calculation Process]
↓
→ [Volume]
→ [Surface Area]
1. Functional Specification: Clearly define the formulas or logic used for computation.
7. What do you understand by a static member function of a class? Discuss their characteristics.
Give an example where you can justify the use of static member functions. (10 marks)
A static member function is associated with the class rather than any specific object.
Characteristics:
Example:
#include<iostream>
class Counter {
public:
count++;
return count;
}
};
int Counter::count = 0;
int main() {
return 0;
Justification:
Static functions are ideal for maintaining shared properties or behaviors, like counting the number of
objects created or logging system-wide data.
i. SA/SD (Structured Analysis and Structured Design) vs. OMT (Object Modeling Technique):
Focus What the system does. What the system contains and behaves.
Use Case Suitable for small, procedural systems. Suitable for large, complex systems.
ii. SA/SD (Structured Analysis and Structured Design) vs. JSD (Jackson System Development):
9. What are Constructors in Object-Oriented Programming? Explain their types with examples. (10
marks)
Constructors:
A constructor is a special member function in a class that is automatically called when an object of
the class is created. It initializes the object’s data members.
Characteristics:
1. Same name as the class.
3. Automatically invoked.
Types of Constructors:
1. Default Constructor:
o Example:
Code
class Demo {
int x;
public:
};
2. Parameterized Constructor:
o Example:
Code
class Demo {
int x;
public:
};
3. Copy Constructor:
o Example:
Code
class Demo {
int x;
public:
Demo(int val) { x = val; }
};
10. What are the advantages of Object-Oriented Programming (OOP)? How does it differ from
Structured Programming? (10 marks)
Advantages of OOP:
5. Real-World Modeling: Models complex systems as objects with attributes and behaviors.
11. Explain the concept of Inheritance in Object-Oriented Programming. What are its types?
Provide examples. (10 marks)
Inheritance:
Inheritance allows a class (child class) to acquire properties and behaviors of another class (parent
class). It promotes code reuse and creates a hierarchical relationship between classes.
Types of Inheritance:
1. Single Inheritance: One child class inherits from one parent class.
o Example:
Code
class Parent {
2. Multilevel Inheritance: A class inherits from another class, which in turn inherits from
another.
o Example:
Code
class Grandparent {
};
o Example:
Code
class Parent {
};
4. Multiple Inheritance: A class inherits from more than one parent class.
o Example:
Code
class Parent1 {
};
class Parent2 {
};
Polymorphism:
Polymorphism allows the same function or operator to behave differently based on the context. It
enhances flexibility and reusability in programs.
Types of Polymorphism:
o Example:
Code
class Demo {
public:
};
o Example:
Code
class Parent {
};
};
13. What is a Virtual Function? Explain its need and working with an example. (10 marks)
Virtual Function:
A virtual function is a member function in a base class that can be overridden in a derived class. It
ensures that the function called is determined by the object type (runtime polymorphism), not by
the pointer type.
Ensures flexibility in cases where the object’s actual type is determined at runtime.
Working of Virtual Function:
When a base class declares a function as virtual, the compiler creates a vtable (virtual table), and
each derived class has its own entry. At runtime, the vtable is used to resolve which function to call.
Example:
cpp
Copy code
#include <iostream>
class Base {
public:
};
public:
};
int main() {
Base *ptr;
Derived obj;
ptr = &obj;
return 0;
Output:
Derived class
14. What is a Friend Function? Explain its purpose with an example. (10 marks)
Friend Function:
A friend function is a non-member function that can access the private and protected members of a
class. It is declared using the friend keyword inside the class definition.
Purpose:
1. Provides access to private data of a class to functions that are not its members.
Example:
cpp
Copy code
#include <iostream>
class Demo {
private:
int data;
public:
};
int main() {
Demo obj(10);
return 0;
}
Output:
Data: 10
15. Write a short note on Exception Handling in C++. Explain try, catch, and throw with an example.
(10 marks)
Syntax:
cpp
Copy code
try {
throw exception;
Example:
cpp
Copy code
#include <iostream>
if (b == 0)
}
int main() {
try {
divide(10, 2);
return 0;
Output:
Result: 5
Exception caught: Division by zero error!
16. What are Design Patterns? Explain any two design patterns with examples. (10 marks)
Design Patterns:
Design patterns are reusable solutions to common software design problems. They are templates
that can be applied to specific problems in object-oriented design.
1. Creational Patterns: Deal with object creation mechanisms (e.g., Singleton, Factory).
3. Behavioral Patterns: Deal with object interaction and responsibilities (e.g., Observer,
Strategy).
Ensures a class has only one instance and provides a global point of access to it.
Code
#include <iostream>
class Singleton {
private:
if (!instance)
return instance;
};
int main() {
cout << (s1 == s2); // Output: 1 (both are the same instance)
return 0;
code
#include <iostream>
#include <vector>
class Observer {
public:
};
class WeatherStation {
vector<Observer*> observers;
int temperature;
public:
void addObserver(Observer *obs) { observers.push_back(obs); }
temperature = temp;
obs->update(temp);
};
public:
cout << "Temperature updated: " << temp << "°C" << endl;
};
int main() {
WeatherStation station;
station.addObserver(&display1);
station.addObserver(&display2);
station.setTemperature(25);
return 0;
17. Explain the relationship between classes with examples. (10 marks)
Class Relationships:
Relationships describe how classes interact or depend on each other in object-oriented design.
Types of Relationships:
2. Aggregation: A "has-a" relationship where a class is made up of other classes but can exist
independently.
o Example: A Team has Players.
3. Composition: A stronger form of aggregation where the containing class cannot exist
without its components.
Examples:
// Association Example
class Teacher {
public:
};
// Aggregation Example
class Team {
vector<Player*> players;
};
// Composition Example
class Car {
};
// Inheritance Example
18. What are Interaction Diagrams? Explain Sequence Diagrams and Collaboration Diagrams. (10
marks)
Interaction Diagrams:
Interaction diagrams model the dynamic aspects of a system. They show how objects communicate
with each other.
1. Sequence Diagram: Focuses on the time order of messages exchanged between objects.
code
Code
+-------------------+
| User |
| ----------------- |
| - credentials |
| ----------------- |
| + enter() |
+-------------------+
+-------------------+
| LoginPage |
| ----------------- |
| - credentials |
| ----------------- |
| + validate() |
+-------------------+
|
+-------------------+
| Database |
| ----------------- |
| - records |
| ----------------- |
| + check() |
+-------------------+
19. What is the role of visibility in object design? Explain public, private, and protected with
examples. (10 marks)
Access Specifiers:
3. Protected: Members are accessible within the class and its derived classes.
Example:
Code
#include <iostream>
class Base {
private:
protected:
public:
};
class Derived : public Base {
public:
void show() {
};
int main() {
Derived obj;
return 0;
20. Explain the concept of Object Design with reference to relationships and responsibility. (10
marks)
Object Design:
Object design focuses on how classes and objects interact to perform system functionality. It bridges
the gap between the analysis model and the implementation model.
2. Responsibilities: Assigning specific roles and tasks to objects based on the Single
Responsibility Principle (SRP).
Example:
class Book {
};
class Member {
};
class Transaction {
Member member;
Book book;
void borrowBook();
void returnBook();
};