C++ most important basic clear

*Tumi ekhon intermediate level e aso, eta sesh koire first to last sesh korba oi udemyr course ta

There are three ways to use scope operators ::

  1. namespace
  2. ENUM
  3. Class ( Amar onno blog e e bepare likha asey)

conditional compilation with preprocessor directive in C/C++, ifdef, undef, in header file:

//https://www.youtube.com/watch?v=J0nWQRqb3Fc
#include <iostream>
#define PI 3.14
//#define AREA
#define ZAKI 1
using namespace std;

int main()
{
#ifdef AREA
    cout<<"Hey Zaki"<<endl;
#else
    cout<<"Hey baybee"<<endl;
#endif

#ifdef ZAKI
cout<<"heyy Zaki"<<endl;
#endif

#undef ZAKI

#ifdef ZAKI
cout<<"heyy Zaki2";
#else
cout<<"Ki obostha"<<endl;
#endif

    return 0;
}

inline:

//ref: https://www.youtube.com/watch?v=TGwl3tJYFRg

#include <iostream>
using namespace std;

inline void display(int a){ //to save execution time we use inline, use in short function to increase performance
    cout<<a<<endl;
}

int main()
{
    display(10);
    return 0;
}

Pass vector to a function: https://www.geeksforgeeks.org/passing-vector-function-cpp/

Prototype vs Definition
Prameter vs Definition
Function prototype:
int add(int, int); //parameter

int main(){
int m=20, n = 30, sum;
sum=add(m,n); //argument
}

Function definition:
int add(int a, int b) //parameter
{

return a+b;

}

Function definition is block of code which is capable of doing some task

JSOn niye ei video ta vallagse:

My learning for c++ pointer

#include <iostream>

using namespace std;

void display(int *array, int sentinel){
    while(*array != sentinel){
        cout<<*array++<<" ";
    }
    cout<<endl;
}

int main()
{
    int scores [] {100, 98, 97, 79, 85, -1};
    display(scores,-1);

    return 0;
}
#include <iostream>

using namespace std;

void double_data(int *int_ptr_fetching){

    *int_ptr_fetching *= 2;

}

int main()
{
    int value {10};
    int *int_ptr{nullptr};

    std::cout<< "Value: " << value <<std::endl;
    double_data(&value);
    std::cout<< "Value: " << value <<std::endl;

    std::cout<<"----------"<<std::endl;
    int_ptr = &value;
    double_data(int_ptr);
    std::cout<< "Value: " << value <<std::endl;
    return 0;
}
#include<iostream>
#include<string>
#include<vector>

using namespace std;

void display(const vector<string> *const v){ //v is addressing the pointer //remember if i put here const we cannot change the value here
    (*v).at(0)="Funny";

    for(auto str: *v){ //here dereferencing the pointer
        cout<<str<<" ";
    }
    cout<<endl;

    v=nullptr;
}

int main()
{
    vector<string> stooges {"Larry", "Moe", "Curly"};
    display(&stooges);

    cout<<endl;
    return 0;
}

Throws

#include <iostream>

using namespace std;


void throws();
//void no_throws() noexcept;

void no_throws() noexcept{
    throw true;
}

int main()
{
    if(noexcept (throws())){
        cout<<"No exceptions"<<endl;
    }
    if(!noexcept (throws())){
        cout<<"exceptions"<<endl;
    }
  no_throws();
    return 0;
}

this

#include <iostream>
// https://www.youtube.com/watch?v=uVsoUyDO8Ow
using namespace std;


class Person{
private:
    int age;
public:
    void setAge(int age){
        this->age = age;
    }

    void showAge(){
        cout<<this->age<<endl;
    }

};

int main()
{

    Person zaki;
    zaki.setAge(29);
    zaki.showAge();
    cout << "Hello World!" << endl;
    return 0;
}

Enum:

//https://www.youtube.com/watch?v=_Dp66kZ8u6E
//readable enum: https://www.youtube.com/watch?v=oW5UusoWEY0
#include <iostream>

using namespace std;

enum Suit{
    Uno,
    Spade=5,
    Club,
    Heart,
    Diamond,
};


