Deep and Shallow Constructor Date: 05/09/2026
// Online C++ compiler to run C++ program online
#include <iostream>
#include<string>
using namespace std;
class Student{
public:
int age;
Student(int a){
age=a;
}
Student(Student &ob){
age=ob.age;
}
};
class Shallow{
public:
int *age;
Shallow(int a){
age=new int(a);
}
};
class Deep{
public:
int *age;
Deep(int a){
age=new int(a);
}
Deep(const Deep &o){
age=new int(*o.age);
}
};
template<typename T>
void display(T a){
cout<<"The value : "<<a<<endl;
}
template<>
void display<string>(string a){
cout<<"The string is : "<<"\""<<a<<"\""<<endl;
}
template<typename S>
class Test{
public:
void display(S a){
cout<<"General : "<<a<<endl;
}
};
template<>
class Test<string>{
public:
void display(string a){
cout<<"String : "<<a<<endl;
}
};
int main() {
// Write C++ code here
// std::cout << "Start small. Ship something.";
// Student s(22);
// Student s1=s;
// // s1.age=34;
// cout<<s1.age<<endl;
// cout<<s.age<<endl;
// Shallow s1(20);
// Shallow s2=s1;
// // *s2.age=35;
// cout<<*s1.age<<endl;
// cout<<*s2.age<<endl;
// Deep d(27);
// // Deep d1=d; // or Deeo d1(d);
// *d1.age=23;
// cout<<*d.age<<endl;
// cout<<*d1.age<<endl;
display(12);
display(12.34f);
display(string("Gunjan Nayak"));
Test<int> o1;
Test<string> o2;
o1.display(12);
o2.display(string("Gunjan Nayak"));
return 0;
}
Comments
Post a Comment