A parameterized constructor helps create objects with specific values right when you make them. It saves time by setting up the object automatically.
Parameterized constructor in C++
class ClassName { public: ClassName(type1 param1, type2 param2) { // use param1, param2 to set object properties } };
class Car { public: string color; Car(string c) { color = c; } };
class Point { public: int x, y; Point(int a, int b) { x = a; y = b; } };
This program creates a Book object with a title and number of pages using a parameterized constructor. It then prints these details.
#include <iostream> #include <string> using namespace std; class Book { public: string title; int pages; Book(string t, int p) { title = t; pages = p; } }; int main() { Book myBook("C++ Basics", 150); cout << "Title: " << myBook.title << "\n"; cout << "Pages: " << myBook.pages << "\n"; return 0; }
If you don't provide a parameterized constructor, C++ uses a default constructor with no parameters.
You can have multiple constructors with different parameters (called constructor overloading).
Parameterized constructors let you set object values when creating them.
They have the same name as the class and take parameters.
They make your code cleaner and easier to use.
Practice
What is the main purpose of a parameterized constructor in C++?
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 AQuick Check:
Parameterized constructor = initialize with values [OK]
- Confusing constructor with destructor
- Thinking constructors copy objects
- Believing constructors are separate functions
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;
};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 AQuick Check:
Constructor = class name + no return type [OK]
- Adding void return type to constructor
- Using different name than class
- Missing parameters in parameterized constructor
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;
}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".Final Answer:
3, 4 -> Option BQuick Check:
Constructor sets values correctly = 3, 4 [OK]
- Assuming default values instead of passed ones
- Expecting compilation error due to missing default constructor
- Confusing output format
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;
}Solution
Step 1: Check object creation
The lineCar c;tries to create an object without parameters.Step 2: Verify constructors available
Only a parameterized constructorCar(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 creatingCar c;-> Option CQuick Check:
No default constructor + no args object = error [OK]
- Assuming default constructor exists automatically
- Thinking variable declaration is missing
- Expecting garbage output instead of compile error
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; }
};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 DQuick Check:
Pass all required parameters to constructor [OK]
- Using empty parentheses which declare a function
- Calling constructor without required arguments
- Passing wrong number of arguments
Ready to prove it?
Pass the test and this pattern moves unknown