int main()
{

    Suit myCard;
    Suit hisCard;

    myCard = Suit::Uno;
    hisCard = Suit::Club;

    if(myCard < hisCard){
        cout<<"You win!"<<endl;
    }else{
        cout<<"Not win!"<<endl;
    }

    //cout <<myCard<< endl;
    return 0;
}

Static convert

#include <iostream>

using namespace std;

enum Scale{
    SINGLE=1,
    DOUBLE =2,
    QUAD =4

};


int main()
{
    //Scale s = 1;
    Scale s = static_cast<Scale>(2);
    std::cout<<s<<std::endl;
    return 0;
}

Scope resolution

//scope resolution https://www.youtube.com/watch?v=-lcWu8bD_Y0
#include <iostream>

using namespace std;
int x=10;

//class Box{

//public:
//    double length;
//    double breadth;
//    double height;

//    double getVolume(void){
//        return length*breadth*height;
//    }
//};

//double Box::getVolume(void){
//    return length*breadth*height;
//}
void display(){
    std::cout<<"Inside the display function "<<x<<std::endl;
}

int main()
{
    //int x=50;
    int x=10;
       ::x=30;
    std::cout<<"Inside the main function without scope resolution calling "<<x<<std::endl;

    std::cout<<"Inside the main function "<<::x<<std::endl;

    display();
    return 0;
}

namespace:

//ref: https://www.educba.com/c-plus-plus-namespace/?source=leftnav 
#include <iostream>
using namespace  std;

namespace first{
    int myvar=5;

    namespace second
    {
        namespace third
        {
         int myvar1=myvar;
        }
    }

}

namespace myalias=first::second::third;
namespace demo{
int gvar=300;
}
using namespace demo;

int main()
{
    cout<<"Value of demo="<<demo::gvar<<endl;
    cout<<"Value of nested namespace third "<<myalias::myvar1<<endl;
    return 0;
}

scope resol class

//https://www.youtube.com/watch?v=59fy7la7yEI
#include <iostream>

using namespace std;

class Human{
public:
    string name;
    void introduce();

};

void Human::introduce(){
    std::cout<<Human::name<<std::endl;
}

int main()
{
    Human zaki_object;
    zaki_object.name="Zaki";
    zaki_object.introduce();
    return 0;
}

scope resolution global variable:

//ref: https://www.youtube.com/watch?v=9bO8EeB6IHM
#include <iostream>

using namespace std;

int a=100; //global variable

int main()
{
    int a=50;
    {
        int a=5; //local variable
        cout<<"inner block"<<endl;
        cout<<"a="<<a<<endl;
        cout<<"global a="<<::a<<endl;
    }
    cout << "We are outer block" << endl;
    cout<<"a="<<a<<endl;
    cout<<"global a="<<::a<<endl;

    return 0;
}

Override

#include <iostream>
using namespace std;
//https://www.youtube.com/watch?v=_wqhcEegmN4 perfect learning from here

class Base {
    int b_var;
public:
    virtual void fun(){
        std::cout<<"Base fun"<<std::endl;
    }
};

class Derived: public Base{
    int d_var;
public:
    void fun() override{ //to make testing easier for other
        std::cout<<"Derived fun"<<std::endl;
    }
};

int main()
{
    Base *b = new Derived();
    b->fun();

    return 0;
}

arrow

#include <iostream>

using namespace std;

class Entity{

public:
    int x;

public:
    void Print() const{
        std::cout<<"Hello"<<std::endl;
    }
};

class ScopedPtr{


};

int main()
{
    Entity e;
    e.Print();

    Entity *ptr=&e;
    ptr->Print(); // here -> it is dereferencing
    return 0;
}

#pragma once

//ref: https://www.youtube.com/watch?v=EIOQIP58ieg
#pragma once
//benfit
less code
compilation speed
avoidance of name clashes
same as like ifndef
 
"grandparent.h"
#pragma once

struct test{

	int member;
};

"parent.h"
#include "grandparent.h"

"child.cpp"
#include "parent.h"
#include "parent.h"


#ifndef _GRANPARENT_H
#define _GRANPARENT_H
...contents of grandparent.h
#endif

