๐ Scenario: Imagine you are creating a simple program to represent a book in a library. Each book has a title and a number of pages. You want to create a class to hold this information and learn how to use a default constructor to set initial values.
๐ฏ Goal: Build a C++ class called Book with a default constructor that sets the title to "Unknown" and pages to 0. Then create an object of this class and print its details.
๐ What You'll Learn
Create a class named Book
Add two member variables: title (string) and pages (int)
Write a default constructor that sets title to "Unknown" and pages to 0
Create an object of Book using the default constructor
Print the title and pages of the object
๐ก Why This Matters
๐ Real World
Default constructors help create objects with safe starting values, useful in many programs like managing library books or user profiles.
๐ผ Career
Understanding constructors is essential for C++ programming jobs, especially in software development and systems programming.
Progress0 / 4 steps
1
Create the Book class with member variables
Write a class called Book with two public member variables: title of type std::string and pages of type int.
C++
Hint
Use class Book { public: std::string title; int pages; }; to define the class.
2
Add a default constructor to Book
Inside the Book class, add a default constructor named Book() that sets title to "Unknown" and pages to 0.
C++
Hint
Write a constructor like Book() { title = "Unknown"; pages = 0; }.
3
Create a Book object using the default constructor
In the main() function, create an object named myBook of type Book using the default constructor.
C++
Hint
Write Book myBook; inside main().
4
Print the title and pages of myBook
Add code in main() to print the title and pages of the myBook object. Use std::cout to print exactly: Title: Unknown and Pages: 0 on separate lines.
C++
Hint
Use std::cout << "Title: " << myBook.title << std::endl; and similarly for pages.
Practice
(1/5)
1. What is a default constructor in C++?
easy
A. A constructor that requires at least one argument.
B. A constructor with no parameters that initializes objects with default values.
C. A function that deletes objects automatically.
D. A special function that runs only when the program ends.
Solution
Step 1: Understand constructor basics
A constructor is a special function to create objects.
Step 2: Identify default constructor meaning
A default constructor has no parameters and sets default values.
Final Answer:
A constructor with no parameters that initializes objects with default values. -> Option B
Quick Check:
Default constructor = no parameters, default values [OK]
Hint: Default constructor has zero parameters [OK]
Common Mistakes:
Thinking default constructor requires arguments
Confusing destructor with constructor
Assuming default constructor runs at program end
2. Which of the following is the correct syntax for a default constructor in C++?
easy
A. class MyClass { MyClass() {} };
B. class MyClass { MyClass(int x) {} };
C. class MyClass { void MyClass() {} };
D. class MyClass { MyClass() int {} };
Solution
Step 1: Check constructor syntax
Constructor has the same name as the class and no return type.
Step 2: Identify default constructor syntax
Default constructor has empty parentheses and no parameters.
Final Answer:
class MyClass { MyClass() {} }; -> Option A
Quick Check:
Constructor syntax = class name + () + no return type [OK]
Hint: Constructor name = class name, no return type, empty () [OK]
Common Mistakes:
Adding void return type to constructor
Using parameters in default constructor
Incorrect placement of braces
3. What will be the output of this code?
class Box {
public:
int length;
Box() { length = 5; }
};
int main() {
Box b;
std::cout << b.length;
return 0;
}
medium
A. Compilation error
B. 0
C. 5
D. Random value
Solution
Step 1: Understand the default constructor
The constructor sets length to 5 when object b is created.
Step 2: Check output statement
Printing b.length outputs the value set by constructor, which is 5.
Final Answer:
5 -> Option C
Quick Check:
Constructor sets length=5, output = 5 [OK]
Hint: Default constructor sets values before output [OK]
Common Mistakes:
Assuming uninitialized int defaults to 0
Expecting compilation error due to missing initialization
Thinking output is random without constructor
4. Identify the error in this class definition:
class Sample {
public:
int x;
Sample() {
x = 10
}
};
medium
A. Class missing closing brace
B. Constructor name does not match class name
C. Missing return type in constructor
D. Missing semicolon after x = 10
Solution
Step 1: Check constructor body syntax
Each statement inside constructor must end with a semicolon.
Step 2: Locate missing semicolon
The line 'x = 10' lacks a semicolon at the end.
Final Answer:
Missing semicolon after x = 10 -> Option D
Quick Check:
Statements need semicolons [OK]
Hint: Check for missing semicolons inside constructor [OK]
Common Mistakes:
Adding return type to constructor
Ignoring missing semicolon errors
Confusing constructor name with return type
5. Given this class:
class Point {
public:
int x, y;
Point() { x = 0; y = 0; }
Point(int a, int b) { x = a; y = b; }
};
// In main:
Point p1;
Point p2(3, 4);
std::cout << p1.x << "," << p1.y << " " << p2.x << "," << p2.y;
What is the output and why?
hard
A. 0,0 3,4 because p1 uses default constructor, p2 uses parameterized constructor.
B. 3,4 0,0 because constructors are swapped.
C. Compilation error due to multiple constructors.
D. 0,0 0,0 because default constructor overrides parameterized one.
Solution
Step 1: Understand constructor calls
p1 is created with no arguments, so default constructor sets x=0, y=0.
Step 2: Understand parameterized constructor
p2 is created with (3,4), so x=3, y=4 from parameterized constructor.
Step 3: Check output statement
Output prints p1's coordinates then p2's coordinates separated by space.
Final Answer:
0,0 3,4 because p1 uses default constructor, p2 uses parameterized constructor. -> Option A
Quick Check:
Default and parameterized constructors work together [OK]
Hint: Default constructor for no args, parameterized for args [OK]