UNIT 3 NEW OOSD
UNIT 3 NEW OOSD
on the design
1. Classes to Structs
A struct in C++ is similar to a class, but its members are public by default (whereas, in a
class, they are private by default).
Associations and Encapsulation in Object-Oriented Design
In object-oriented design, associations and encapsulation are two key concepts that help organize and structure
data, relationships, and behaviors.
1. Associations
Associations refer to the relationships between objects (or classes). These relationships define how one object
is connected or related to another. In object-oriented programming (OOP), associations can be:
•One-to-One
•One-to-Many
•Many-to-One
•Many-to-Many
#include <iostream>
// Method to add a book to the library
#include <cstring> void addBook(Book* book) {
// Book class (represents a book) if (bookCount < 10) {
class Book { books[bookCount++] = book; // Add book to the array
public: } else {
std::cout << "Library is full!" << std::endl;
char title[100];
}
// Constructor to initialize the book's title
}
Book(const char* title) {
// Method to display all books in the library
strncpy(this->title, title, sizeof(this->title) - 1);
void displayBooks() const {
this->title[sizeof(this->title) - 1] = '\0'; // Ensure nullstd::cout << "Books in the Library:\n";
termination for (int i = 0; i < bookCount; ++i) {
} books[i]->displayInfo(); // Call displayInfo() for each book
// Method to display book information }
void displayInfo() const { }
std::cout << "Book Title: " << title << std::endl; };
}
};
// Library class (holds multiple books)
class Library {
private:
Book* books[10]; // Array of pointers to Book objects (up to 10 books)
int bookCount; // Number of books in the library
public:
// Constructor to initialize the library with no books
Library() : bookCount(0) {}
int main() {
// Create books
Book book1("1984");
Book book2("Brave New World");
Book book3("The Catcher in the Rye");
// Create a library
Library library;
return 0;
}
Encapsulation in Data Structures:
When transitioning from classes to data structures, encapsulation is often sacrificed for simplicity and
performance. While you might not have the same strict data protection and behavior management, you can still
use functions to enforce certain behaviors or provide controlled access to the data.
For example, in the Student and Course example above, the encapsulation is not as strict. Instead of using a class
to encapsulate behavior (like enrolling a student), we just use data structures and functions. However, you can
still ensure controlled access to the data by using functions that manipulate the structure, like enroll() in the
Course class.
1. Reusability
Reusability refers to the ability to use a piece of code or a component (such as a class, function, or module) in
different programs or parts of the same program without modification.
return 0;
}
2. Extensibility
Extensibility refers to the ability to extend or modify the system easily without making major changes to the
existing code.
· Hides Complexity: By hiding the internal workings of a class and exposing only the necessary methods,
abstraction helps in managing complexity.
· Focus on Behavior: The goal is to define what an object can do (its behavior) rather than how it achieves
those actions.
· Interfaces or Abstract Classes: In many languages, abstract classes or interfaces are used to define abstract
behavior that must be implemented by subclasses.
// Derived class: Rectangle
class Rectangle : public Shape
Abstraction Example {
#include <iostream> public:
// Abstract base class void draw() const override
class Shape { {
public: std::cout << "Drawing a Rectangle!" << std::endl;
// Pure virtual function (makes this class abstract) }
virtual void draw() const = 0; };