Pages

Tuesday, May 6, 2025

Copy Constructor

 #include <iostream>

#include <string>

using namespace std;


class Car {

    public:

        string name;

        string color;

    Car(string name, string color) {

        this->name = name;

        this->color = color;

    }

    

    Car(Car &original) {

        cout << "copying original to new..\n";

        name = original.name;

        color = original.color;

    }

};



int main() {

    Car c1("maruti 800", "blue");

    

    Car c2(c1); // Custom copy constructor

    cout << c2.name << endl; // maruti  800

    cout << c2.color << endl; // white


    return 0;

}

No comments:

Post a Comment

Encode and Decode Strings - NeetCode.IO Solution by me + gemini pro 3.0 explained

  class Solution {         // encode: List of Strings -> String     public String encode ( List < String > strs) {         Stri...