0% found this document useful (0 votes)
12 views2 pages

8 Construtors App

The document describes a C++ program that illustrates different types of constructors including a default constructor, parameterized constructor, and copy constructor. The program defines a MyClass with private integer and string members that are initialized differently depending on the constructor called. Main creates instances using each constructor and calls the display function to output the member values.

Uploaded by

prembisuit2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

8 Construtors App

The document describes a C++ program that illustrates different types of constructors including a default constructor, parameterized constructor, and copy constructor. The program defines a MyClass with private integer and string members that are initialized differently depending on the constructor called. Main creates instances using each constructor and calls the display function to output the member values.

Uploaded by

prembisuit2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical No.

Aim : Write a program to illustrate default constructor, parameterized


constructor and copy constructors.

Code :

#include <iostream>
#include <string>
class MyClass {
private:
int value;
std::string name;
public:
// Default Constructor
MyClass() {
value = 0;
name = "Default";
}
// Parameterized Constructor
MyClass(int val, const std::string& nm) {
value = val;
name = nm;
}
// Copy Constructor
MyClass(const MyClass& other) {
value = other.value;
name = other.name;
}
// Function to display the object's details
void display() {
std::cout << "Value: " << value << ", Name: " << name << std::endl;
}
};
int main() {
// Default Constructor
MyClass obj1;
std::cout << "Default Constructor:" << std::endl;
obj1.display();
// Parameterized Constructor
MyClass obj2(10, "Parameterized");
std::cout << "\nParameterized Constructor:" << std::endl;
obj2.display();
// Copy Constructor
MyClass obj3 = obj2;
std::cout << "\nCopy Constructor:" << std::endl;
obj3.display();
return 0;
}
Output :

You might also like