return multiple values:


c++17 if else initilaizer:

//c++11 tuple cppnuts return multiple values
//https://www.youtube.com/watch?v=mjnsj0QxEN0
#include<iostream>
#include<tuple>
using namespace std;

int main(){
    std::tuple<int, std::string, char> t(1,"ZakiLive", 'a');
    cout<<get<1>(t)<<endl;
    return 0;
}
//ref: https://www.youtube.com/watch?v=tbDlNSjDLeM
#include<iostream>
using namespace std;

//int x = min(5,10);
//if(x==5){
//    Job;
//}

//c++11 features
int main(){
    if(int x = min(5,10); x==5){
        cout<<"job"<<endl;
    }

    return 0;
}

constexpr

//ref: https://www.youtube.com/watch?v=MGsSDSa6uSQ
//c++17 features
#include<iostream>
#include<string>
#include<type_traits>
using namespace std;

template <typename T>
auto length_find(T const& value){

    if constexpr(is_integral<T>::value){
        return value;
    }
    else{
        return value.length(); //after putting constexpr it becoe compile time without it is runtime
    }
}

auto length_find(string const& value){
    return value.length();
}

int main(){
    int n{10};
    string s{"abc"};

    cout<<"n="<<n<<"and length"<<length_find(n)<<endl;
    cout<<"s="<<s<<"and length"<<length_find(s)<<endl;

    return 0;
}

another example:

//https://www.youtube.com/watch?v=frifFlPO_uI
#include<iostream>
using namespace std;

constexpr int add(int a, int b){
    return a+b;
}

int main(){
    //example 2 - runtime
    int a,b;
    cin>>a>>b;
    cout<<add(a,b)<<endl;

    //example 1 - compile time
    cout<<add(3,4)<<endl;
    return 0;
}

auto keyword:

#include<iostream> // https://www.youtube.com/watch?v=nBKwKsj-GpI
#include<typeinfo>
using namespace std;

class Base{};

int main(){

    auto x=20;
    auto y=20.5;
    auto b = new Base();

    cout<<typeid(x).name()<<endl;
    cout<<typeid(b).name()<<endl;


return 0;
}

Assign object to int:

#include<iostream> // https://www.youtube.com/watch?v=gcjSTy7IBuc
#include<typeinfo>
using namespace std;

class Base{
    int var;

public:
    Base(){}
    Base(int val): var{val}{}
    operator int() const{
        return var;
    }

};

int main(){
   Base b(45454);
   int tmp=b;
   cout<<tmp<<endl;

   return 0;
}

Name mangling: https://www.youtube.com/watch?v=FUIle4Ghasw

How vector works: https://www.youtube.com/watch?v=OdPiF_K2miw

vector why bad sometime?

it uses array behind

#include<iostream> // https://www.youtube.com/watch?v=BZv_kQkCJzg
#include<vector>
using namespace std;

int main(){
    vector<int> vec;
    std::cout<<"size:"<<vec.size()<<std::endl;
    cout<<"cap:"<<vec.capacity()<<endl;
    vec.push_back(4);
    vec.push_back(4);
    vec.push_back(4);
    cout<<"size:"<<vec.size()<<endl;
    cout<<"capacity:"<<vec.capacity()<<endl;

}

Some C++ concept clear:

stop someone taking address of your object

//someone cannot take address of your object
#include<iostream>
using namespace std;

class Base{
    int x;
public:
    Base(){}
    Base(int x): x{x} {}
//    Base* operator &(){
//        //cout<<"Bingo"<<endl;
//        //return this;
//    }
    Base* operator & () = delete;
};


int main(){
    Base b;
   // Base *bp=&b;
    //cout<<bp<<endl;

   // cout<<bp<<endl;

    return 0;
}

alternative operator https://www.youtube.com/watch?v=HT_s8A5BIss

#include<iostream>
using namespace std;


int main(){
    if(1 or 1)
        std::cout<<"inice"<<std::endl;

    return 0;
}

Friend function and Friend class:

//use of friend function and classes in c++ https://www.youtube.com/watch?v=QLdeKM7UVq0
#include<iostream>
using namespace std;

