WebNest
Team/Shahnawaz Sazid/W_2_STRING_CLASS_OBJECTS

Repository

W_2_STRING_CLASS_OBJECTS

View on GitHub ↗
C++0 stars0 forks

README

W_2_M_2_MORE_ABOUT_STRING_IN_CPP

#include<bits/stdc++.h>
using namespace std;

int main(){
    string s = "Hello World"; // we will use this most of the time to create a string object
    string s2("hello world"); // another way to create a string object
    string s3("hello world", 5); // this will create a string object with first 5 characters of the given string. this does resize the string object to 5 characters.
    string s4(s,4); // this will create a string object with first 4 characters of the given string. this does resize the string object to 4 characters.
    string s5(5, 'a'); // this will create a string object with 5 characters 'a'. this does resize the string object to 5 characters.
    cout << s << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    cout << s5 << endl;

    return 0;
}
  • sort string
#include<bits/stdc++.h>
using namespace std;

int main(){
    // write code here

    string s;
    cin >> s;
    sort(s.begin(), s.end());
    cout << s << endl;
    return 0;
}
  • range based for loop
#include<bits/stdc++.h>
using namespace std;

int main(){
    // write code here
    string s;
    cin >> s;
    // for(int i=0; i<s.size(); i++){
    //     cout << s[i] << " ";
    // }
    // shortcut ->>> but it does not work with index 
    for(auto c : s){
        cout << c << " ";
    }
    return 0;
}
  • reverse function
5
1 2 3 4 5
string
reverse string
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;

    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    string s, s1;

    cin >> s;

    // FIX: consume leftover newline before getline
    cin.ignore();

    getline(cin, s1);

    reverse(s.begin(), s.end());
    reverse(s1.begin(), s1.end());
    reverse(a, a + n);

    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }

    cout << endl;
    cout << s << endl;
    cout << s1 << endl;

    return 0;
}
  • reverse word
i love you
#include<bits/stdc++.h>
using namespace std;

int main(){
    // write code here
    string s;
    getline(cin, s);
    stringstream ss(s);
    string word;
    ss >> word;
    cout << word;
    while(ss >> word){
        reverse(word.begin(), word.end());
        cout << " " << word;
    }
    return 0;
}
  • function inside class
#include<bits/stdc++.h>
using namespace std;
class Student 
{
    public:
    string name;
    int roll;
    int math;
    int physics;
    int chemistry;
    Student(string name, int roll, int math, int physics, int chemistry){
        this->name = name;
        this->roll = roll;
        this->math = math;
        this->physics = physics;
        this->chemistry = chemistry;
    }
    void hello(){
        cout << "Hello, I am a student." << endl;
        cout << "My name is " << name << " and my roll number is " << roll << "." << endl;
    }
    void total(){
        int total_marks = math + physics + chemistry;
        cout << "My Total Marks Are = " << total_marks << endl;
    }
};
int main(){
    // write code here
    Student s("John Doe", 101, 85, 90, 80);
    cout << "Name: " << s.name << endl;
    cout << "Roll: " << s.roll << endl;
    s.hello();
    s.total();
    return 0;
}
  • copy dynamic object
#include <bits/stdc++.h>
using namespace std;

class Cricketer
{
public:
    string country;
    int jersey;
    Cricketer(string country, int jersey)
    {
        this->country = country;
        this->jersey = jersey;
    }
};

int main()
{
    // write code here
    Cricketer *dhoni = new Cricketer("India", 100);
    Cricketer *kohli = new Cricketer("India", 18);
    kohli = dhoni;
    //delete dhoni; // this is called a memory leak because we have lost the reference to the memory allocated for kohli. we cannot delete kohli because it is already deleted by deleting dhoni. this is called a dangling pointer.
    cout << kohli->country << kohli->jersey << endl;

    return 0;
}
  • this will cause error because here the reference in pointed to the deleted dhoni
  • we can manage this using deep copy , i mean copy the value and store in the heap memory direfctly
#include <bits/stdc++.h>
using namespace std;

class Cricketer
{
public:
    string country;
    int jersey;
    Cricketer(string country, int jersey)
    {
        this->country = country;
        this->jersey = jersey;
    }
};

int main()
{
    // write code here
    Cricketer *dhoni = new Cricketer("India", 100);
    Cricketer *kohli = new Cricketer("India", 18);
    // kohli = dhoni;
    // //delete dhoni; // this is called a memory leak because we have lost the reference to the memory allocated for kohli. we cannot delete kohli because it is already deleted by deleting dhoni. this is called a dangling pointer.
    // cout << kohli->country << kohli->jersey << endl;

    //  lets copy in more effeicient way ---> for dynamic objects we have to copy the values of the object instead of copying the reference of the object. this is called a deep copy.
    kohli->country = dhoni->country;
    kohli->jersey = dhoni->jersey;
    delete dhoni; // this will not delete kohli because we have copied the values of dhoni to kohli. this is called a deep copy.
    cout << kohli->country << " " << kohli->jersey << endl;
    return 0;
}
  • we can optimize this more
#include <bits/stdc++.h>
using namespace std;

class Cricketer
{
public:
    string country;
    int jersey;
    Cricketer(string country, int jersey)
    {
        this->country = country;
        this->jersey = jersey;
    }
};

int main()
{
    // write code here
    Cricketer *dhoni = new Cricketer("India", 100);
    Cricketer *kohli = new Cricketer("India", 18);
    // kohli = dhoni;
    // //delete dhoni; // this is called a memory leak because we have lost the reference to the memory allocated for kohli. we cannot delete kohli because it is already deleted by deleting dhoni. this is called a dangling pointer.
    // cout << kohli->country << kohli->jersey << endl;

    //  lets copy in more effeicient way ---> for dynamic objects we have to copy the values of the object instead of copying the reference of the object. this is called a deep copy.
    // kohli->country = dhoni->country;
    // kohli->jersey = dhoni->jersey;

    // instead of these 

    *kohli = *dhoni; // this will copy the values of dhoni to kohli. this is called a deep copy.
    
    delete dhoni; // this will not delete kohli because we have copied the values of dhoni to kohli. this is called a deep copy.
    cout << kohli->country << " " << kohli->jersey << endl;
    return 0;
}
← Back to profile