๐ Scenario: You are building a simple program to manage information about books in a library. First, you will create the program using a procedural style, then you will rewrite it using object-oriented programming (OOP) style.
๐ฏ Goal: Learn how to organize code in two ways: procedural and object-oriented. You will create a simple program that stores book details and prints them.
๐ What You'll Learn
Create a procedural version using variables and functions
Create an OOP version using a class
Use clear and simple code for both versions
Print book details in both versions
๐ก Why This Matters
๐ Real World
Managing collections of related data like books, students, or products is common in software. Procedural code works for simple tasks, but OOP helps organize bigger programs.
๐ผ Career
Understanding procedural and OOP approaches is essential for software development jobs. Many projects use classes and objects to keep code clean and reusable.
Progress0 / 4 steps
1
Create procedural data variables
Create three variables: title as a std::string with value "The Great Gatsby", author as a std::string with value "F. Scott Fitzgerald", and year as an int with value 1925.
C++
Hint
Use std::string for text and int for numbers.
2
Create a function to print book details
Create a function called printBook that takes three parameters: std::string title, std::string author, and int year. Inside the function, print the book details in the format: Title: [title], Author: [author], Year: [year]. Then call printBook inside main with the variables title, author, and year.
C++
Hint
Define the function before main. Use std::cout to print.
3
Create a Book class with members
Create a class called Book with three public members: title and author as std::string, and year as int. Inside main, create an object book of type Book and set its title to "The Great Gatsby", author to "F. Scott Fitzgerald", and year to 1925.
C++
Hint
Define the class before main. Use dot . to set object members.
4
Print book details using the Book object
Add a function called printBookObject that takes a Book object as a parameter and prints its details in the format: Title: [title], Author: [author], Year: [year]. Then call printBookObject inside main with the book object.
C++
Hint
Use std::cout inside printBookObject to print the object's members.
Practice
(1/5)
1.
Which of the following best describes procedural programming?
Procedural programming:
easy
A. Uses functions and step-by-step instructions to solve problems.
B. Groups data and functions inside classes called objects.
C. Focuses on creating reusable components with inheritance.
D. Uses event-driven programming with callbacks.
Solution
Step 1: Understand procedural programming basics
Procedural programming organizes code as a sequence of instructions and functions.
Step 2: Compare with other programming styles
OOP groups data and functions in classes, which is different from procedural style.
Final Answer:
Uses functions and step-by-step instructions to solve problems. -> Option A
You want to model a simple bank account in C++. Which approach is better and why?
Options:
Procedural: Use global variables for balance and functions to deposit/withdraw.
OOP: Create a BankAccount class with balance as private and methods for deposit/withdraw.
hard
A. Procedural is better because it is simpler and uses fewer lines of code.
B. OOP is worse because it cannot handle simple tasks like bank accounts.
C. Procedural is better because classes are slow and complex.
D. OOP is better because it groups data and functions, protecting balance and making code reusable.
Solution
Step 1: Understand the problem domain
A bank account needs to protect its balance and provide controlled access to deposit and withdraw money.
Step 2: Compare procedural and OOP approaches
Procedural uses global variables which can be changed anywhere, risking errors. OOP encapsulates balance inside a class, controlling access and improving code reuse.
Final Answer:
OOP is better because it groups data and functions, protecting balance and making code reusable. -> Option D
Quick Check:
OOP encapsulates data and behavior safely [OK]
Hint: Use OOP to protect data and group related functions [OK]
Common Mistakes:
Assuming procedural is always simpler
Thinking classes slow down simple programs
Ignoring data protection benefits of OOP
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.