class Base{
    int x;
public:
    Base(){};
    Base(int x):x{x} {}
    friend void func(Base &); //it is defining who is friend of base
};

void func(Base &obj){
    cout<<obj.x<<endl;
    obj.x=20;
    cout<<obj.x<<endl;
}

int main(){
    Base b(10);
    func(b);
    return 0;
}
//use of friend function and classes in c++ https://www.youtube.com/watch?v=VQxEyukjYfc
//valo bujhi nai
#include<iostream>
using namespace std;
class Base1;

class Base2{
    int y;
public:
    Base2(int y=0):y{y} {}
    void print (Base1 &b1);
};

class Base1{
    int x;
public:
    Base1(){};
    Base1(int x=0):x{x} {}
    friend void Base2::print(Base1 &);
};

void Base2::print(Base1& b1){
    cout<<"x: "<<b1.x<<endl;
}

int main(){
    Base1 b1(10);
    Base2 b2(20);

    return 0;
}

const and dest both:

//https://www.youtube.com/watch?v=EbqjycJza5I
//dest and const explicitely
#include<iostream>
using namespace std;

class Base{
    int _baseVariable;
public:
    Base(){std::cout<<"base constr"<<std::endl;}
    ~Base(){std::cout<<"base destructor"<<std::endl;}
};

int main(){
    Base obj;

    //obj.~Base(); //you should not do like this , it is dangerous for application
    //Base();
    cout<<"Something"<<endl;

    return 0;
}

assert()

it is some of decision. Based on this decision code can be proceed.

#include<iostream>
#include<cassert>
using namespace std;

void display_number(int* p){
    assert(p!=NULL);
    std::cout<<*p<<std::endl;
}

int main(){
    int a = 10;
    display_number(&a);

    int *k=NULL;
    display_number(k);

    return 0;
}

runtime vs compile time:

Binding meaning: Binding means mapping of one thign to antoher.
Ref: https://www.youtube.com/watch?v=ZAB60DNUTbE

compile time /compile time polymorphism / early binding /static binding:

Function overloading: //the parameter of the function should be different but function name should be same

#include<iostream>
#include<cassert>
using namespace std;

class Test{
public:
    void fun(int x)
    {
        cout<<"Integer"<<endl;
    }
    void fun(double x){
        cout<<"Double"<<endl;
    }

};

int main(){
    Test t1;
    t1.fun(10);

    t1.fun(10.5);

    return 0;
}

Operator overloading:

//ref: https://www.youtube.com/watch?v=mv5_l4kuVho
#include<iostream>
using namespace std;

class Complex {
private:
    int real, image;
public:
    Complex(int r=0, int i=0): real{r}, image{i} {}
    Complex operator + (Complex const &obj)
    {
        Complex res;
        res.real=real+obj.real;
        res.image=real+obj.image;
        return res;
    }

    void show(){
        cout<<real<<"+i"<<image<<endl;
    }

};


int main(){
    Complex c1(1,3), c2(2,5);
    Complex c3=c1+c2;
    c3.show();
    return 0;
}

runtime/dynamic binding/ lazy / late binding: It only exceutes with virtual functions override

//https://www.youtube.com/watch?v=mv5_l4kuVho
#include<iostream>
using namespace std;

//base class is abstract class, when it has virtual keyword it become like that
class Base{
public:
    virtual void fun(int y){
        cout<<"Base"<<endl;
    }
   // ~Base();
};

class Derived: public Base{
public:
    virtual void fun(int x) override{
        cout<<"Derived"<<endl;
    }
 //   ~Derived();
};


int main(){
//    Base *b=new Derived();
//    b->fun(5);
    Derived d;
    Base &b=d;
    b.fun(10);
    return 0;
}

Virtual dest:

//ref: https://www.youtube.com/watch?v=kDHiOLC5sQo&list=PLk6CEY9XxSICC720VHmI-N-fqlEF7XwG_&index=2
#include<iostream>
using namespace std;

class Base{
public:
    Base(){cout<<"ctor base"<<endl;}
    virtual ~Base(){cout<<"dtor base"<<endl;}
};

