๐ 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++
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++
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++
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++
Hint
Call displayBook(book1); and displayBook(book2); inside main(). Add a blank line between outputs for readability.
Practice
(1/5)
1. Why is object-oriented programming (OOP) used in C++? class Car { ... };
easy
A. To make programs run faster by avoiding classes
B. To write code only using functions without any data
C. To group data and functions that operate on that data into one unit
D. To use only global variables for all data
Solution
Step 1: Understand OOP structure
OOP groups related data and functions into classes, making code organized.
Step 2: Compare options
Only To group data and functions that operate on that data into one unit describes grouping data and functions together, which is the core of OOP.
Final Answer:
To group data and functions that operate on that data into one unit -> Option C
Quick Check:
OOP groups data and actions = A [OK]
Hint: OOP bundles data and actions together [OK]
Common Mistakes:
Thinking OOP is only about faster code
Confusing OOP with procedural programming
Believing OOP avoids using classes
2. Which of the following is the correct way to define a class in C++?
easy
A. class Animal: void sound() {}
B. class Animal { public: void sound() {} };
C. def class Animal { void sound() {} }
D. Animal class { void sound() {} };
Solution
Step 1: Check C++ class syntax
In C++, classes start with the keyword 'class', followed by the name and braces containing members.
Step 2: Validate options
Only class Animal { public: void sound() {} }; uses correct C++ syntax with 'class', braces, and member function declaration.
Final Answer:
class Animal { public: void sound() {} }; -> Option B
Quick Check:
Correct C++ class syntax = C [OK]
Hint: C++ classes start with 'class' keyword and braces {} [OK]
Common Mistakes:
Using Python or other language syntax
Missing semicolon after class definition
Placing colon incorrectly after class name
3. How does object-oriented programming help in modeling real-world things in C++? Choose the best explanation.
easy
A. By using only global variables for all data
B. By writing all code in a single main function
C. By avoiding the use of variables and functions
D. By using classes to represent objects with properties and behaviors
Solution
Step 1: Understand real-world modeling
Real-world things have properties (data) and behaviors (actions).
Step 2: Match with OOP concept
Classes in OOP represent these things by bundling properties and behaviors together.
Final Answer:
By using classes to represent objects with properties and behaviors -> Option D
Quick Check:
Classes model real-world objects = A [OK]
Hint: Classes model objects with data and actions [OK]
Common Mistakes:
Thinking all code should be in main
Ignoring the role of classes in modeling
Believing global variables model real-world things
4. What will be the output of this C++ code?
#include <iostream>
class Box {
public:
int length;
Box(int l) { length = l; }
int getLength() { return length; }
};
int main() {
Box b(10);
std::cout << b.getLength();
return 0;
}
medium
A. 10
B. length
C. 0
D. Compilation error
Solution
Step 1: Understand constructor usage
The constructor sets length to 10 when object b is created.
Step 2: Check getLength() output
Calling b.getLength() returns the length value, which is 10.
Final Answer:
10 -> Option A
Quick Check:
Constructor sets length = 10, output = 10 [OK]
Hint: Constructor sets value; method returns it [OK]
Common Mistakes:
Expecting variable name printed instead of value
Thinking default value is zero without constructor
Assuming syntax error due to missing std:: prefix
5. Find the error in this C++ class code:
class Person {
string name;
public:
void setName(string n) {
name = n;
}
string getName() {
return name;
}
};
medium
A. Missing #include <string> and std:: prefix for string
B. Missing semicolon after class definition
C. setName() should return string
D. getName() should be void
Solution
Step 1: Check string usage
The code uses 'string' but does not include <string> or use std::string.
Step 2: Validate other syntax
Class ends with semicolon; setName() and getName() signatures are correct.
Final Answer:
Missing #include <string> and std:: prefix for string -> Option A
Quick Check:
Using string requires include and std:: [OK]
Hint: Always include <string> and use std::string [OK]
Common Mistakes:
Forgetting to add semicolon after class
Changing return types unnecessarily
Ignoring namespace std for string
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.