Pages

Saturday, May 10, 2025

Shallow & Deep Copy in OOPs

  #include <iostream>

using namespace std;

class Car{
    public:
        string name;
        string color;
        int *mileage;

    Car(string name, string color) {
        this->name = name;
        this->color = color;
        mileage = new int; // dynamic allocation
        *mileage = 12; // value allocated
    }

    Car(Car &original) {
        cout << "copying original to new..\n";
        name = original.name;
        color = original.color;
        mileage = new int; // yeh line likhne se , new memory allocate hogi
        *mileage = *original.mileage; // memory address copied from previous memory of mileage
    }
   
// 12 and 12 because * and * pointer laga hai  this is diff. between shallow n deep copy, to revise watch again for concept clarity..

};

int main() {
    Car c1("maruti 800", "white");
    Car c2(c1);
    cout << c2.name << endl;
    cout << c2.color << endl;
    cout << *c2.mileage << endl;

    *c2.mileage = 10; // changed to 10 (my mistake : remember to use * )

    cout << *c1.mileage << endl; // 10


    return 0;
}

Friday, May 9, 2025

Sum of Diagonal Matrix in C++

 #include <iostream>

using namespace std;

int diagonalSum(int mat[][3], int n) {
    int sum = 0;

    // O(n^2)
    for(int i=0; i<n; i++) { // rows
        for(int j=0; j<n; j++) // cols
        if(i == j) {
            sum += mat[i][j];
        } else if(j == n-i-1) {
            sum += mat[i][j];
        }
    }

    cout << "sum = " << sum << endl;
    return sum;
}

int main() {
int matrix[4][4] = {{1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 10, 11, 12},
                        {13, 14, 15, 16}};


int matrix2[3][3] = {{1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}};

diagonalSum(matrix2, 3);
    return 0;
}

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;

}

Practice question of Constructor in OOPs (C++)

 #include <iostream>

#include <string>

using namespace std;


class User  {

private:

    int id; 

    string password;

    

public:

        string username;

        

        User(int id) {

            this->id = id; // current is being stored to parameter id (right side is parameter) and left side is an object.

        } 

        

        

        // getter

        string getPassword() {

            return password;

        }

        

        //setter 

        void setPassword(string password) {

            this-> password = password;

        }

};


int main() {

    // Object

    User user1(101);

    user1.username = "apnacollege";

    user1.setPassword("abcd"); // always getting this wrong

    // correct is user1.setPassword("abcd");

    // not user.setPassword = "abcd"; for strings , mistake noted for next time!

    

    

    cout << "username : " << user1.username << endl;

    cout << "password : " << user1.getPassword() << endl;




    return 0;

}

Constructor in OOPs

 #include<iostream>

#include<string>

using namespace std;


class Car {

    string name;

    string color;

    

    public:

        Car() {

            cout << "constructor without paramas\n";

        }

    

        Car(string name, string color) {

            cout << "constructor with params\n";

            this->name = name;

            this->color = color;

            // *this.color = color; // this is a complex syntax so try to avoid it

        }

    

    

    void start() {

        cout << "car has started..\n";

    }

    

    void stop() {

        cout << "car has stopped..\n";

        }

    

    //Getter

    string getName() {

        return name;

    }

};


int main() {

    

    Car c0; // non-parameter

    Car c1("maruti 800", "white"); // parameter

    Car c2("fortuner", "white");

    

    return 0;

} 

Getters and Setters in OOPs

 class Student {

    private:

    //Properties


    string name;

    float cgpa;


    public:

    

    //Setters

    void setName(string nameVal) {  // remember to include #include <string> as well

        name = nameVal;

    }

    

    void setCgpa(float cgpaVal) {

        cgpa = cgpaVal;

    }

    

    //Getters

    string getName() {

        return name;

    }

    

    float getCgpa() {

        return cgpa;

    }

    


    //Methods


    void getPercentage() {


        cout << (cgpa*10) << endl;


    }


};




class User{


    int id;


    string username;


    string password;


    string bio; 


    


    void deactivate() {


        cout << "deactivating account\n";


    }


    


    void editBio(string newBio) {


        bio = newBio;


    }


};




int main() {


    Student s1; // objects

    s1.setName("raju");

    s1.setCgpa(9.0);

   

    cout << s1.getName() << endl;

    cout << s1.getCgpa() << endl;

 

}



MISTAKES:- 

    1. I put s1.setName = "raju"; which is WRONG!

        correct way:- s1.setName("raju");

    2. i did same for s1.setCgpa as well, learnt mistake ! 

Access modifiers in OOPs

 class Student {

    public:

    //Properties


    string name;

    float cgpa;


    


    //Methods


    void getPercentage() {


        cout << (cgpa*10) << endl;


    }


};




class User{


    int id;


    string username;


    string password;


    string bio; 


    


    void deactivate() {


        cout << "deactivating account\n";


    }


    


    void editBio(string newBio) {


        bio = newBio;


    }


};




int main() {


    Student s1; // objects


    s1.name = "debadattacodes";

    s1.cgpa = 9.0;

    

    cout << s1.cgpa << endl;

    s1.getPercentage();

    

    cout << s1.name << endl;

 

}

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...