class Derived: public Base{
public:
    Derived(){cout<<"ctor derived"<<endl;
    }
    ~Derived(){cout<<"dtor derived"<<endl;}
};

int main(){
    Base *b = new Derived();
    delete b;

    Derived d;
    cout<<sizeof(d)<<endl;
    return 0;
}

//pure virtual function:

//pure virtual function ref:https://www.youtube.com/watch?v=y8LoET4oSKc
#include<iostream>
using namespace std;

class Animal{
public:
    virtual void move()=0;
};


void Animal::move(){ //body of pure virtual function
    cout<<"Hey"<<endl;
}

class Cow: public Animal{
public:
    void move(){
        Animal::move();
        cout<<"I can walk and run"<<endl;
    }
};


class Snake: public Animal{
public:
    void move(){
        cout<<"I do loco"<<endl;
    }

};
int main(){
    Animal *a;
    Cow c;
    c.move();
    return 0;
}

//pure virtual function:

//ref: https://www.youtube.com/watch?v=SvesRBYu65k&t=256s
#include <iostream>
using namespace std;

class Database{
public:
    virtual void getName()=0;
};

class Accountant : public Database{
public:
    void getName(){
        cout<<"This is account class"<<endl;
    }
};
class Manager : public Database{
    //we cannot create object of abstract class
    //but we can create pointer of abstract class
public:
    void getName(){

        cout<<"This is manager class"<<endl;
    }
};

class Customer : public Database{
public:
    void getName(){
        cout<<"This is customer class"<<endl;
    }
};

int main()
{
    Database *ptr;
    Manager m;
    Accountant a;
    Customer c;

    ptr=&m;
    ptr->getName();

    m.getName();
    a.getName();
    c.getName();

    return 0;
}


Forward declaration:

//example with forward declaration
//why we need
//ref:https://www.geeksforgeeks.org/what-are-forward-declarations-in-c/
//https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c

#include<iostream>
using namespace std;
//Forward declaration
class A;
class B;


class B{
    int x;

public:
    void getdata(int n)
    {
        x=n;
    }
    friend int sum(A, B);
    
};

class A{
    int y;
public:
    void getdata(int m)
    {
        y=m;
    }
    
    friend int sum(A,B);
};


int sum(A m,B n){
    int result;
    result=m.y+n.x;
    return result;
}

int main(){
    B b;
    A a;
    a.getdata(5);
    b.getdata(4);
    return 0;
}

Friend function:

//https://www.youtube.com/watch?v=mD5f3-30HEc
//bets mone hoise
#include<iostream>
using namespace std;

class Base{
protected:
    int x;
public:
    Base(){};
    Base(int a):x{a} {}
    friend void set(Base &, int);
    friend int get(Base&);
};


void set(Base &obj, int x){
    obj.x=x;
}

int get(Base &obj)
{
    return obj.x;
}

int main(){
    Base obj(10);
    cout<<get(obj)<<endl;
    set(obj,20);
    cout<<get(obj)<<endl;
    return 0;
}

atoi() function : ascii to integer

//https://www.youtube.com/watch?v=QyDE7cPycnU#include<iostream>
using namespace std;

int myAtoi(char *str){
    int res=0;
    int sign=1;
    int i=0;
    
    //if number is negative, then update sign
    if(str[0]=='-'){
        sign=-1;
        i++;
    }
    
    for(; str[i]!= '\0'; ++i)
        res=res*110+str[i]-'0';
    
    return sign*res;
}

int main(){
//    Base *b=new Derived();
//    b->fun(5;
    char str1[] = "-1234";
    int val = myAtoi(str1);
    cout<<val<<endl;
    
    return 0;
}

Memory leak is dangerous ? https://www.youtube.com/watch?v=pAaxuVTLnRU
https://www.youtube.com/watch?v=F2nrej6Kjww

dangling pointer()

//ref: https://www.youtube.com/watch?v=SK7hNNXPgio
#include<iostream>
using namespace std;

void func(int *ptr){
    cout<<*ptr<<endl;
    delete ptr; //dangling pointer
}

int main(){
    int *p1=new int(100);
    int *p2=p1;
    func(p2);
//    cout<<*p1<<endl; //delete hoye jawar pore memory te oita ar nai
}

