C++ Classess Part -2 (with templates) Date:- 29/08/2026

// Online C++ compiler to run C++ program online

#include <iostream>

#include<string>

using namespace std;


class A{

    protected:

    int x=1;

    public:

    void dis(int a){

        x=a;

        cout<<"The value of x is "<<x<<endl;

    }

};


class B:protected A{

    public:

    void d(int a){

        x=a;

        cout<<x<<endl;

    }

};


class parent{

public:

void dis(){

    cout<<"Display Function"<<endl;

}

};


class BB:public parent{

    public:

    void res(){

    cout<<"Rest Now!!"<<endl;

    }

};

class C:public BB{};


class AA{

    public:

    void dis(){

    cout<<"This is a display function"<<endl;

    }

};

class BBC{

public:

void res(){

    cout<<"Rest Now!!"<<endl;

}

};


class D:public BBC,public AA{};


class E:public BBC{};

class F:public AA{};


class Student{

public:

Student(string n){

cout<<"Constructor!!"<<endl;

cout<<n<<endl;

}

Student(int x){

cout<<x<<endl;

}

};


class stdd{

public:

int x;

stdd(int n){

x=n;

}

stdd(const stdd& obj){

x=obj.x;

}

};


// template <typename t>

template <typename t,typename u>

// void bruh(t a,t b){

void bruh(t a,u b){

cout<<a+b<<endl;

}


template <typename v>


class Blob{

private:

v a;

public:

Blob(v x){

a=x;

cout<<"The value of a is "<<a<<endl;

}

};


template<typename f>

class Block{

public:

int a;

void dis(f x){

a=x;

cout<<a<<endl;

}


};



template<typename n>

class Rock{

public:


void dis(n t){

cout<<"The value of t is "<<t<<endl;

}


};


template<>

class Rock<string>{

    public:

    void display(string val){

        cout<<"The string is : "<<val<<endl;

    }

};


template<typename fd>

void addd(fd s){

    cout<<"The value of s is "<<s<<endl;

}


template<>

void addd<string>(string n){

    cout<<"The value of string is : "<<"\""<<n<<"\""<<endl;

}


int main() {

    // Write C++ code here

    B a;

    // cout<<a.x<<endl;

    a.d(12);

    C b;

    BB c;

    b.dis();

    c.dis();

    D aa;

    aa.res();

    aa.dis();

    // a.dis(12);  

    F f;

    f.dis();

    Student s("Gunjan Nayak");

    Student s1(27);

    stdd ss(23);

    stdd ss1=ss;

    cout<<ss1.x<<endl;

    bruh(12,12);

    // bruh("Hi","Their");

    bruh(12,12.24);

    // Blob<int> desk(12); // we can also explicitly define the datatype for template

    Blob<int> desk(12);

    Blob desk1("Gunjan Nayak");

    Blob<double> desk2(3.1345);

    Block<int> blk; // Here we have explicitly declared the datatype which should be used

    // blk.dis("12");

    Rock<int> r; //explicitly calls the int object 

    r.dis(12);

    Rock<string> ns; //specialized class template for string

    ns.display("Hello");


    addd(12);

    addd(3.1456);

    addd<string>("Gunjan Nayak");





    return 0;

} 

Comments

Popular posts from this blog

Requests and Urllib

Strings in C Language

Dictionary in Python