Real life example
Naya phone kharidte hi factory settings milti hain. Constructor bhi object ko shuru ki settings deta hai.
Industry example
Database connection class: constructor connection khol deta hai, har user ko manually nahi karna padta.
C++ code
class Student {
string name; int age;
public:
Student() : name("NA"), age(0) {}
Student(string n, int a) : name(n), age(a) {}
};
What happens when it runs
Student s1; = NA, 0
Student s2("Ali", 20); = Ali, 20
Run the full program in the compiler Interactive version
Quiz
Constructor ka return type kya hota hai?
Constructor ka koi return type nahi hota, naam class jaisa hota hai.
Try it yourself
Write one example of Constructor from your own home, school or work. Then change the program in the compiler and run it again.
All 12 OOP lectures
- Lecture 1: Introduction aur Objects
- Lecture 2: Encapsulation aur Interface
- Lecture 3: Abstraction, Class, Inheritance
- Lecture 4: Inheritance ke Concepts
- Lecture 5: Association, Composition, Aggregation
- Lecture 6: Polymorphism
- Lecture 7: C++ me Class
- Lecture 8: Constructor
- Lecture 9: Copy Constructor, Destructor, this
- Lecture 10: this Pointer aur const Functions
- Lecture 11: const, Initializer List, static
- Lecture 12: static Functions aur Array of Objects
START WELL TO END WELL