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

3917. Count Indices With Opposite Parity (Brute Force) O(n2) + Optimized Solution O(n) + tips LEETCODE WEEKLY 500

  class Solution {     public int [] countOppositeParity ( int [] nums ) {          // approach 1 - checks every pair         int n = n...