0
0
C++programming~30 mins

Why object-oriented programming is used in C++ - See It in Action

Choose your learning style9 modes available
Why Object-Oriented Programming is Used
πŸ“– Scenario: Imagine you are building a simple program to manage a library. You want to keep track of books and their details like title, author, and whether they are checked out. Using object-oriented programming helps organize this information clearly and makes your program easier to manage and expand.
🎯 Goal: You will create a simple class to represent a book, set up some book objects, and then display their details. This will show why object-oriented programming is useful for organizing related data and actions together.
πŸ“‹ What You'll Learn
Create a class called Book with three public member variables: title, author, and isCheckedOut.
Create two Book objects with specific titles and authors, and set their isCheckedOut status.
Write a function to display the details of a Book object.
Call the display function for each book to show their information.
πŸ’‘ Why This Matters
🌍 Real World
Object-oriented programming is used to model real-world things like books, cars, or people in software. It helps organize data and actions together, making programs easier to understand and change.
πŸ’Ό Career
Many software jobs require understanding classes and objects because they are the foundation of large applications, from games to business software.
Progress0 / 4 steps
1
Create the Book class
Create a class called Book with three public member variables: title (string), author (string), and isCheckedOut (bool).
C++
Need a hint?

Use the class keyword to define Book. Declare the variables as public inside the class.

2
Create two Book objects
Create two Book objects named book1 and book2. Set book1.title to "The Great Gatsby", book1.author to "F. Scott Fitzgerald", and book1.isCheckedOut to false. Set book2.title to "1984", book2.author to "George Orwell", and book2.isCheckedOut to true.
C++
Need a hint?

Create objects using the class name and set each member variable using dot notation.

3
Write a function to display book details
Write a function called displayBook that takes a Book object as a parameter and prints its title, author, and isCheckedOut status as "Available" if isCheckedOut is false, or "Checked out" if true.
C++
Need a hint?

Use a function that takes a const Book& parameter to avoid copying. Use cout to print each detail. Use the conditional operator to print status.

4
Display details of both books
Call the displayBook function for book1 and book2 inside main() to print their details.
C++
Need a hint?

Call displayBook(book1); and displayBook(book2); inside main(). Add a blank line between outputs for readability.