0
0
C++programming~30 mins

Data members and member functions in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Data members and member functions
πŸ“– Scenario: You are creating a simple program to manage a book's information in a library system.
🎯 Goal: Build a C++ class called Book with data members and member functions to store and display book details.
πŸ“‹ What You'll Learn
Create a class named Book
Add data members title and author of type std::string
Add a member function displayInfo() that prints the book's title and author
Create an object of Book and call displayInfo()
πŸ’‘ Why This Matters
🌍 Real World
Managing book information is common in library software, bookstore inventory, and reading apps.
πŸ’Ό Career
Understanding classes, data members, and member functions is fundamental for software development in C++.
Progress0 / 4 steps
1
Create the Book class with data members
Create a class called Book with two public data members: title and author, both of type std::string.
C++
Need a hint?

Use class Book { public: std::string title; std::string author; }; to define the class.

2
Add a member function to display book info
Inside the Book class, add a public member function called displayInfo() that prints the title and author separated by a comma and a space.
C++
Need a hint?

Define void displayInfo() { std::cout << title << ", " << author << std::endl; } inside the class.

3
Create a Book object and set data members
In main(), create an object called myBook of type Book. Set myBook.title to "The Great Gatsby" and myBook.author to "F. Scott Fitzgerald".
C++
Need a hint?

Use Book myBook; then assign myBook.title = "The Great Gatsby"; and myBook.author = "F. Scott Fitzgerald";.

4
Call the displayInfo() function to show book details
In main(), call the displayInfo() member function on the myBook object to print the book's title and author.
C++
Need a hint?

Call myBook.displayInfo(); to print the book details.