#include <iostream>
Saturday, May 10, 2025
Shallow & Deep Copy in OOPs
Friday, May 9, 2025
Sum of Diagonal Matrix in C++
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...
-
public class Main { public static void printSolution(int sol[][]) { for (int i = 0; i < sol.length; i++) { for (...
-
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...
-
#include <iostream> #include <vector> using namespace std ; // conquer step - O(n) void merge ( int arr[], int si, int mid...