0
0
C++programming~30 mins

Procedural vs OOP approach in C++ - Hands-On Comparison

Choose your learning style9 modes available
Procedural vs OOP approach
πŸ“– 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++
Need a 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++
Need a 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++
Need a 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++
Need a hint?

Use std::cout inside printBookObject to print the object's members.