Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language C++ . At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight you profile to the recruiters.
In this post, you will find the solution for C++ Class Templates in C++-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To C++
C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language.
C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same.
C++ programming language was developed in 1980 by Bjarne Stroustrup at bell laboratories of AT&T (American Telephone & Telegraph), located in U.S.A. Bjarne Stroustrup is known as the founder of C++ language.
C++ Class Templates in C++ - Hackerrank Solution
Objective
A class template provides a specification for generating classes based on parameters. Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments. Here is an example of a class, MyTemplate, that can store one element of any type and that has just one member function divideBy2, which divides its value by 2.
It is also possible to define a different implementation of a template for a specific type. This is called Template Specialization. For the template given above, we find that a different implementation for type char will be more useful, so we write a function printElement to print the char element:
You are given a main() function which takes a set of inputs. The type of input governs the kind of operation to be performed, i.e. concatenation for strings and addition for int or float. You need to write the class template AddElements which has a function add() for giving the sum of int or float elements. You also need to write a template specialization for the type string with a function concatenate() to concatenate the second string to the first string.
Input Format :
The first line contains an integer n. Input will consist of n+1 lines where n is the number given in the first line of the input followed by n lines.
Each of the next n line contains the type of the elements provided and depending on the type, either two strings or two integers or two floating point numbers will be given. The type will be one of int, float or string types only. Out of the following two elements, you have to concatenate or add the second element to the first element.
Constraints :
- 1 <= n <= 5*10^5
- 1.0 <= value float <= 10.0, where value float is any float value.
- 1 <= value int <= 10^5, where value int is any int value.
- 0 <= len string <= 10, where len string is the length of any string.
The time limit for this challenge is 4 seconds.
Output Format :
The code provided in the code editor will use your class template to add/append elements and give the output.
Sample Input :
3 string John Doe int 1 2 float 4.0 1.5
Sample Output :
JohnDoe 3 5.5
Explanation :
“Doe” when appended with “John” gives “JohnDoe”. 2 added to 1 gives 3, and 1.5 added to 4.0 gives 5.5.
C++ Class Templates in C++ – Hacker Rank Solution
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <cassert> using namespace std; /* C++ Class Templates in C++ - Hacker Rank Solution START */ // class template: template <class T> class AddElements { T element; public: AddElements (T arg) {element=arg;} T add (T x) {return x+element;} }; // class template specialization: template <> class AddElements <string> { string element; public: AddElements (string arg) {element=arg;} string concatenate (string arg) { string s = element+arg; return s; } }; /* C++ Class Templates in C++ - Hacker Rank Solution END */ int main () { int n,i; cin >> n; for(i=0;i<n;i++) { string type; cin >> type; if(type=="float") { double element1,element2; cin >> element1 >> element2; AddElements<double> myfloat (element1); cout << myfloat.add(element2) << endl; } else if(type == "int") { int element1, element2; cin >> element1 >> element2; AddElements<int> myint (element1); cout << myint.add(element2) << endl; } else if(type == "string") { string element1, element2; cin >> element1 >> element2; AddElements<string> mystring (element1); cout << mystring.concatenate(element2) << endl; } } return 0; }
I like this web blog very much so much good information.
I like what you guys are up also. Such clever work and reporting! Keep up the excellent works guys I have incorporated you guys to my blogroll. I think it will improve the value of my website :).
Wow Look At Amazin News Website Daily Worldwide [url=https://sepornews.xyz]Best News Website[/url]
Having read this I thought it was very informative. I appreciate you taking the time and effort to put this article together. I once again find myself spending way to much time both reading and commenting. But so what, it was still worth it!
I like what you guys are up too. Such clever work and reporting! Keep up the superb works guys I’ve incorporated you guys to my blogroll. I think it’ll improve the value of my website :).
Hey there, You’ve done an excellent job. I’ll definitely digg it and personally recommend to my friends. I’m confident they’ll be benefited from this web site.
Thank you, I have just been searching for information approximately this topic for a while and yours is the best I have found out so far. But, what in regards to the conclusion? Are you sure concerning the source?
Hey would you mind stating which blog platform you’re working with? I’m planning to start my own blog in the near future but I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something unique. P.S Sorry for getting off-topic but I had to ask!
hi!,I like your writing so much! share we communicate more about your post on AOL? I need an expert on this area to solve my problem. May be that’s you! Looking forward to see you.
Can I just say what a relief to find someone who actually knows what theyre talking about on the internet. You definitely know how to bring an issue to light and make it important. More people need to read this and understand this side of the story. I cant believe youre not more popular because you definitely have the gift.
Excellent weblog right here! Additionally your website so much up fast! What web host are you using? Can I get your associate link for your host? I desire my site loaded up as fast as yours lol
cialis next day order tadalafil 5mg online cheap buy generic ed pills online
I really enjoy examining on this website , it has got great articles.
order cefadroxil 250mg online cheap where can i buy duricef propecia 1mg over the counter
fluconazole 100mg usa cipro canada order cipro 500mg pill
metronidazole for sale cephalexin 125mg generic keflex 250mg oral
order avana 100mg generic order avanafil 100mg online diclofenac 100mg ca
buy cheap clindamycin order generic cleocin 150mg fildena sale
tamoxifen 20mg ca nolvadex 10mg us order cefuroxime 500mg pills
buy careprost generic robaxin 500mg oral buy trazodone 100mg without prescription
order catapres 0.1 mg without prescription order antivert 25 mg online cheap order tiotropium bromide 9mcg generic
suhagra 50mg tablet order sildenafil 100mg order sildalis generic
cheap isotretinoin 10mg zithromax pills generic zithromax 250mg
lasix 100mg price order generic doxycycline oral albuterol
purchase vardenafil online purchase levitra cheap hydroxychloroquine 400mg
Hi there! I know this is kinda off topic but I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My website addresses a lot of the same subjects as yours and I believe we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Terrific blog by the way!
purchase benicar generic calan 240mg cost buy divalproex
oral coreg 6.25mg buy cenforce sale buy aralen 250mg
acetazolamide cost acetazolamide pill azathioprine 25mg cheap
I have recently started a site, the information you offer on this site has helped me greatly. Thank you for all of your time & work.
buy cheap lanoxin order telmisartan 80mg online cheap buy molnunat 200mg pills
naprosyn 250mg sale order prevacid 30mg pill order prevacid online
buy albuterol 100 mcg online cheap purchase pantoprazole online pyridium order online