0% found this document useful (0 votes)
5 views4 pages

C++20+ Modular Programming & OOP Concepts

The document covers four lectures on C++ programming concepts, including modular programming, classes and data abstraction, relationships between classes, and polymorphism. Key topics include module interfaces, access specifiers, composition vs inheritance, and the use of virtual functions for polymorphism. Best practices are provided for each topic to enhance clarity and maintainability in code.

Uploaded by

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

C++20+ Modular Programming & OOP Concepts

The document covers four lectures on C++ programming concepts, including modular programming, classes and data abstraction, relationships between classes, and polymorphism. Key topics include module interfaces, access specifiers, composition vs inheritance, and the use of virtual functions for polymorphism. Best practices are provided for each topic to enhance clarity and maintainability in code.

Uploaded by

chidinojoko2004
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 1: Modular Programming

Key Concepts:

• Modules are self-contained subcomponents of related functionality in C++20+

• Module Interface: Combination of all entities a module exports (functions,


types, constants)

• Exporting: Using export keyword to make entities available to other files

• Importing: Using import to use exported entities from other modules

Important Details:

• Module declaration syntax: export module ModuleName;

• Module names can use dots (e.g., [Link])

• Only module interface files (with export module) can contain export declarations

• Implementation can be separated from interface within same file or in separate


files

• Import uses module name, not filename: import LibMath;

Best Practices:

• Group related functionality into modules

• Consider separating interface from implementation for clarity

• Use meaningful module names that reflect their purpose

Lecture 2: Classes and Data Abstraction

Key Concepts:

• Class: Collection of components (members) that can be variables or functions

• Access Specifiers:

o private (default): Accessible only within class

o public: Accessible outside class

o protected: Accessible by derived classes

• Constructors: Special functions for object initialization

• Destructors: For cleaning up resources when object goes out of scope

• Abstract Data Type (ADT): Separates logical properties from implementation

Important Details:
• UML notation: + public, - private, # protected

• Member access syntax: [Link]

• Constructors:

o Same name as class

o No return type

o Can be overloaded

o Default constructor has no parameters

• Static members:

o Shared by all objects of class

o Accessed using class name and scope resolution operator

Best Practices:

• Use constructors to ensure proper initialization

• Make destructors virtual in base classes (more in Lecture 4)

• Separate interface (.h) from implementation (.cpp)

• Use accessor/mutator functions to control access to member variables

Lecture 3: Has-a and Is-a Relationships

Key Concepts:

• Composition (has-a): One class contains objects of another class

• Inheritance (is-a): Derived class inherits from base class

• Types of Inheritance:

o Public: Base class public members stay public in derived class

o Protected: Base class public members become protected in derived class

o Private: Base class public members become private in derived class

Important Details:

• Derived class syntax: class Derived : access-specifier Base

• Redefining member functions:

o Must have same signature

o Use scope resolution to call base version: Base::function()


• Constructor calling order:

o Base class constructor executes first

o Member objects constructed next (in declaration order)

o Derived class constructor executes last

• Destructors execute in reverse order

Best Practices:

• Use public inheritance for "is-a" relationships

• Use composition for "has-a" relationships

• Make base class destructors virtual when using polymorphism

• Consider using protected members for derived class access

Lecture 4: Polymorphism

Key Concepts:

• Polymorphism: "One interface, multiple implementations"

• Virtual Functions: Enable runtime polymorphism via late binding

• Pure Virtual Functions: virtual void draw() = 0; (no implementation)

• Abstract Base Class: Contains at least one pure virtual function

• Late Binding: Function call resolved at runtime based on object type

Important Details:

• Virtual function declaration: virtual returnType function()

• Overriding vs redefining:

o Virtual functions changed: overridden

o Non-virtual functions changed: redefined

• Slicing Problem:

o Occurs when derived object assigned to base object

o Derived-specific members are "sliced off"

o Can be avoided using pointers/references

• Virtual destructors:

o Ensure proper cleanup of derived objects


o Should be used in polymorphic base classes

Best Practices:

• Use virtual functions for polymorphic behavior

• Make base class destructors virtual

• Avoid downcasting when possible

• Use abstract base classes to define interfaces

• Be aware of performance overhead with virtual functions

You might also like