Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
NameTempSpecial
//g++ 7.4.0 //NameTempSpecial: example of Namespace and template specialization //this code is created by Rezaul Hoque on May 12,2022; //contact:jewelmrh@yahoo.com;Dhaka,Bangladesh;https://rezaulhoque.wordpress.com,https://hoquestake.blogspot.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> #include <string> namespace intro{ template<class X> class Deco{ public: void f1(){std::cout<<"---------";} void f2(){std::cout<<"This is a demo";} }; template<> class Deco<std::string>;//forward declare specialization for string } template <> class intro::Deco<std::string>{ public: void f1(){ std::cout<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<"\n"; std::cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<"\n"; } void f2(){std::cout<<"Namespace & Template Specialization Example \n";} }; namespace NamAd{ template <class X> class Name { X n; public: Name(){} Name(X); Name(Name&); ~Name(){} X getName(); template <class V>//member template class Ad{ V ad; public: Ad(){} Ad(V); ~Ad(){} V getAd(){ return ad;} }; }; template<>//for the specialized Name template <>//for the specialized Ad std::string Name<std::string>::Ad<std::string>::getAd(){std::cout<<"Address of a specialized template.\n"; return ad;} //definition of Name(X) constructor declared earlier template<class X> Name<X>::Name(X a){n=a;} //definition of Name (Name&) copy constructor declared earlier template<class X> Name<X>::Name(Name& c){n=c.n;} //definition of getName() function declared earlier template<class X> X Name<X>::getName(){ return n;} //definition of Ad(V) constructor declared earlier template <class X> template <class V> Name<X>::Ad<V>::Ad(V a){ad=a;} } int main() { intro::Deco<std::string> a; a.f1(); a.f2(); a.f1(); NamAd::Name<std::string> naam("AAA"); NamAd::Name<std::string>::Ad<std::string> bari("Nowhere "); std::cout<<naam.getName()<<" \n"; std::cout<<bari.getAd(); return 0; }
run
|
edit
|
history
|
help
0
Empty C++
c++ mouse
Hello
Splitwise Problem - 2
Microsoft - MaxEmployeeAttendence (R repititions - Optimised DP)
Making pyramid using nested loop 2/2
macros
temp
CPP - ex 5 - solution
code