static_cast

//https://www.youtube.com/watch?v=HlNVgmvX1EI
#include<iostream>
using namespace std;

class Int{
    int x;

public:
    Int(int x=0):x{x}{
        cout<<"conversion constructor"<<endl;
    }
    operator string(){
        cout<<"conversion operator"<<endl;
        return to_string(x);
    }
};

int main(){

    Int obj(3);
    string str1=obj;
    obj=20;
    string str2=static_cast<string>(obj); //compile time casting
    obj=static_cast<Int>(30);

//    float f=3.5;
//    int a;
//    a=f;
//    a=static_cast<int>(f);


    return 0;
}

comma operator

//ref:https://www.youtube.com/watch?v=116NUKfvbgY
#include<iostream>
using namespace std;

int fun()
{
    return (1,2,3);
}
int main(){
    int v1, v2;
    v1=1,2,3;
    v2=(1,2,3);
    cout<<fun()<<endl;
    return 0;
}

Shallow copy, Deep copy (pore dekhte hobe: https://www.youtube.com/watch?v=TjN_5rGMpfg

lvalue and rvalue>

//rvalue
#include<iostream>
using namespace std;

int square(int x){
    return x*x;
}

int main(){

    int x=10;
    int a=2, b=3;

    int *p=square(10);
    
    return 0;
}
//lvalue : https://www.youtube.com/watch?v=EElsUaoWxIo
#include<iostream>
using namespace std;

int square(int x){
    return x*x;
}

int main(){
    int x=10;
    int &rx=x;
    int && rr = 20;

    const int &p=20;

    cout<<rr<<endl;
    cout<<rx<<endl;
    return 0;
}

Segmentation fault:

//segmentation fault  https://www.youtube.com/watch?v=bfWxAG1vUM4

//stack overflow
//write violation
//read violation
//many more memory related error

#include<iostream>
using namespace std;

int main(){
	int *i=NULL;
	*i=10;
	cout<<*i<<endl;


}


int main(){

	main();
	return 0;
}

Could not run successfully: pair in c++ STL

//https://www.youtube.com/watch?v=Puaw04YmtrI
#include <iostream>
#include<vector>
using namespace std;

void print(std::pair<int,int> &obj){
    cout<<obj.first<<" "<<obj.second<<endl;
}

int main()
{

    std::pair<int, int> obj(10,20);
    print(obj);

//    std::pair<int, int> obj = std::make_pair(10,20);
//    print(obj);

    std::vector<std::pair<std::string, int>> list;
    list.push_back(make_pair("Rupesh", 30));
    list.push_back(make_pair("Hitesh", 28));
    list.push_back(std::pair<std::string, int>("Bhupendra",22));
    list.push_back(std::pair("Sagar", 18));
    for(auro &elm:list){
        cout<<elm.first<<" "<<elm.second<<endl;
    }
    return 0;
}

map in c++: https://www.youtube.com/watch?v=nPSDR5nZzHA&list=PLk6CEY9XxSIA-xo3HRYC3M0Aitzdut7AA&index=8 (pore aro bujha lagbe)

Nested namespace:

//ref: https://www.youtube.com/watch?v=-CTPds55l2U
#include<iostream>
using namespace std;

//nested namespace before c++17
namespace Vehicle
{
    namespace Car
    {
    namespace Engine
    {
    class PetrolEngine{

    };
    }

    }
}

//nested namespace in c++17
namespace Vehicle::Car::Engine{
    class PetrolEnginer{};
}


int main(){




}

Need to watch:

https://www.youtube.com/watch?v=70qXSmuqB2I&t=2s

Memory leak:

//Simple betting game
//"Jack Queen King" -computer shuffles these cards
//player has to guess the position of queen
//if he wins, he takes 3*bet
//if he looses, he looses the bet amount.
// player has 100 usd initially

#include<iostream>
using namespace std;

int cash = 100;
int main(){
    int bet;
    while(cash>0){
        cout<
    }
}

Final:https://www.youtube.com/watch?v=WObyOa2FXwI

Const: https://www.youtube.com/watch?v=n9ryobxgtx0

Header: https://www.youtube.com/watch?v=qaGzc56Rekg&t=375s

C++ Object Slicing:

//ref: https://www.youtube.com/watch?v=ezgq_fShJw4
//https://www.youtube.com/watch?v=f29xDhRNPfU
#include <iostream>
using namespace std;

class Base{
public:
    int b_var;
    Base(){
        std::cout<<"base cons"<<std::endl;
    }
    ~Base(){
        std::cout<<"base dest"<<std::endl;
    }
};

class Derived: public Base{
public:
    int d_var;
    Derived(){
     std::cout<<"der cons"<<std::endl;
    }
    ~Derived(){
        std::cout<<"der cons"<<std::endl;
    }
};

int main()
{
    Derived d_obj;

    Base b_obj=d_obj;
    return 0;
}

Memory save with new:

//ref: https://www.youtube.com/watch?v=2bsGFQgBMXs
#include <iostream>

using namespace std;
class Base{
    int a;
public:
    Base(){

    }
    ~Base(){

    }

};

int main()
{
    //Normal case:
    Base *obj=new Base();
    delete obj;

//placement new case:
    std::cout<<"placement new case"<<std::endl;
    char *memory=new char[10*sizeof(Base)];
    Base *obj1=new (&memory[0])Base();
    Base *obj2=new (&memory[4])Base();

    obj1->~Base();
    obj2->~Base();


    return 0;
}

This:

//ref: https://www.youtube.com/watch?v=WFqqX0MMn-w
#include <iostream>
using namespace std;

class ClassName{
public:
    int VarName;

    void OutputVar(int VarName){
        this->VarName=5;

        cout<<VarName<<endl;
        cout<<this->VarName<<endl;
    }
};

int main()
{
    ClassName Object;
    Object.OutputVar(20);


    return 0;
}

From w3schools:

Exception handling niye halka ghataghati:

#include <iostream>
using namespace std;

int main()
{
    try {
        int age=15;
        if(age >= 18) {
            cout<<"Access granted- you are old enough";
        }
        else {
            throw 505;
        }
    }
    catch(int myNum) {
        cout<<"Access denied-you must be at least 18 years old.\n";
        //cout<<"Age is: "<<myNum;
        cout<<"Error number: "<<myNum;
    }
    
    return 0;
}

Access specifier:

#include <iostream>
using namespace std;

//Base class
class Employee{
    protected:
        int salary;
};

//Derived class
class Programmer: public Employee{
public:
    int bonus;
    void setSalary(int s){
        salary = s;
    }

    int getSalary(){
        return salary;
    }
};



int main()
{

    Programmer obj1;
    obj1.setSalary(50000);
    obj1.bonus=15000;

    cout<<"Salary:"<<obj1.getSalary()<<"\n";
    cout<<"Bonus:"<<obj1.bonus<<"\n";

    return 0;
}

Shallow Copy Deep Copy:

Ref: Shallow and Deep Copy in C++ – YouTube

#include <iostream>

using namespace std;

class Demo1{
    int data1, data2, *p;

public:
    //constructor
    Demo1(){
        p=new int;
    }
    //copy constructor
    Demo1(Demo1 &d){
        data1=d.data1;
        data2=d.data2;
        p=new int;
        *p=*(d.p);
        *p=50;
    }


void getData(int a, int b, int c){
    data1=a;
    data2=b;
    *p=c;
}

void showData(){
    cout<<"data1="<<data1<<" data2="<<data2<<" *p="<<*p<<endl;
}
};
int main()
{
    Demo1 obj1;
    obj1.getData(10,20,30);
    obj1.showData();
    Demo1 obj2=obj1;
    obj2.showData();
    return 0;
}

Copy Constructor

Pass by Value, pass by reference

Operator overloading

part 1: C++ Important Knowledge Gathered All Together – Syed Ahmed Zaki (zakilive.com)

Pure Virtual function:

https://www.youtube.com/watch?v=y8LoET4oSKc

Virtual Destructor:

https://www.youtube.com/watch?v=kDHiOLC5sQo

Rule of three:

https://www.youtube.com/watch?v=RspbK8tfU0Y

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *