Skip to main content

Posts

Showing posts with the label program to illustrate copy constructor

Write a program to illustrate copy constructor.

 Here's an example program to illustrate the use of a copy constructor in C++: #include <iostream> #include <string> using namespace std; class Person { private:     string name;     int age; public:     Person(string n, int a) {         name = n;         age = a;     }     // Copy constructor     Person(const Person& p) {         name = p.name;         age = p.age;     }     void display() {         cout << "Name: " << name << endl;         cout << "Age: " << age << endl;     } }; int main() {     // Create a person object     Person p1("John", 30);     // Call the display function     cout << "Original object:" << endl;     p1.display();     // Create ...