A parameterized constructor is a special constructor that takes arguments to initialize an object with specific values when it is created.
Click to reveal answer
beginner
How does a parameterized constructor differ from a default constructor?
A default constructor takes no arguments and initializes objects with default values, while a parameterized constructor requires arguments to set initial values.
Click to reveal answer
beginner
Consider this code snippet:<br><pre>class Car {<br> public:<br> std::string brand;<br> Car(std::string b) { brand = b; }<br>};<br>Car myCar("Toyota");<br>What does this code do?
It creates an object myCar of class Car and initializes its brand to "Toyota" using the parameterized constructor.
Click to reveal answer
intermediate
Can a class have multiple parameterized constructors?
Yes, a class can have multiple parameterized constructors with different parameters. This is called constructor overloading.
Click to reveal answer
beginner
Why use a parameterized constructor instead of setting values after creating an object?
Using a parameterized constructor ensures the object is fully initialized right when it is created, reducing errors and making code cleaner.
Click to reveal answer
What is the main purpose of a parameterized constructor?
ATo initialize an object with specific values when created
BTo delete an object
CTo copy an object
DTo create a default object without values
✗ Incorrect
A parameterized constructor initializes an object with values passed as arguments.
Which of the following is a valid parameterized constructor declaration in C++?
Avoid Car(std::string brand);
BCar() { brand = ""; }
CCar(std::string brand) { this->brand = brand; }
DCar brand(std::string);
✗ Incorrect
Option C correctly defines a constructor with a parameter to initialize the brand.
What happens if you do not define any constructor in a class?
AThe compiler provides a default constructor
BThe program will not compile
CObjects cannot be created
DOnly parameterized constructors are created
✗ Incorrect
If no constructor is defined, the compiler automatically provides a default constructor.
Can a parameterized constructor be called without arguments?
AOnly if the class has no other constructors
BYes, always
COnly if default values are provided
DNo, it requires arguments
✗ Incorrect
A parameterized constructor requires arguments unless default values are specified.
What is constructor overloading?
AUsing constructors in inheritance
BHaving multiple constructors with different parameters
CCalling a constructor multiple times
DDeleting constructors
✗ Incorrect
Constructor overloading means defining multiple constructors with different parameter lists.
Explain what a parameterized constructor is and why it is useful.
Think about how you give an object its initial details when you create it.
You got /3 concepts.
Describe how constructor overloading works with parameterized constructors.
Imagine having different ways to create an object depending on what information you have.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of a parameterized constructor in C++?
easy
A. To initialize an object with specific values when it is created
B. To delete an object from memory
C. To copy one object to another
D. To create a function outside the class
Solution
Step 1: Understand constructor role
A constructor sets up an object when it is created.
Step 2: Identify parameterized constructor use
Parameterized constructors allow passing values to set object properties immediately.
Final Answer:
To initialize an object with specific values when it is created -> Option A
Quick Check:
Parameterized constructor = initialize with values [OK]
Hint: Think: constructor sets initial values when making an object [OK]
Common Mistakes:
Confusing constructor with destructor
Thinking constructors copy objects
Believing constructors are separate functions
2.
Which of the following is the correct syntax for a parameterized constructor in C++?
class Box {
public:
Box(int w, int h) { width = w; height = h; }
private:
int width, height;
};
easy
A. Box(int w, int h) { width = w; height = h; }
B. void Box(int w, int h) { width = w; height = h; }
C. Box() { width = w; height = h; }
D. Box(int w, int h) -> void { width = w; height = h; }
Solution
Step 1: Check constructor name and return type
Constructor must have the same name as the class and no return type.
Step 2: Verify parameter list and body
Box(int w, int h) { width = w; height = h; } matches correct syntax: name matches class, parameters present, no return type.
Final Answer:
Box(int w, int h) { width = w; height = h; } -> Option A
Quick Check:
Constructor = class name + no return type [OK]
Hint: Constructor has no return type and matches class name [OK]
Common Mistakes:
Adding void return type to constructor
Using different name than class
Missing parameters in parameterized constructor
3.
What will be the output of this code?
#include <iostream>
class Point {
public:
int x, y;
Point(int a, int b) { x = a; y = b; }
};
int main() {
Point p(3, 4);
std::cout << p.x << ", " << p.y << std::endl;
return 0;
}
medium
A. Compilation error
B. 3, 4
C. 0, 0
D. Random values
Solution
Step 1: Understand constructor call
The object p is created with values 3 and 4 passed to the constructor.
Step 2: Check member variable assignment and output
The constructor sets x=3 and y=4, so printing p.x and p.y outputs "3, 4".
Expecting compilation error due to missing default constructor
Confusing output format
4.
Identify the error in this code snippet:
#include <iostream>
class Car {
public:
int speed;
Car(int s) { speed = s; }
};
int main() {
Car c;
std::cout << c.speed << std::endl;
return 0;
}
medium
A. Variable speed is not declared
B. Incorrect syntax in constructor definition
C. Missing default constructor causes error when creating Car c;
D. Output will be garbage value
Solution
Step 1: Check object creation
The line Car c; tries to create an object without parameters.
Step 2: Verify constructors available
Only a parameterized constructor Car(int s) exists; no default constructor is defined.
Step 3: Understand compilation error
Because no default constructor exists, Car c; causes a compilation error.
Final Answer:
Missing default constructor causes error when creating Car c; -> Option C
Quick Check:
No default constructor + no args object = error [OK]
Hint: If no default constructor, you must pass arguments when creating object [OK]
Common Mistakes:
Assuming default constructor exists automatically
Thinking variable declaration is missing
Expecting garbage output instead of compile error
5.
Given this class, how can you create an object r with width 5 and height 10 using the parameterized constructor?
class Rectangle {
public:
int width, height;
Rectangle(int w, int h) { width = w; height = h; }
};
hard
A. Rectangle r = Rectangle{5};
B. Rectangle r();
C. Rectangle r = Rectangle();
D. Rectangle r = Rectangle(5, 10);
Solution
Step 1: Identify constructor parameters
The constructor requires two integers: width and height.
Step 2: Match object creation syntax
Rectangle r = Rectangle(5, 10); correctly calls the constructor with two arguments (5, 10) to create object r.
Step 3: Check other options
Rectangle r(); is a function declaration, not object creation. Rectangle r = Rectangle(); calls default constructor which doesn't exist. Rectangle r = Rectangle{5}; passes only one argument, which is invalid.
Final Answer:
Rectangle r = Rectangle(5, 10); -> Option D
Quick Check:
Pass all required parameters to constructor [OK]
Hint: Call constructor with all parameters to create object [OK]
Common Mistakes:
Using empty parentheses which declare a function
Calling constructor without required arguments
Passing wrong number of arguments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.